免费注册 查看新帖 |

Chinaunix

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

加密模块的使用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-02-21 12:07 |只看该作者 |倒序浏览
python 的加密模块有5种,即hmac, md5, sha, mpz, rotor
前3种用法几乎一样,主要的函数是
new(msg) //创建一个加密的object
digest() //Return the digest of the strings passed to the update() method so far
hexdigest() //以hex码显示加密结果
update(msg) //m.update(a),m.update(b)结果为m.update(a+b)
copy()  //新建一个object,加密的串和copy的那个一样

eg:
import hmac
import md5
import sha
h = hmac.new ('python')
m = md5.new ('python')
s = sha.new ('python')
print h.hexdigest()
print m.hexdigest()
print s.hexdigest()

mpz: Deprecated since release 2.2. See the references at the end of this section for information about packages which provide similar functionality. This module will be removed in Python 2.3.
reference from book
因此就不记下它的用法了

rotor: Deprecated since release 2.3. The encryption algorithm is insecure. 好像也不太可靠
主要函数如下:
newrotor(
key[, numrotors])
Return a rotor object. key is a string containing the encryption key for the object; it can contain arbitrary binary data but not null bytes. The key will be used to randomly generate the rotor permutations and their initial positions. numrotors is the number of rotor permutations in the returned object; if it is omitted, a default value of 6 will be used.
Rotor objects have the following methods:
setkey(
key)
Sets the rotor's key to key. The key should not contain null bytes.
encrypt(
plaintext)
Reset the rotor object to its initial state and encrypt plaintext, returning a string containing the ciphertext. The ciphertext is always the same length as the original plaintext.
encryptmore(
plaintext)
Encrypt plaintext without resetting the rotor object, and return a string containing the ciphertext.
decrypt(
ciphertext)
Reset the rotor object to its initial state and decrypt ciphertext, returning a string containing the plaintext. The plaintext string will always be the same length as the ciphertext.
decryptmore(
ciphertext)
Decrypt ciphertext without resetting the rotor object, and return a string containing the plaintext.
An example usage:
>>> import rotor
>>> rt = rotor.newrotor('key', 12)
>>> rt.encrypt('bar')
'\xab4\xf3'
>>> rt.encryptmore('bar')
'\xef\xfd$'
>>> rt.encrypt('bar')
'\xab4\xf3'
>>> rt.decrypt('\xab4\xf3')
'bar'
>>> rt.decryptmore('\xef\xfd$')
'bar'
>>> rt.decrypt('\xef\xfd$')
'l(\xcd'
>>> del rt



本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/13878/showart_76923.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP