[python] M2Crypto를 사용하여 AES 암호화를 수행하는 방법은 어떻게 되나요?
먼저, M2Crypto 라이브러리를 설치해야 합니다. 다음 명령을 사용하여 설치할 수 있습니다:
pip install M2Crypto
다음은 AES 암호화를 수행하는 예제 코드입니다:
from M2Crypto.EVP import Cipher
def aes_encrypt(key, iv, plaintext):
cipher = Cipher(alg='aes_128_cbc', key=key, iv=iv, op=1)
ciphertext = cipher.update(plaintext)
ciphertext += cipher.final()
return ciphertext
def aes_decrypt(key, iv, ciphertext):
cipher = Cipher(alg='aes_128_cbc', key=key, iv=iv, op=0)
plaintext = cipher.update(ciphertext)
plaintext += cipher.final()
return plaintext
# 키와 초기화 벡터 설정
key = b'mysecretkey123456'
iv = b'initialvector78'
# 평문
plaintext = b'This is a secret message'
# 암호화
encrypted_data = aes_encrypt(key, iv, plaintext)
print("암호화된 데이터:", encrypted_data)
# 복호화
decrypted_data = aes_decrypt(key, iv, encrypted_data)
print("복호화된 데이터:", decrypted_data.decode('utf-8'))
이 예제 코드는 M2Crypto를 사용하여 AES-128 CBC 모드로 암호화 및 복호화하는 방법을 보여줍니다.
더 자세한 내용은 M2Crypto 문서를 참조하시기 바랍니다.