LoginSignup
27
30

More than 1 year has passed since last update.

PyCryptodome AES暗号化と復号処理

Last updated at Posted at 2020-10-16

環境

Python3.7
pycryptodome 3.9.8

ソースコード

# -*- coding: utf-8 -*-
from Crypto.Cipher import AES

key = b"1234567890123456" 
data = b"hogehoge" # 暗号化する文字

# 暗号化処理
cipher = AES.new(key, AES.MODE_EAX)
ciphertext, tag = cipher.encrypt_and_digest(data)

print(ciphertext)
print(tag)
print(cipher.nonce)

# 復号処理
cipher_dec = AES.new(key, AES.MODE_EAX, cipher.nonce)
dec_data = cipher_dec.decrypt_and_verify(ciphertext, tag)

print(dec_data)

結果

b'7\xecO,\xa4J\\:'
b'\x8eQ\x95\x0eL\xe2\xa2\xbb\x9e\xf9!\xb7\x83\xbd\xefk'
b'\x16\xe3\xf7`\x0e\x05L/\xf7\xe0\x1a\x067\xa4V\xfa'
b'hogehoge'

参考

27
30
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
27
30