LoginSignup
3
3

More than 5 years have passed since last update.

PythonでnosaltなAES暗号化をする

Posted at

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")

備忘録的に残す。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3