对明文进行Hash加密,获得密文,可是密文不能解密为明文。
例如:Md5 sha1等html
使用密钥,对明文进行加密,获得密文
使用密钥,对密文进行解密,获得明文
例如 AES算法
有密钥和公钥。
公钥是全部人均可以看到的。
密钥只有本身拥有。
使用公钥,对明文进行加密,获得密文。
使用密钥,对密文进行解密,获得明文。加密
例如RSAcode
安装opensslhtm
yum install openssl生成私钥blog
openssl genrsa -out private.pem 1024
- genrsa 表示使用rsa算法
生成公钥ssl
openssl rsa -in private.pem -pubout -out public.pemget
1.公钥加密,私钥解密openssl
from M2Crypto import RSA msg = 'aaaa-aaaa' rsa_pub = RSA.load_pub_key('public.pem') rsa_pri = RSA.load_key('private.pem') print '公钥加密' en_msg=rsa_pub.public_encrypt(msg,RSA.pkcs1_padding) en_msg64=en_msg.encode('base64') print '密文base64',en_msg64 print '私钥解密' de_msg=rsa_pri.private_decrypt(en_msg,RSA.pkcs1_padding) print '明文',de_msg
这个用于加密传输的数据。例如A用公钥加密信息,传送给B,只要B有私钥,才能解密。hash
2.私钥加密,公钥解密
print '###################################' print '私钥加密' en_msg=rsa_pri.private_encrypt(msg, RSA.pkcs1_padding) en_msg64=en_msg.encode('base64') print '密文base64',en_msg64 print '公钥解密' de_msg=rsa_pub.public_decrypt(en_msg,RSA.pkcs1_padding) print '明文',de_msg
这个用于生成数字签名。也就是证实数据是私钥拥有者发送的,并且未被修改。
例如
因为全部人都有公钥,全部人都能解密,因此若是这个方法不能用来加密数据。
未经容许,请不要转发