美文网首页
AES 256 Lua + PHP

AES 256 Lua + PHP

作者: mellete | 来源:发表于2020-06-17 16:55 被阅读0次

    PHP OpenSSL 已经封装了 base64 ,encrypt 后不需要再次 base64,lua就需要单独操作

    lua 加密后 PHP不能解密,具体原因还没了解到底层,现在测试成功了 CBC模式的

    先上 CBC模式的代码吧

    PHP代码

    
    public static function aesEncrypt($data, $method, $key, $iv, $options = 0)
    
    {
    
        if (empty($data)) {
    
            return '';
    
        }
    
        $validMethods = openssl_get_cipher_methods();
    
        if (!in_array($method, $validMethods)) {
    
            throw new \Exception('param `method` is invalid`');
    
        }
    
        return openssl_encrypt($data, $method, $key, $options, $iv);
    
    }
    
    
    
    public static function aesDecrypt($data, $method, $key, $iv, $options = 0)
    
    {
    
        if (empty($data)) {
    
            return '';
    
        }
    
        $validMethods = openssl_get_cipher_methods();
    
        if (!in_array($method, $validMethods)) {
    
            throw new \Exception('param `method` is invalid`');
    
        }
    
        return openssl_decrypt($data, $method, $key, $options, $iv);
    
    }
    
    

    Lua代码:

    
    local _M = {}
    
    local base64_encode = ngx.encode_base64
    
    local base64_decode = ngx.decode_base64
    
    local aes   = require("resty.aes")
    
    
    _M.aes_128_cbc = 'AES-128-CBC'
    
    _M.aes_128_ecb = 'AES-128-ECB'
    
    _M.aes_256_cbc = 'AES-256-CBC'
    
    _M.aes_256_ecb = 'AES-256-ECB'
    
    
    local ciphers = {
    
        [_M.aes_128_cbc] = aes.cipher(128, 'cbc'),
    
        [_M.aes_128_ecb] = aes.cipher(128, 'ecb'),
    
        [_M.aes_256_cbc] = aes.cipher(256, 'cbc'),
    
        [_M.aes_256_ecb] = aes.cipher(256, 'ecb')
    
    }
    
    --aes加密
    
    function _M.aes_encrypt(data, key, method, iv)
    
        local aes_cipher
    
        if method and ciphers[method] then
    
            aes_cipher = ciphers[method]
    
        end
    
        -- the default cipher is AES 128 CBC with 1 round of MD5
    
        local aes_obj, err   = aes:new(key, nil, aes_cipher, {iv = iv})
    
        if not aes_obj then
    
            return nil, err
    
        end
    
        local encrypted, err = aes_obj:encrypt(data)
    
        if not encrypted then
    
            return nil, err
    
        end
    
        return base64_encode(encrypted)
    
    end
    
    
    --aes解密
    
    function _M.aes_decrypt(encode_data, key, method, iv)
    
        local aes_cipher
    
        if method and ciphers[method] then
    
            aes_cipher = ciphers[method]
    
        end
    
        -- the default cipher is AES 128 CBC with 1 round of MD5
    
        local aes_obj, err = aes:new(key, nil, aes_cipher, {iv = iv})
    
        if not aes_obj then
    
            return nil, err
    
        end
    
        local data = base64_decode(encode_data)
    
        if not data then
    
            return nil
    
        end
    
        data, err = aes_obj:decrypt(data)
    
        if not data then
    
            return nil, err
    
        end
    
        return data
    
    end
    
    

    相关文章

      网友评论

          本文标题:AES 256 Lua + PHP

          本文链接:https://www.haomeiwen.com/subject/jhbsxktx.html