1

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

许可证密钥字段可能有 3 种情况:

  1. 它可能为空 - 一些用户还没有存储在数据库中的许可证密钥。
  2. 它可能是 25 位的许可证,每 5 个字符用连字符分隔:例如:ABCD1-EFGH2-IJKL3-MNOP4-QRST5
  3. 可能是10位的license,都是数字,没有分隔符:例如:1234567890

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

我想我需要从检查许可证的 strlen 开始。

  1. 如果为 0,则什么也不做。我可以在加载函数之前检查它是否为空。
  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;
}

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

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

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

有任何想法吗?

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

4

3 回答 3

4

“@Fred 和 CaptainCarl 你们都是对的,我怎么可能没有意识到......将 $st 更改为 $first 等等...... – BottyZ”

提交作为答案:

这里的问题是您的变量以数值开头;不能那样做。$a1st在以$1st字母开头时做类似的事情。

Stack 上的参考资料,您可以阅读:

于 2016-04-13T14:14:20.573 回答
1

有效的变量名称以字母或下划线开头,后跟任意数量的字母、数字或下划线。作为正则表达式,它会这样表达:'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

https://secure.php.net/manual/en/language.variables.basics.php

你的变量的命名,例如$1st是无效的。

于 2016-04-13T14:13:12.263 回答
0

您还可以在 MySQL 5.7 或 MariaDB 10.1 中加密数据库中的数据。您可以使用单个字段、表或完整的表空间(包括日志文件等)来完成。

见:https ://mariadb.com/kb/en/mariadb/encryption/

于 2016-04-13T14:36:05.313 回答