I want to store software license keys in a field of my mysql db, but I want to have the license number stored in a scrambled format so that if the db is compromised the license keys won't be usable.
There are 3 scenarios possible with the license key field:
I'd like to scramble the license prior to storing, then when displaying to the user once logged in, run the same scramble function again to unscramble the license.
I think I would need to start by checking the strlen of the license.
I've setup the following:
function scramble($scramblestr) {
// check string length
$length = strlen($scramblestr);
// if 10 digit license (all numbers)
if ($length == 10) {
$1st = substr($scramblestr, 0, 1);
$2nd = substr($scramblestr, 1, 1);
$3rd = substr($scramblestr, 2, 1);
$4th = substr($scramblestr, 3, 1);
$5th = substr($scramblestr, 4, 1);
$6th = substr($scramblestr, 5, 1);
$7th = substr($scramblestr, 6, 1);
$8th = substr($scramblestr, 7, 1);
$9th = substr($scramblestr, 8, 1);
$10th = substr($scramblestr, 9, 1);
// swap 3rd character with 7th / swap 5th character with 9th
$scramblestr = $1st . $2nd . $7th . $4th . $9th . $6th . $3rd . $8th . $5th . $10th;
// if 25 digit license (with hyphen separators)
} elseif ($length == 29) {
$scramblestr = array_filter(explode('-', $scramblestr), 'strlen');
// swap 2nd & 4th sections
$scramblestr = $scramblestr[0] . "-" . $scramblestr[3] . "-" . $scramblestr[2] . "-" . $scramblestr[1] . "-" . $scramblestr[4];
// swap alpha characters 13 places in the alphabet
$scramblestr = str_rot13($scramblestr);
// if null or if stored incorrectly (for example if the license is not null but contains an invalid number of characters)
} else {
$scramblestr = "Unknown";
}
return $scramblestr;
}
However, this results in the following server 500 error:
PHP Parse error: syntax error, unexpected '1' (T_LNUMBER), expecting variable (T_VARIABLE) or '$'
This points to the 1st substr reference. However, according to php.net it should be a integer used here to demark length of string.
Any ideas?
Or is there a more efficient way of performing this action? Or does anyone have any alternative methods which may suit?
"@Fred & CaptainCarl you were both right, how could I of not realised that... changed the $st to $first and so on... – BottyZ"
Submitting as an answer:
The problem here is that your variables start with a numeric value; can't do that. Do something like $a1st
as opposed to $1st
while starting with a letter.
References on Stack you can have a read: