Python對數據加密與解密

1、base64模塊

>>>import base64

>>>s1 = base64.encodestring('Hello world') #加密過程

>>>print s1

>>>SGVsbG8gd29ybGQ=


>>>s2 = base64.decodestring(s1) #解決過程

>>>print s2

>>>Hello world


2、Crypto模塊

>>> import Crypto
>>> from Crypto.Cipher import AES
>>> obj = AES.new('This is a key123',AES.MODE_CBC,'This is an IV456')
>>> message = 'The answer is no'
>>> ciphertext = obj.encrypt(message)
>>> ciphertext
'\xd6\x83\x8dd!VT\x92\xaa`A\x05\xe0\x9b\x8b\xf1'


>>> obj2 = AES.new('This is a key123',AES.MODE_CBC,'This is an IV456')
>>> obj2.decrypt(ciphertext)
'The answer is no'
>>>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章