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

I want to scramble / unscramble a software license key into / from mysql database

我想在我的 mysql 数据库的一个字段中存储软件许可证密钥,但我想以加密格式存储许可证号,这样如果数据库被破坏,许可证密钥就不会可用。

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

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

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

我想我需要先检查许可证的 strlen。

  1. 如果为 0,则什么都不做。我可以在加载函数之前检查它是否为 null。
  2. 如果是 29,则使用连字符分隔符来随机排列许可证的部分,例如第 2 和第 4 部分,并可能使用 str_rot13 更改字母字符。
  3. 如果是 10,select 说出第 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) {
        st = substr($scramblestr, 0, 1);
        nd = substr($scramblestr, 1, 1);
        rd = substr($scramblestr, 2, 1);
        th = substr($scramblestr, 3, 1);
        th = substr($scramblestr, 4, 1);
        th = substr($scramblestr, 5, 1);
        th = substr($scramblestr, 6, 1);
        th = substr($scramblestr, 7, 1);
        th = substr($scramblestr, 8, 1);
        th = substr($scramblestr, 9, 1);

        // swap 3rd character with 7th / swap 5th character with 9th
        $scramblestr = st . nd . th . th . th . th . rd . th . th . th;

    // 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 Parse error: syntax error, unexpected '1' (T_LNUMBER), expecting variable (T_VARIABLE) or '$'

这指向第一个 substr 引用。但是,根据php.net,这里应该是一个整数来区分字符串的长度。

有什么想法吗?

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

A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

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

变量的命名,例如st 无效。

"@Fred & CaptainCarl you were both right, how could I of not realised that... changed the $st to $first and so on... – BottyZ"

作为答案提交:

这里的问题是你的变量以数值开头;不能那样做。以字母开头时,执行类似 $a1st 而不是 st 的操作。

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

  • Why variable can not start with a Number?
  • Why can't variable names start with numbers?

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

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