본문 바로가기

Python

Python AES

key는 16바이트 단위로 해야하며,

평문도 16바이트 크기로 '{'로 패딩된다.


from Crypto.Cipher import AES

key="a"*16

#key = b'\xbf\xc0\x85)\x10nc\x94\x02)j\xdf\xcb\xc4\x94\x9d(\x9e[EX\xc8\xd5\xbfI{\xa2$\x05(\xd5\x32'

message="keyiskey"*3

print (len(key))

#key = b'\xbf\xc0\x85)\x10nc\x94\x02)j\xdf\xcb\xc4\x94\x9d(\x9e[EX\xc8\xd5\xbfI{\xa2$\x05(\xd5\x18'

#message = "this is my super secret message"

cipher =AES.new(key)

def pad(s):

    return s + ((16-len(s) %16)*'{')


def encrypt(plaintext):

    global cipher, key

    print (pad(plaintext))

    return cipher.encrypt(pad(plaintext))


def decrypt(ciphertext):

    global cipher

    dec = cipher.decrypt(ciphertext).decode('utf-8')

    l = dec.count('{')

    return dec[:len(dec)-l]


print ("Message:",message)

encrypted = encrypt(message)

decrypted = decrypt(encrypted)

print("Encrypted:",encrypted)

print("Decrypted:",decrypted) 


'Python' 카테고리의 다른 글

web6  (0) 2014.09.21
Pip 설치  (0) 2014.09.18
Python ksg_code.py  (0) 2014.09.18
Python get, post 값 전달하기  (0) 2014.09.14
Python 할 일 추천기  (0) 2014.09.14