Python3 PyCrypto - ValueError: IV must be 16 bytes long
Python3 PyCrypto - ValueError: IV must be 16 bytes long
我正在试验 PyCrypto
库。我的朋友给了我他们用来加密字符串、密文和 IV 的 public 密钥 (base64)。他们使用的密码是AES-CBC
。我认为这会相对简单,但我收到错误 ValueError: IV must be 16 bytes long
。以下是我的代码、回溯和发现:
CODE:
import sys
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
message = "GgFyMNGjCOnrE3NjNEYko57ZUdy36ldB+/WrR5+ctN6jglicmb3ONvY4mfswk09BUmm0rCBy6yyaTFWKWuEgnMtu0RpbyyHICCQAVTmgwtgQ9wWlE1BJJAHJsZd61cMlsLomtRtCuNOnX5fnoGcs2X=="
key = b'xSAU2gWfDhWusUPplf8RtWknJ0ZKZ+dqK23/go8i0ls='
print(sys.getsizeof(key))
iv = b'WmiBaaAuyX7YCSTTPj07/c=='
print(sys.getsizeof(iv))
aes = AES.new(key, AES.MODE_CBC, iv)
decd = aes.decrypt(message)
print(decd)
TRACE:
77
65
Traceback (most recent call last):
File "main.py", line 21, in <module>
aes = AES.new(key, AES.MODE_CBC, iv)
File "/usr/local/lib/python3.7/site-packages/Crypto/Cipher/AES.py", line 95, in new
return AESCipher(key, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/Crypto/Cipher/AES.py", line 59, in __init__
blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/Crypto/Cipher/blockalgo.py", line 141, in __init__
self._cipher = factory.new(key, *args, **kwargs)
ValueError: IV must be 16 bytes long
如您所见,密钥大小为 77 字节,IV 大小为 65 字节。我只是使用提供给我的密钥和 IV 来加密原始文本(我知道将密钥放在纯文本中的安全隐患。这些不是实际的密钥,只是相同长度的随机字符串演示目的)。
你需要同时解码 key
和 iv
但你还必须解码 message
否则它不会像你使用的那样是 16 字节对齐的 CBC
模式。
from Crypto.Cipher import AES
import base64
message = b"GgFyMNGjCOnrE3NjNEYko57ZUdy36ldB+/WrR5+ctN6jglicmb3ONvY4mfswk09BUmm0rCBy6yyaTFWKWuEgnMtu0RpbyyHICCQAVTmgwtgQ9wWlE1BJJAHJsZd61cMlsLomtRtCuNOnX5fnoGcs2X=="
message = base64.b64decode(message)
key = b'xSAU2gWfDhWusUPplf8RtWknJ0ZKZ+dqK23/go8i0ls='
key = base64.b64decode(key)
iv = b'WmiBaaAuyX7YCSTTPj07/c=='
iv = base64.b64decode(iv)
aes = AES.new(key, AES.MODE_CBC, iv)
decd = aes.decrypt(message)
print(decd)
我正在试验 PyCrypto
库。我的朋友给了我他们用来加密字符串、密文和 IV 的 public 密钥 (base64)。他们使用的密码是AES-CBC
。我认为这会相对简单,但我收到错误 ValueError: IV must be 16 bytes long
。以下是我的代码、回溯和发现:
CODE:
import sys
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
message = "GgFyMNGjCOnrE3NjNEYko57ZUdy36ldB+/WrR5+ctN6jglicmb3ONvY4mfswk09BUmm0rCBy6yyaTFWKWuEgnMtu0RpbyyHICCQAVTmgwtgQ9wWlE1BJJAHJsZd61cMlsLomtRtCuNOnX5fnoGcs2X=="
key = b'xSAU2gWfDhWusUPplf8RtWknJ0ZKZ+dqK23/go8i0ls='
print(sys.getsizeof(key))
iv = b'WmiBaaAuyX7YCSTTPj07/c=='
print(sys.getsizeof(iv))
aes = AES.new(key, AES.MODE_CBC, iv)
decd = aes.decrypt(message)
print(decd)
TRACE:
77
65
Traceback (most recent call last):
File "main.py", line 21, in <module>
aes = AES.new(key, AES.MODE_CBC, iv)
File "/usr/local/lib/python3.7/site-packages/Crypto/Cipher/AES.py", line 95, in new
return AESCipher(key, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/Crypto/Cipher/AES.py", line 59, in __init__
blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/Crypto/Cipher/blockalgo.py", line 141, in __init__
self._cipher = factory.new(key, *args, **kwargs)
ValueError: IV must be 16 bytes long
如您所见,密钥大小为 77 字节,IV 大小为 65 字节。我只是使用提供给我的密钥和 IV 来加密原始文本(我知道将密钥放在纯文本中的安全隐患。这些不是实际的密钥,只是相同长度的随机字符串演示目的)。
你需要同时解码 key
和 iv
但你还必须解码 message
否则它不会像你使用的那样是 16 字节对齐的 CBC
模式。
from Crypto.Cipher import AES
import base64
message = b"GgFyMNGjCOnrE3NjNEYko57ZUdy36ldB+/WrR5+ctN6jglicmb3ONvY4mfswk09BUmm0rCBy6yyaTFWKWuEgnMtu0RpbyyHICCQAVTmgwtgQ9wWlE1BJJAHJsZd61cMlsLomtRtCuNOnX5fnoGcs2X=="
message = base64.b64decode(message)
key = b'xSAU2gWfDhWusUPplf8RtWknJ0ZKZ+dqK23/go8i0ls='
key = base64.b64decode(key)
iv = b'WmiBaaAuyX7YCSTTPj07/c=='
iv = base64.b64decode(iv)
aes = AES.new(key, AES.MODE_CBC, iv)
decd = aes.decrypt(message)
print(decd)