use*_*151 13 python openssl public-key-encryption private-key
我希望通过公钥加密秘密文本,并通过Python中的私钥解密.我可以通过openssl命令实现:
echo "secrettext/2011/09/14 22:57:23" | openssl rsautl -encrypt -pubin -inkey public.pem | base64 data.cry
base64 -D data.cry | openssl rsautl -decrypt -inkey private.pem
Run Code Online (Sandbox Code Playgroud)
如何在Python中实现它?
jfs*_*jfs 20
#!/usr/bin/env python
import fileinput
from M2Crypto import RSA
rsa = RSA.load_pub_key("public.pem")
ctxt = rsa.public_encrypt(fileinput.input().read(), RSA.pkcs1_oaep_padding)
print ctxt.encode('base64')
Run Code Online (Sandbox Code Playgroud)
#!/usr/bin/env python
import fileinput
from M2Crypto import RSA
priv = RSA.load_key("private.pem")
ctxt = fileinput.input().read().decode('base64')
print priv.private_decrypt(ctxt, RSA.pkcs1_oaep_padding)
Run Code Online (Sandbox Code Playgroud)
另请参见如何使用密钥加密字符串以及在python中使用public-key编码字符串的最佳方法是什么.
| 归档时间: |
|
| 查看次数: |
11888 次 |
| 最近记录: |