Skip to content

aes.lua貌似有些不对劲 #10

@netyum

Description

@netyum

你好,春哥
查看源码,感觉好像password(salt)没有起做用,使用的是key做为password了,那么,只有在lua下加密,lua下解密了,和其他语言无法交换数据!
代码片段

local aes = require "resty.aes"
local str = require "resty.string"
local hash = {
        iv = "fedcba9876543210",
        method = nil
}
local salt = "0123456789abcdef"
local data = "hello"
local aes_128_cbc, err = aes:new("AKeyForAES128CBC", salt, aes.cipher(128,"cbc"), hash)
if err  then
        ngx.say(err)
        ngx.exit(200)
end
local encrypted = aes_128_cbc:encrypt(data)
ngx.say("AES 128 CBC Encrypted HEX: ", str.to_hex(encrypted) .. "<br />")
ngx.say("AES 128 CBC Decrypted: ", aes_128_cbc:decrypt(encrypted))

屏幕回显是

AES 128 CBC Encrypted HEX: 5dff16410bf805de1d14d96fbffcab7c
AES 128 CBC Decrypted: hello

这里我使用了AKeyForAES128CBC 做为key,但这个更好的写法应该是aes-128-cbc,或者说这个key跟本也没有什么用吧,因为后面的cipher才确定了使用什么算法,因为我要自定义向量iv,查看源码结构,构造了一个hash table,确定初始化向量,这样问题就来了,这个key你源代码里写的一个检查长度,以上面为例,则必须是长度16,

好的,也这可以,但是在下面生成gen_key的时候,你使用的就是这个key,而不是salt,
当我将源码中的#key改成#salt,key改成slat后,得到的结束是正确的,另一种不指定hash或指定一个hash在aes中的hash table中的一个算法时,不知道有没有问题,可能也有问题,因为貌似也使用到了key

我不清楚这个key与salt的区别,不知道你的初衷是key只做为一个描述存在的吗?如果参与计算,貌似比较麻烦。
下面是php的代码片段

<?php
$iv = 'fedcba9876543210';
$salt = '0123456789abcdef';
$data = 'hello';
echo bin2hex(openssl_encrypt($data, "aes-128-cbc", $salt, true, $iv));

屏幕回显是

7bedceb0655f38941069789f36462091

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions