我想将软件许可证密钥加密/解密到mysql数据库中

Bot*_*tyZ 1 php mysql syntax-error scramble

我想将软件许可证密钥存储在我的mysql数据库的一个字段中,但我希望以乱码格式存储许可证号,这样如果数据库被泄露,许可证密钥将无法使用.

许可证密钥字段有3种可能的方案:

  1. 它可能为null - 某些用户还没有存储在数据库中的许可证密钥.
  2. 它可能是25位数的许可证,每5个字符由一个超级分隔:例如:ABCD1-EFGH2-IJKL3-MNOP4-QRST5
  3. 它可能是10位数的许可证,所有这些都是数字,并且没有分隔符:例如:1234567890

我想在存储之前加扰许可证,然后在登录后向用户显示时,再次运行相同的加扰功能以解密许可证.

我想我需要先检查许可证的格式.

  1. 如果为0,则什么也不做.我可以在加载函数之前检查它是否为null.
  2. 如果是29,则使用连字符分隔符来混洗许可证的各个部分,例如2nd和4th,并且可以使用str_rot13来更改字母字符.
  3. 如果是10,请选择说出第3,第5,第7和第9个字符并更改其顺序.比如第5名第9名和第3名第7名.

我设置了以下内容:

    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;
}
Run Code Online (Sandbox Code Playgroud)

但是,这会导致以下服务器500错误:

PHP解析错误:语法错误,意外的'1'(T_LNUMBER),期望变量(T_VARIABLE)或'$'

这指向第一个子参考.但是,根据php.net,它应该是一个整数,用于标记字符串的长度.

有任何想法吗?

或者是否有更有效的方法来执行此操作?或者有没有人有任何替代方法可能适合?

Fun*_*ner 6

"@Fred&CaptainCarl你们都是对的,我怎么能没有意识到...将$ st改为$ first等等...... - BottyZ"

提交答案:

这里的问题是你的变量以数值开头; 不能那样做.做一些事情$a1st,而不是$1st从一封信开始.

Stack上的参考文献你可以读一下: