I am using the code presented in the documentation of the PyCryptoDome AES CBC documentation.
import json
from base64 import b64encode
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from Crypto.Random import get_random_bytes
data = b"secret"
key = get_random_bytes(16)
cipher = AES.new(key, AES.MODE_CBC)
ct_bytes = cipher.encrypt(pad(data, AES.block_size))
iv = b64encode(cipher.iv).decode('utf-8')
ct = b64encode(ct_bytes).decode('utf-8')
result = json.dumps({'iv':iv, 'ciphertext':ct})
print(result)
The expected output is:
{"iv": "bWRHdzkzVDFJbWNBY0EwSmQ1UXFuQT09", "ciphertext": "VDdxQVo3TFFCbXIzcGpYa1lJbFFZQT09"}
But the output I am getting (Same copy-pasted code is):
{"iv": "PnMwNa/dPBGSkg5LeJ++1g==", "ciphertext": "OGTvHSp2O4dGgfbKbXpSzA=="}
I thought the issue might be caused by the random key, so I run it 20 times in a loop:
import json
from base64 import b64encode
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from Crypto.Random import get_random_bytes
print('Result should be like\n{"iv": "bWRHdzkzVDFJbWNBY0EwSmQ1UXFuQT09", "ciphertext": "VDdxQVo3TFFCbXIzcGpYa1lJbFFZQT09"}')
for x in range(0, 20):
data = b"secret"
key = get_random_bytes(16)
cipher = AES.new(key, AES.MODE_CBC)
ct_bytes = cipher.encrypt(pad(data, AES.block_size))
iv = b64encode(cipher.iv).decode('utf-8')
ct = b64encode(ct_bytes).decode('utf-8')
result = json.dumps({'iv':iv, 'ciphertext':ct})
print(result)
Sadly, all the results were similar:
Result should be like
{"iv": "bWRHdzkzVDFJbWNBY0EwSmQ1UXFuQT09", "ciphertext": "VDdxQVo3TFFCbXIzcGpYa1lJbFFZQT09"}
{"iv": "PnMwNa/dPBGSkg5LeJ++1g==", "ciphertext": "OGTvHSp2O4dGgfbKbXpSzA=="}
{"iv": "FR9GQqA3tssPKn2YHhHe4w==", "ciphertext": "FM3CLY51i0Gvctr9efXGDg=="}
{"iv": "ocsVcOggvjUXj8s2IHXbUg==", "ciphertext": "mR3mYEK4M3QhtUmPt6jwuA=="}
{"iv": "TCox/Pfv5Xjkrm8z3weJ0w==", "ciphertext": "DkkGKXvXpV4B8pV5B4ctxw=="}
{"iv": "eZvCrk97EC4eL1kO5PQ+Dg==", "ciphertext": "idaLj7wl/F79qUdzZodbBg=="}
{"iv": "KqwaWZTxU5ZFn6Ekq/wWEg==", "ciphertext": "A25LP0wNTJRkndhvuhuasw=="}
{"iv": "FlFNCnQHmUajP2jNDvIiLw==", "ciphertext": "T9vOu0z9DkUrXCUGqXacxw=="}
{"iv": "U74vTvWZNhKbp5sq9uT9Sg==", "ciphertext": "vfJ1Brftb897O6xTMI3MQg=="}
{"iv": "tIVgKly8wyhpwdoRQ6SzqA==", "ciphertext": "EjnyNuIPVmt7cnEaFSsoww=="}
{"iv": "5qoVwEB7WfHGSGZvHY25Hg==", "ciphertext": "RBxS7gIMAfZqS5a9vTEcjQ=="}
{"iv": "NCHdCr09s9pMQsHK+4ECjA==", "ciphertext": "V8ezqaw8WrrygrokrD48ew=="}
{"iv": "x+T+fM3diahDBie4o7fvpA==", "ciphertext": "0xnjaIArYsfKn5JkjXnniQ=="}
{"iv": "v0r3123VCkZy7zOVixyBkA==", "ciphertext": "J2U0MEHqDEwC45UCY7+SdA=="}
{"iv": "l1iA//cLxESUax39ZanyRw==", "ciphertext": "QLC74BydGuQ1J73PUvKl+Q=="}
{"iv": "FcsfeZ3iCrVVaRsZSm3Gsw==", "ciphertext": "wyiZEo1VpdM/u+ucstzn6Q=="}
{"iv": "av6FSkAzfkOyfLvxDIHY9g==", "ciphertext": "EuexPtCb69+37F9INqWemw=="}
{"iv": "a8F+8OEv1ieHcgwMXocEww==", "ciphertext": "6rd04diPiA7tw/4MgIJsWw=="}
{"iv": "jUY115jhERX2KI+dtVajlQ==", "ciphertext": "TRC5RR3qX6EDjjH1kmO6ew=="}
{"iv": "fcW66kw4gpEWRvNf3AQg7g==", "ciphertext": "o1G6luiDT1Cf2bmZlTu57A=="}
{"iv": "U5FbVnDONa0h4Q6PDJo60A==", "ciphertext": "ilGJKuKNxUxx/Lxz5N85NQ=="}
So, my question is, what could be possibly causing this mismatch? Also, please note that the IV size is wrong compared to the expected output