-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lua aes ECB 128 decrypt error #28
Comments
I have the same question. |
Maybe due to a different padding used by PHP? |
@agentzh Thanks for answering.
They have the same encrypted string, when the cipher is then, I tested with the aes of lua-resty-string lib, there is my code local aes = require "resty.aes"
local str = require "resty.string"
local data = "hello"
local aes_128_ecb, err = aes:new("secret", nil, aes.cipher(128,"ecb"), nil)
if err then
ngx.say(err)
ngx.exit(200)
end
local encrypted = aes_128_ecb:encrypt(data)
ngx.say("AES 128 EBC Encrypted HEX: ", str.to_hex(encrypted))
ngx.say("AES 128 EBC Decrypted: ", aes_128_ecb:decrypt(encrypted)) the result is:
The result is not matched, and I do it on a wrong way? |
@SurFire91 Maybe this is helpful: #10 (comment) |
(I have removed my code. It may misleading you.) |
@WiLdWiNd-WH Maybe ecb mode don't need a iv ? |
I'm pretty sure, as @agentzh said, this is issue with different padding. Some info:
Let me illustrate this using my lua-resty-nettle:
local function hex(str,spacer)
return (string.gsub(str,"(.)", function (c)
return string.format("%02X%s",string.byte(c), spacer or "")
end))
end
local aes = require "resty.nettle.aes"
local aes128 = aes.new "secret\0\0\0\0\0\0\0\0\0\0"
local ciphertext = aes128:encrypt "hello\0\0\0\0\0\0\0\0\0\0\0"
print("aes128 ecb encrypt", #ciphertext, hex(ciphertext))
local aes128 = aes.new "secret\0\0\0\0\0\0\0\0\0\0"
local plaintext = aes128:decrypt(ciphertext)
print("aes128 ecb decrypt", #plaintext, plaintext) And when I run this I get this output (just like in PHP):
I'm not sure about how |
And the |
@bungle Thanks |
For PHP mycrypt the padding is \0 and openssl use PKCS#5 padding, so if you use openssl api's to decrypt php's encrypt result you can disabled default padding. Or using my aes-php |
@Wang hai shi ni niubi!!! |
Hi, @agentzh
` |
@haorenfsa What do you think of this pending pull request? |
@agentzh
and the actual key used to encrypt is while in ngx.resty.aes if u input 'some_key' as key, then after md5 the real key is actually And thus bad time comes........ |
@haorenfsa That sounds like a bug to me. We should not do MD5 on the key unless explicitly dictated. |
@agentzh yes, exactlly |
I tryed encrypt a text with a key with PHP code,then it is OK when decrypt it with Java and Object-C ,
I dont know why do you need a sha1 when I just want want decrypt a ECB mode string
local aes_default = aes:new(key,nil,
aes.cipher(128,"ecb"),aes.hash.sha1)
The text was updated successfully, but these errors were encountered: