如何用特定键和IV长度加密AES / CBC 128
我正在尝试使用AES CBC 128加密字符串,但是每次
self._cipher = factory.new(key, args,kwargs)value error:iv必须是16个字节长
我正在使用pycrypto库,这是我的代码,
import base64
from Crypto.Cipher import AES
def pad(m):
return m+chr(16-len(m)%16)*(16-len(m)%16)
msg = 'Hello there I am new in Python'
ciph = AES.new("asdfghjkl", AES.MODE_CBC, "asdfghjkl")
enc = ciph.encrypt(pad(msg))
base = base64.b64encode(enc)
print (base)
每当我将键和IV长度更改为16位时,它都可以正常工作...但是我只想使用此键加密字符串,IV是“ ” asdfghjkl “
我已经看到了许多在线网站,这些网站用任何键,iv engent Gentings Gemprypt Strings。
提前致谢。
I am trying to encrypt a string using AES cbc 128 but I got this problem every time
self._cipher = factory.new(key, args, kwargs)ValueError: IV must be 16 bytes long
I am using pycrypto library and here is my code,
import base64
from Crypto.Cipher import AES
def pad(m):
return m+chr(16-len(m)%16)*(16-len(m)%16)
msg = 'Hello there I am new in Python'
ciph = AES.new("asdfghjkl", AES.MODE_CBC, "asdfghjkl")
enc = ciph.encrypt(pad(msg))
base = base64.b64encode(enc)
print (base)
Whenever I change my key and IV length to 16 bit it worked ... but I want to encrypt the string only with this key and IV which is "asdfghjkl"
I have seen a lot of online websites that encrypt strings same way with any key , IV length .. but I did not figure it ... how does it work>
thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,迈克(Mike)如“评论”中的评论使用16个字节IV和16、24或32个字节的密钥大小,因此不可能加密字符串或任何内容,除非您为您做了一些技巧,否则使用短IV和键示例用零的值扩展长度,直到它变为16个字节。.
您可以非常简单地将您的密码转换为iv并添加零,直到在您的情况下变为16个“ asdfghjkl ”将是在十六进制中“ 61 73 64 66 67 68 68 6B 6B 6C ”,因此您需要添加 0000000000000000 这样它将在没有任何错误的情况下工作。 。
Well Mike as in the comments AES uses a 16 bytes IV and 16, 24 or 32 bytes for the key size , so it is not possible to encrypt the Strings or whatever it was, with a short IV and Key unless u did some tricks for example extend the length with the value of Zeros until it becomes 16 Byte..
You can very simply convert ur password and IV to hex and add zeros until it becomes 16 in ur case the "asdfghjkl" will be in hex "61 73 64 66 67 68 6a 6b 6c " so u will need to add 00000000000000 this way it will work without any errors.. but it is not secure.. in python u can use unhexlify() to convert the hex key and iv to bytes.