[python] M2Crypto를 사용하여 대칭키 암호화를 수행하는 방법은 어떻게 되나요?
먼저, M2Crypto를 설치해야 합니다. 다음 명령을 사용하여 pip를 통해 설치할 수 있습니다.
pip install M2Crypto
이제 다음과 같은 방법으로 대칭키 암호화를 수행할 수 있습니다.
from M2Crypto import EVP
def symmetric_encrypt(key, data):
cipher = EVP.Cipher(alg='aes_256_cbc', key=key, iv='1234567812345678', op=1)
ciphertext = cipher.update(data)
ciphertext += cipher.final()
return ciphertext
def symmetric_decrypt(key, data):
cipher = EVP.Cipher(alg='aes_256_cbc', key=key, iv='1234567812345678', op=0)
plaintext = cipher.update(data)
plaintext += cipher.final()
return plaintext
# 키 생성
key = b'mysecretkey'
# 암호화할 데이터
data = b'this is a secret message'
# 대칭키로 데이터 암호화
encrypted_data = symmetric_encrypt(key, data)
print("암호화된 데이터:", encrypted_data)
# 암호화된 데이터를 다시 원래 데이터로 복호화
decrypted_data = symmetric_decrypt(key, encrypted_data)
print("복호화된 데이터:", decrypted_data.decode())
위 코드에서는 M2Crypto의 EVP 모듈을 사용하여 AES 알고리즘을 통해 대칭키 암호화와 복호화를 수행하고 있습니다.
M2Crypto를 사용하여 대칭키 암호화를 수행하는 방법에 대한 예제를 제공했습니다. 자세한 내용은 M2Crypto 공식 문서를 참조하시기 바랍니다.