【问题标题】:I want to scramble / unscramble a software license key into / from mysql database我想将软件许可证密钥加扰/解扰到 mysql 数据库中/从 mysql 数据库中
【发布时间】:2016-08-04 16:34:12
【问题描述】:

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

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

  1. 它可能为 null - 一些用户还没有存储在数据库中的许可证密钥。
  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,它应该是这里用来标记字符串长度的整数。

有什么想法吗?

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

【问题讨论】:

  • 不太确定:但变量不能(部分)是数字,不是吗?
  • 为什么不直接散列呢?
  • 这里的问题是你的变量以数值开头;不能那样做。做$a1st 而不是$1st 等。将此作为某种形式的“答案”。 @BottyZ
  • 可能值得一提的是,如果您的数据库遭到入侵,那么您的代码也可能同样如此。这意味着获得您的数据库的人可能可以解码所有密钥。

标签: php mysql syntax-error scramble


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    作为答案提交:

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

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

    【讨论】:

    • 把我的学分还给我!不,我很高兴它成功了。
    • @CaptainCarl 我有 ;-)
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-10-01
      • 1970-01-01
      • 2015-03-30
      • 2013-06-27
      • 2019-02-13
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      相关资源
      最近更新 更多