免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3828 | 回复: 1
打印 上一主题 下一主题

[Python] 字符串加密解密 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-11-15 13:14 |只看该作者 |倒序浏览
1. 最简单的方法是用base64:
  1. import base64

  2. s1 = base64.encodestring('hello world')
  3. s2 = base64.decodestring(s1)
  4. print s1,s2

  5. # aGVsbG8gd29ybGQ=\n
  6. # hello world
复制代码
Note: 这是最简单的方法了,但是不够保险,因为如果别人拿到你的密文,也可以自己解密来得到明文

2. 第二种方法是使用win32com.client
  1. import win32com.client
  2. def encrypt(key,content): # key:密钥,content:明文
  3.     EncryptedData = win32com.client.Dispatch('CAPICOM.EncryptedData')
  4.     EncryptedData.Algorithm.KeyLength = 5
  5.     EncryptedData.Algorithm.Name = 2
  6.     EncryptedData.SetSecret(key)
  7.     EncryptedData.Content = content
  8.     return EncryptedData.Encrypt()

  9. def decrypt(key,content): # key:密钥,content:密文
  10.     EncryptedData = win32com.client.Dispatch('CAPICOM.EncryptedData')
  11.     EncryptedData.Algorithm.KeyLength = 5
  12.     EncryptedData.Algorithm.Name = 2
  13.     EncryptedData.SetSecret(key)
  14.     EncryptedData.Decrypt(content)
  15.     str = EncryptedData.Content
  16.     return str

  17. s1 = encrypt('lovebread', 'hello world')
  18. s2 = decrypt('lovebread', s1)
  19. print s1,s2

  20. # MGEGCSsGAQQBgjdYA6BUMFIGCisGAQQBgjdYAwGgRDBCAgMCAAECAmYBAgFABAgq
  21. # GpllWj9cswQQh/fnBUZ6ijwKDTH9DLZmBgQYmfaZ3VFyS/lq391oDtjlcRFGnXpx
  22. # lG7o
  23. # hello world
复制代码
Note: 这种方法也很方便,而且可以设置自己的密钥,比第一种方法更加安全,是加密解密的首选之策!


3. 还有就是自己写加密解密算法,比如:
  1. def encrypt(key, s):
  2.     b = bytearray(str(s).encode("gbk"))
  3.     n = len(b) # 求出 b 的字节数
  4.     c = bytearray(n*2)
  5.     j = 0
  6.     for i in range(0, n):
  7.         b1 = b[i]
  8.         b2 = b1 ^ key # b1 = b2^ key
  9.         c1 = b2 % 16
  10.         c2 = b2 // 16 # b2 = c2*16 + c1
  11.         c1 = c1 + 65
  12.         c2 = c2 + 65 # c1,c2都是0~15之间的数,加上65就变成了A-P 的字符的编码
  13.         c[j] = c1
  14.         c[j+1] = c2
  15.         j = j+2
  16.     return c.decode("gbk")

  17. def decrypt(key, s):
  18.     c = bytearray(str(s).encode("gbk"))
  19.     n = len(c) # 计算 b 的字节数
  20.     if n % 2 != 0 :
  21.         return ""
  22.     n = n // 2
  23.     b = bytearray(n)
  24.     j = 0
  25.     for i in range(0, n):
  26.         c1 = c[j]
  27.         c2 = c[j+1]
  28.         j = j+2
  29.         c1 = c1 - 65
  30.         c2 = c2 - 65
  31.         b2 = c2*16 + c1
  32.         b1 = b2^ key
  33.         b[i]= b1
  34.     try:
  35.         return b.decode("gbk")
  36.     except:
  37.         return "failed"

  38. key = 15
  39. s1 = encrypt(key, 'hello world')
  40. s2 = decrypt(key, s1)
  41. print s1,'\n',s2

  42. # HGKGDGDGAGPCIHAGNHDGLG
  43. # hello world
复制代码
Note: 这是网上抄来的一个简单的例子,还有许许多多复杂的加密算法,大家可以自行查阅密码学的相关算法。

论坛徽章:
0
2 [报告]
发表于 2012-12-05 11:35 |只看该作者
如何将python的值传到shell中呢,如上面打出来的hello world ,不能在python 通过打印方式保存到一个文件中,再来取
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP