求大神帮看一下,如何根据下面的程序,把明文生成密文谢谢
求大神帮看一下,如何根据下面的程序,把明文生成密文谢谢
def parserLicense(self,encCode):
data = base64.b64decode(encCode)
cert = X509.load_cert(I2_CRT)
puk = cert.get_pubkey().get_rsa()
self.code = puk.public_decrypt(data, RSA.pkcs1_padding)
.crt文件如下:
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 1 (0x1)
Signature Algorithm: sha1WithRSAEncryption
Issuer: C=CN, ST=Shanghai, L=Newbury, O=Info2soft, OU=Development/emailAddress=dev@info2soft.com
Validity
Not Before: May 22 00:59:30 2012 GMT
Not After : May 20 00:59:30 2022 GMT
Subject: C=CN, ST=Shanghai, O=Info2soft, OU=Development, CN=I2Soft/emailAddress=dev@info2soft.com
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
RSA Public Key: (2048 bit)
Modulus (2048 bit):
00:cb:63:b8:43:d6:e6:48:ec:eb:35:d5:cd:d8:c1:
d3:47:a9:9a:52:3c:cf:b9:14:87:d3:b3:49:6a:9d:
12:8d:9f:8c:64:6e:32:c5:25:fb:e9:5b:92:56:8f:
5c:7b:f7:f6:d4:3c:fc:ba:dc:4b:3e:16:72:5f:09:
e1:46:be:b7:cb:1e:05:70:36:9b:8f:b4:d2:99:1c:
94:7e:35:da:92:88:94:c3:c1:73:38:58:c3:e3:4a:
cf:ee:8d:7b:a7:ae:ac:d9:27:82:b9:96:ac:72:b0:
e8:1d:d4:3a:17:74:aa:38:81:88:fc:ea:60:7f:ff:
4a:fb:37:e3:5c:35:9d:f1:10:37:76:dd:19:bd:f6:
07:ee:52:30:6b:ae:c5:43:9e:9a:2e:d9:a1:9a:e6:
57:f4:23:5e:a4:8d:ab:c0:d4:0f:4f:2b:43:64:8d:
92:a1:2b:83:f9:cb:56:86:13:c0:6c:c0:9e:1a:7d:
3c:5b:ab:ea:c4:4c:03:0f:61:9d:fb:df:a0:7a:8a:
ed:44:5e:7e:1e:5f:71:75:1d:23:08:88:97:05:98:
13:d4:f0:b7:a7:03:c3:93:08:b6:35:1a:bc:76:45:
37:c0:42:a0:6b:da:5f:c4:bb:f9:7a:68:e1:ab:5a:
de:5b:53:12:d4:d8:61:5e:8e:82:3c:43:8d:46:b8:
47:d5
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
CF:9C:30:5F:9F:A9:83:A1:EA:FB:EA:39:A6:22:3E:AE:20:0F:30:FF
X509v3 Authority Key Identifier:
keyid:54:64:DF:A2:32:5F:1D:9B:CD:DE:79:8D:73:C0:0A:2C:CB:D5:5D:51
Signature Algorithm: sha1WithRSAEncryption
2a:b4:4b:f4:ec:14:ad:17:b5:48:bc:c2:b3:b5:3e:3b:d0:ba:
d5:cd:7d:07:76:04:75:37:8e:06:70:65:d5:1a:b0:00:f6:ee:
34:5f:d6:dd:b1:3e:b7:cd:bc:fb:83:c9:ad:e6:59:0d:60:15:
b2:c7:3e:9c:26:5f:10:58:f0:59:bd:40:c0:08:5f:0e:b0:7a:
49:fc:b4:67:c4:78:31:b5:48:2e:10:28:b6:65:b5:11:99:ac:
2c:a3:93:fd:b2:2a:a8:37:9f:ff:ee:c6:d7:95:c9:be:c2:5e:
24:e0:f5:c5:4b:05:7a:17:f5:4e:c9:bd:93:7a:41:8f:17:7a:
95:46
-----BEGIN CERTIFICATE-----
MIIDcDCCAtmgAwIBAgIBATANBgkqhkiG9w0BAQUFADB+MQswCQYDVQQGEwJDTjER
MA8GA1UECBMIU2hhbmdoYWkxEDAOBgNVBAcTB05ld2J1cnkxEjAQBgNVBAoTCUlu
Zm8yc29mdDEUMBIGA1UECxMLRGV2ZWxvcG1lbnQxIDAeBgkqhkiG9w0BCQEWEWRl
dkBpbmZvMnNvZnQuY29tMB4XDTEyMDUyMjAwNTkzMFoXDTIyMDUyMDAwNTkzMFow
fTELMAkGA1UEBhMCQ04xETAPBgNVBAgTCFNoYW5naGFpMRIwEAYDVQQKEwlJbmZv
MnNvZnQxFDASBgNVBAsTC0RldmVsb3BtZW50MQ8wDQYDVQQDEwZJMlNvZnQxIDAe
BgkqhkiG9w0BCQEWEWRldkBpbmZvMnNvZnQuY29tMIIBIjANBgkqhkiG9w0BAQEF
AAOCAQ8AMIIBCgKCAQEAy2O4Q9bmSOzrNdXN2MHTR6maUjzPuRSH07NJap0SjZ+M
ZG4yxSX76VuSVo9ce/f21Dz8utxLPhZyXwnhRr63yx4FcDabj7TSmRyUfjXakoiU
w8FzOFjD40rP7o17p66s2SeCuZascrDoHdQ6F3SqOIGI/Opgf/9K+zfjXDWd8RA3
dt0ZvfYH7lIwa67FQ56aLtmhmuZX9CNepI2rwNQPTytDZI2SoSuD+ctWhhPAbMCe
Gn08W6vqxEwDD2Gd+9+geortRF5+Hl9xdR0jCIiXBZgT1PC3pwPDkwi2NRq8dkU3
wEKga9pfxLv5emjhq1reW1MS1NhhXo6CPEONRrhH1QIDAQABo3sweTAJBgNVHRME
AjAAMCwGCWCGSAGG+EIBDQQfFh1PcGVuU1NMIEdlbmVyYXRlZCBDZXJ0aWZpY2F0
ZTAdBgNVHQ4EFgQUz5wwX5+pg6Hq++o5piI+riAPMP8wHwYDVR0jBBgwFoAUVGTf
ojJfHZvN3nmNc8AKLMvVXVEwDQYJKoZIhvcNAQEFBQADgYEAKrRL9OwUrRe1SLzC
s7U+O9C61c19B3YEdTeOBnBl1RqwAPbuNF/W3bE+t828+4PJreZZDWAVssc+nCZf
EFjwWb1AwAhfDrB6Sfy0Z8R4MbVILhAotmW1EZmsLKOT/bIqqDef/+7G15XJvsJe
JOD1xUsFehf1Tsm9k3pBjxd6lUY=
-----END CERTIFICATE-----
我试了一下应该是第二种,但是运行报错,好像RSA.load_key("/root/Desktop/i2.crt")这里的问题,这个参数是crt文件吗?
[root@localhost Desktop]# ./lic2.py
Traceback (most recent call last):
File "./lic2.py", line 10, in <module>
lic(xx)
File "./lic2.py", line 6, in lic
ReadRSA = RSA.load_key("/root/Desktop/i2.crt")
File "/usr/lib64/python2.6/site-packages/M2Crypto/RSA.py", line 351, in load_key
return load_key_bio(bio, callback)
File "/usr/lib64/python2.6/site-packages/M2Crypto/RSA.py", line 372, in load_key_bio
rsa_error()
File "/usr/lib64/python2.6/site-packages/M2Crypto/RSA.py", line 302, in rsa_error
raise RSAError, m2.err_reason_error_string(m2.err_get_error())
M2Crypto.RSA.RSAError: no start line
我的程序是这样写的:
#!/usr/bin/python
import base64
from M2Crypto import X509, RSA
xx="619CF5B817CA79F1951B6FAE396D906E"
def lic(str):
ReadRSA = RSA.load_key("/root/Desktop/i2.crt")
encrypted = ReadRSA.private_encrypt(str,RSA.pkcs1_padding)
final = base64.b64encode(encrypted)
print final
lic(xx)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你没说清楚到底要用什么加密,有两种可能
用证书里面的公钥加密
用证书对应的私钥加密