์ ์ ์ฌ๋ฆฐ ๊ธ์ ์ฃผ์ ์๋ AES๋ฅผ ํ์ด์ฌ์ผ๋ก ์ํธํ ํด๋ณด๊ธฐ๋ก ํ๋ค...
ํ์ด์ฌ์ ์ด์ฉํ์ฌ ์ํธํ๋ฅผ ํ๊ธฐ ์ํด์ ๋ pycryptodome์ ์ฌ์ฉํ ๊ฒ์ด๋ค.
pip install PyCryptodome
์์ ์ฝ๋๋ฅผ ์ ๋ ฅํ๋๋
Raquirement already satisfied: PyCryptodome in /home/bassel/anaconda3/lib/python3.8/site-packages (3.9.9)
๋ผ๊ณ ๋์๋ค. ๊ทธ๋ผ ์ด์ ํ์ด์ฌ์ผ๋ก ์ฝ๋๋ฅผ ์ ๋ ฅํ๋ฉด ๋๋ค.
from Crypto.Cipher import AES
from secrets import token_bytes
key = token_bytes(16)
def encrypt(msg):
cipher = AES.new(key, AES.MODE_EAX)
nonce = cipher.nonce
ciphertext, tag = cipher.encrypt_and_digest(msg.encode('ascii'))
return nonce, ciphertext, tag
def decrypt(nonce, ciphertext, tag):
cipher = AES.new(key, AES.MODE_EAX, nonce=nonce)
plaintext = cipher.decrypt(ciphertext)
try:
cipher.verify(tag)
return plaintext.decode('ascii')
except:
return False
nonce, ciphertext, tag = encrypt(input('Enter a message: '))
plaintext = decrypt(nonce, ciphertext, tag)
print(f'Cipher text: {ciphertext}')
if not plaintext:
print('Message is corrupted')
else:
print(f'Plain text: {plaintext}')
์ด๋ ๊ฒ ์ฝ๋๋ฅผ ์ ๋ ฅํ๋ค.
ํฐ๋ฏธ๋์ ํ์ผ๋ช ์ธ
python aes_encryption.py
๋ฅผ ์ ๋ ฅํ๋๋
Enter a message:
๋ผ๊ณ ๋ฌ๋ค. ๊ทธ๋ฆฌ๊ณ ์
๋ ฅ์ฐฝ์ ์๋ฌด ๋ฉ์ธ์ง๋ ์
๋ ฅํด์ enter๋ฅผ ๋๋ฅด๋ฉด AES ์๊ณ ๋ฆฌ์ฆ์ ํตํด Cipher text์ Plain text๊ฐ ์ฐจ๋ก๋๋ก ๋ํ๋๋ค.
Reference
https://pycryptodome.readthedocs.io/en/latest/src/cipher/aes.html?highlight=AES