加密解密 - Python RSA 公钥加密结果不一致
高洛峰
高洛峰 2017-04-17 14:55:11
0
2
1403

首先使用rsa生成了公钥,然后将公钥(pubkey)n和e部分转成字符,又将字符转成公钥(pub),使用pubkey和pub进行加密后的结果(test1, test2)并不一样,求解。

>>> import rsa
>>> (pubkey, privkey) = rsa.newkeys(512, poolsize=8)
>>> pubkey.n
10818718420560739313346002978990665036149681542592492045226497373002401361706250920089394493353909096589940348075365896188466224763409423854599514284610391L
>>> pubkey.e
65537
>>> type(pubkey.e)
<type 'int'>
>>> type(pubkey.n)
<type 'long'>
>>> type(pubkey)
<class 'rsa.key.PublicKey'>
>>> n = str(pubkey.n)
>>> e = str(pubkey.e)
>>> n
'10818718420560739313346002978990665036149681542592492045226497373002401361706250920089394493353909096589940348075365896188466224763409423854599514284610391'
>>> e
'65537'
>>> message = 'test'
>>> pub = rsa.PublicKey(long(n), int(e))
>>> pub
PublicKey(10818718420560739313346002978990665036149681542592492045226497373002401361706250920089394493353909096589940348075365896188466224763409423854599514284610391, 65537)
>>> test1 =  rsa.encrypt(message, pub)
>>> test2 =  rsa.encrypt(message, pubkey)
>>> test1 == test2
False
>>> test1
"\xbbVcE\x1e\x1f\xa3\x84\x90]\x19\xbf5\xb9\x8aM\xed<\x7f\xcd\xf3UC\x87f]a\x15/\xb2\xe8\xa3\x05w\xc4Y'#\x9f\xd3\xc0}\xc81\x15F\xba\xc6\xf8\x92\xb6\x11\x1a\xe2\xc7\xfbLZo\xb0Q~\xf0\xf6"
>>> test2
'Z1\xf1\xbd\xe6}!\x11\x0c\xa2\xe2"lx\xb2\xa2\xdf\x15{\x95\xe6\x9aX\xbc)\xfb\xe4!\xf1"\xf0\xfc3y\xbb\x90\x92\x8e\x83\x0c\xbd\xc5\xf9\x0b\xdf\xdd\xd5\xbc\x0ey\x05\x055\xde\x9dh\xb0+\x0f\x8c\x88J\x98\xf1'
>>> pub == pubkey
True
>>> pub 
PublicKey(10818718420560739313346002978990665036149681542592492045226497373002401361706250920089394493353909096589940348075365896188466224763409423854599514284610391, 65537)
>>> pubkey
PublicKey(10818718420560739313346002978990665036149681542592492045226497373002401361706250920089394493353909096589940348075365896188466224763409423854599514284610391, 65537)
>>> import chardet
>>> chardet.detect(test1)
{'confidence': 0.0, 'encoding': None}
>>> type(pub)
<class 'rsa.key.PublicKey'>
>>> type(pubkey)
<class 'rsa.key.PublicKey'>
>>> chardet.detect(test2)
{'confidence': 0.0, 'encoding': None}
>>> 

解密结果是相同的

>>> rsa.decrypt(test1, privkey)
'test'
>>> rsa.decrypt(test2, privkey)
'test'
>>> 

私钥解密:

>>> n = str(privkey.n)
>>> e = str(privkey.e)
>>> d = str(privkey.d)
>>> p = str(privkey.p)
>>> q = str(privkey.q)
>>> n
'10818718420560739313346002978990665036149681542592492045226497373002401361706250920089394493353909096589940348075365896188466224763409423854599514284610391'
>>> e
'65537'
>>> d
'8903648270921220617431832654452301896482433185548143048222170107118994148084511997041768437603762728926301730747011307923395996108348985220136737777676929'
>>> p
'6711170401751480754571255050059571447514931755192869446968529815316138668726356067'
>>> q
'1612046449861751533076589670955589879512068088293380286297803374041352573'
>>> pr = rsa.PrivateKey(long(n), int(e), long(d), long(p), long(q))
>>> pr
PrivateKey(10818718420560739313346002978990665036149681542592492045226497373002401361706250920089394493353909096589940348075365896188466224763409423854599514284610391, 65537, 8903648270921220617431832654452301896482433185548143048222170107118994148084511997041768437603762728926301730747011307923395996108348985220136737777676929, 6711170401751480754571255050059571447514931755192869446968529815316138668726356067, 1612046449861751533076589670955589879512068088293380286297803374041352573)
>>> pr = privkey
>>> pr = rsa.PrivateKey(long(n), int(e), long(d), long(p), long(q))
>>> pr ==  privkey
True
>>> rsa.decrypt(test1, pr)
'test'
>>> rsa.decrypt(test2, pr)
'test'
>>> 

已采纳@依云的答案,滚回去看书了:

>>> rsa.encrypt(message, pub)
'\xcc\x11\xb5\x8dSM\xd0\x01l\r\xc1\xed]\x17U\xf9)\xbaC\xcf-\x07\xfd\xa6V\xdb.\x94\x8b\xb8\xb3M\x0cG\xa7v\xe3\x11\x9a\xa8\xffV\xefo\x92\xb8\xcd$+\x1f\x99q\x06\xa1E\xd0E\xe5\xaa\xea%\xb5\xf1\x93'
>>> rsa.encrypt(message, pub)
'0\x9a\xd7*+o\x9b\xd2\x92\t\x1bb\x9cY\xfc{\\\xa6\x98\xd3\xd0\xcd\xff\xd9\x94\xb4Pa\xeb8r(s\xfe\x17;\xe0\xbd\xfcs\xcc\xb7\xaau~\xba\n\xdb\xb2G\xb9\xd8\xe6K\x1fA\x8c\xb0{P)9\xd6\xa0'
>>> rsa.encrypt(message, pub)
'BV\xcf\xa1\x93\xb1\xe1\x91$\xbd\x01\x08T\xfc\x7f\xf1uvX\x8f"\xfd\x91\xe5*f\xbb\xec\xb2\x14\xe6ug\xc0`\xf0\xba\xf8|\xec\xad\x85\xeej~Ti*\xc5@I\\vl\xef.\x86S\xa4\xdbcTQ\xea'
>>> rsa.encrypt(message, pub)
'\xb4l0\x10\xb6\x8c\x83\x02\xdeTMC\x1fm\x19\xdb\x02\xa8\xc8\x05\xcb\xf4\xee\x919\xe34x\xaf\xa4\x98dc\xa6\t}\x14\xc4\x07\x0eU8\x08Dr\x0bo\x17\x18\x05j\x89\xc6]\xca\x16s\xd9\x92\x0bN\x95o\x9d'
>>> rsa.encrypt(message, pub)
'}.\x9f\xe5q\xee\xa3\xb91k\xb9\xb0\xa2zK%\x88\xdc\xb1\xd7i\xf3$4\x91\xa6\xd9\xd1-boS\xe0\x9b&\x0cv=\xa2\xe8\xc0\x07\x93\x80\xea\xf3\x06vN\xf8M\xe3\xa2\x0e\x16~\x85X\xab\xf2\x18\xd4\xf1\xd0'
>>> rsa.encrypt(message, pub)
"p\xd7\x82\nZz\xc6\x92\x85\xcb6<\x18\xa5\x1fa{\xef\xaa'~\x8a H\xc9\xbb\x90T\xde5\xe3O\x10\xd5Q\xf8qd\xce\x80\x06\xab\xd6\xd0\xec}\xabq\x8c?\xc7\xb0Z\xddO^\x93\xa4\xaaV\x7f\xe7\xff\xb0"
>>> rsa.encrypt(message, pub)
"Rw\xff\xb3\xf35\xe9\x80|\x90T\x03\xf4\xeb\xe2\x8fA\x84\x1cBm,\xc4\x99J^\xfc\xc5Q\ncl\t\x19\xc9W4'\xdf*\x8bN\x9c\xa8']\\\xa5D\x9b\xe1m}:\xba\x05\xb8Q\xe7\xaa\xafb\xaem"
>>> 
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
小葫芦

Correction! The following discussion applies only to AES. RSA is different and generally does not use IV. Cryptography is very complex...


Modern encryption algorithms use the same encryption key to encrypt the same plaintext, and the ciphertext obtained will be different every time to prevent attacks based on characteristics.

If the encryption result is the same every time, such as an HTML document, it is easy to guess the string that is likely to appear in it. Then if multiple HTMLs are requested during this session, the same string will appear more often.

Technically, every time you start encryption, a cryptographically secure random string is generated as an IV (initial vector), and then adjacent blocks or bytes are associated through some kind of chain.

Of course there are special needs, you can use the encryption scheme without IV.

洪涛

I disagree with Evian, this should be random filling.

Where does the asymmetric algorithm come from the grouping mode = =

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template