정보보안공부
python - AES Encrypt / Decrypt 본문
python 설치과정
https://www.microsoft.com/en-us/download/confirmation.aspx?id=44266
설치 후 pip install pycrypto
# Python 2.7 - Encrypt
import base64
import hashlib
from Crypto.Cipher import AES
BS = AES.block_size
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[0:-ord(s[-1])]
key = "12345678901234567890123456789012"; # 32bit
iv=key[:16] # 16bit
String = 'abcd'
print 'Input string: ' + String
cipher = AES.new(key, AES.MODE_CBC, IV=iv)
String = pad(String)
afterCipher = base64.b64encode(cipher.encrypt(String))
print 'Cipher string: ' + afterCipher
=> 결과
# Python 2.7 - Decrypt
import base64
import hashlib
from Crypto.Cipher import AES
BS = AES.block_size
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[0:-ord(s[-1])]
key = "12345678901234567890123456789012"; # 32bit
iv=key[:16] # 16bit
Cipher = "nf2mDLb4L36PWlXz3mNPTw=="
cipher = AES.new(key, AES.MODE_CBC, IV=iv)
deciphed = cipher.decrypt(base64.b64decode(Cipher))
deciphed = unpad(deciphed)
print 'Deciphed string: [' + deciphed +']'
=> 결과
'Language > python' 카테고리의 다른 글
Python 2진수, 16진수 (0) | 2019.12.10 |
---|---|
python_day20_프레임 만들기 (0) | 2017.03.04 |
python_day19_상속2 (0) | 2017.03.04 |
python_day18_상속 (0) | 2017.02.12 |
python_day16_합병정렬, 랜덤합병정렬 (0) | 2017.02.12 |
Comments