1. Rootλ‘ μ μνλ€.
$ sudo su root
2. μνΈνμ νμν Moduleμ μ€μΉνλ€.
3. μνΈν, 볡νΈνμ λν μ½λ© κ°λ¨ μ€λͺ
μλ μ 체 μ½λκ° μμ΅λλ€!
A. import module
B. HMAC hash
C. iv μμ±
D. 볡νΈν
E. μνΈν
F. μ€ν
μ 체μ½λ
# -*- coding: utf-8 -*-
import hmac, binascii
import Crypto.Random
from Crypto.Cipher import AES
def gen_sha256_hashed_key_salt(key):
salt1 = hmac.new(key).digest()
return salt1
#iv κ°μ λλ€μΌλ‘ λ°μμ€λ μ½λ.
def gen_random_iv():
return Crypto.Random.OSRNG.posix.new().read(AES.block_size)
#[ 볡νΈν κ³Όμ ] μνΈν λ¬Έμ₯μ νΈλ¬ΈμΌλ‘ ν΄λ
νλ μ½λ.
def AES256Decrypt(key, iv, cipher):
encryptor = AES.new(gen_sha256_hashed_key_salt(key), AES.MODE_CBC, IV=iv)
plain = encryptor.decrypt(cipher)
plain = plain[0:-ord(plain[-1])]
return plain
#[ μνΈν κ³Όμ ] νλ¬Έμ μνΈν νλ μ½λ.
def AES256Encrypt(key, plain):
length = AES.block_size - (len(plain) % AES.block_size)
plain += chr(length)*length
iv = gen_random_iv()
encryptor = AES.new(gen_sha256_hashed_key_salt(key), AES.MODE_CBC, IV=iv)
return {'cipher': encryptor.encrypt(plain), 'iv': iv}
key = 'password123qwe'
encrypted = AES256Encrypt(key, 'Let me tell you the huge secret! The king has a donkey ear')
decrypted = AES256Decrypt(key, encrypted['iv'], encrypted['cipher'])
print decrypted
Thanks for π
'Lang > π Python' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
π Python * PCM(raw data) λΉ /리ν μλμ λ³ννκΈ° (2) | 2020.03.09 |
---|---|
β Solved * [Ubuntu] Crypto install Error and How to Install with Python (0) | 2020.03.09 |
π Python * MySQLdb λͺ¨λ μ€μΉ (0) | 2020.03.09 |
π Python * Linux(Ubuntu) νκ²½λ³μ μ½λλ‘ κ°μ Έμ€κΈ° (0) | 2020.03.09 |
π Python * socket μ°κ²° + λ¬Έμμ΄ μ μ‘ (0) | 2020.03.09 |