opensslで次のようなAES-256のCBC暗号化で'-nosalt'オプションを付けたような暗号化をPythonでも実現したい時。
echo "this is secret"|openssl enc -aes-256-cbc -e -base64 -pass pass:hogehoge -p -nosalt
こんな感じでできました。
def encryptAes(str, encKey, key_length=32, iv_length=16):
d = d_i = ''
while len(d) < key_length + iv_length:
d_i = md5(d_i + encKey).digest()
d += d_i
key = d[:key_length]
iv = d[key_length:key_length+iv_length]
cipher = AES.new(key, AES.MODE_CBC, iv)
_str = str + (iv_length - len(str) % iv_length) * chr(iv_length - len(str) % iv_length)
return base64.b16encode(cipher.encrypt(_str)).lower()
print encyptAes("this is secret","hogehoge")
備忘録的に残す。