douyanxing6054 2015-01-20 01:12 采纳率: 0%
浏览 12
已采纳

将数据嵌入序列号中

I am looking at implementing a PVKS as outlined here. I have it working as presented in the article (but in PHP), however I have an extra requirement I'm trying to figure out how to fulfill. I'm trying to figure out how I should embed a date and 3 digit number into the generated code. I'm not even sure where to start here, so I honestly haven't tried anything. They unfortunately won't be unique in combination, so I can't hash them for the seed value. As far as I can tell I can't have them be part of the getKeyByte function either, as the inputs to that function should be only the seed and some arguments, where those arguments define the algorithm for a valid key. They have to be the same between the generator and validator so they have to be static. Is there an accepted practice for this kind of task?

  • 写回答

1条回答 默认 最新

  • dougaimian1143 2015-01-21 04:24
    关注

    Managed to figure this one out myself. I realized I was over complicating things by trying to find a way I could encode a value into a keybyte. The way to do this (well, the way I did this) is just to add extra bytes after the keybytes and before the checksum. They don't need to be validated like the other keybytes, but do affect the checksum.

    Below is the part of my class related to generating and validating keys, with the new sections I needed to add noted.

    static public function generateKey($extraKeyArgs = array(), $encodedData = array())
    {
        $args = self::instanceKeyArgs($extraKeyArgs);
        $keyBytes = array();
        $seed = self::getSeed();
        $hexSeed = self::intToHex($seed,self::$seedWidth);
        $key = $hexSeed;
        $numKeys = count($args);
    
        for ($i=0; $i < $numKeys; $i++) {
            list($a, $b, $c) = $args[$i];
            $keyBytes[$i] = self::getKeyByte($seed, $a, $b, $c, self::$keyWidthBytes);
            $key .= self::intToHex($keyBytes[$i],self::$keyWidthHex);
        }
    
        // Section added to handle encoded data
        foreach ($encodedData as $data) {
            // Make $data an integer value, one byte wide.
            $data = (((int) $data) & 255);
            $keyBytes[] = $data;
            $numKeys++;
            $key .= self::intToHex($data,self::$keyWidthHex);
        }
        // End Section
    
        $checksum = self::getChecksum($key);
    
        $key = $hexSeed . self::$seperator;
        for ($i=0; $i < $numKeys; $i++) {
            $key .= self::intToHex($keyBytes[$i],self::$keyWidthHex);
            if ($i & 1) {
                $key .= self::$seperator;
            }
        }
    
        if (substr($key, -1) !== self::$seperator) {
            $key .= self::$seperator;
        }
    
        $key .= $checksum;
    
        return $key;
    }
    
    static public function checkKey($key, $extraKeyArgs = array(), &$data = array())
    {
        $args = self::instanceKeyArgs($extraKeyArgs);
        $numKeys = count($args);
    
        if (!self::checkKeyChecksum($key)) {
            return false;   // Failed checksum! Maybe a mistype or optical reader error?
        }
    
        $key = self::normalizeKey($key);
    
        // TODO - we would check against a blacklist here if we wanted to implement that.
    
        $seed = hexdec(substr($key,0,self::$seedWidth));
        if (!is_int($seed) || $seed < 1) {
            return false;   // Failed to get seed. Are you sure this key came from here?
        }
    
        $key = substr($key, self::$seedWidth, (strlen($key) - (self::$seedWidth + self::$checksumWidth)));
        for ($i=0; $i < $numKeys; $i++) {
            $keyByte = substr($key, 0, self::$keyWidthHex);
            $key = substr($key, self::$keyWidthHex);
            list($a, $b, $c) = $args[$i];
            if ($keyByte !== self::intToHex(self::getKeyByte($seed, $a, $b, $c, self::$keyWidthBytes),2)) {
                return false;   // Key byte failed check. Possible forgery attempt?
            }
        }
    
        // This line added to handle encoded data
        $data = array_map('hexdec', str_split($key, self::$keyWidthHex));
    
        return true; // Valid Key, Yay!
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?