1.
1.
最简单的方法是用
base64:
2.
3.
import base64
4.
5.
s1 = base64.encodestring('hello world')
6.
s2 = base64.decodestring(s1)
7.
print s1,s2
8.
9.
# aGVsbG8gd29ybGQ=\n
10.
# hello world
11.
12.
注
:
这是最简单的方法了,
但是不够保险,
因为如果别人拿到你的密文,
也可以自己解密来得到明文;
不过可以把密文字符串进行处理,
如字母转换成数字或是特殊字符等,
自己解密的时候在替换回去在
进行
base64.decodestring
,这样要安全很多。
13.
14.
15.
16.
17.2.
第二种方法是使用
win32com.client
18.
19.import win32com.client
20.def encrypt(key,content):
# key:
密钥
,content:
明文
21. EncryptedData = win32com.client.Dispatch('CAPICOM.EncryptedData')
22. EncryptedData.Algorithm.KeyLength = 5
23. EncryptedData.Algorithm.Name = 2
24. EncryptedData.SetSecret(key)
25. EncryptedData.Content = content
26. return EncryptedData.Encrypt()
27.
28.def decrypt(key,content):
# key:
密钥
,content:
密文
29. EncryptedData = win32com.client.Dispatch('CAPICOM.EncryptedData')
30. EncryptedData.Algorithm.KeyLength = 5
31. EncryptedData.Algorithm.Name = 2
32. EncryptedData.SetSecret(key)
33. EncryptedData.Decrypt(content)
34. str = EncryptedData.Content