필요한 모듈 임포트
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES
import base64
class AESCipher:
def __init__(self, key):
self.key = key
def encrypt(self, raw):
cipher = AES.new(self.key, AES.MODE_CBC)
ciphertext_bytes = cipher.encrypt(raw.ljust(len(raw) + AES.block_size - len(raw) % AES.block_size))
return base64.b64encode(ciphertext_bytes)
def decrypt(self, enc):
enc = base64.b64decode(enc)
cipher = AES.new(self.key, AES.MODE_CBC)
decrypted_bytes = cipher.decrypt(enc)
return decrypted_bytes.rstrip(b"\0").decode("utf-8")
key = get_random_bytes(16)
aes_cipher = AESCipher(key)
plaintext = "Hello, World!"
encrypted_text = aes_cipher.encrypt(plaintext)
decrypted_text = aes_cipher.decrypt(encrypted_text)
print("원본 텍스트:", plaintext)
print("암호화된 텍스트:", encrypted_text)
print("복호화된 텍스트:", decrypted_text)