At the time of the reptile analysis, the RSA public key in the following format is often seen on the Web page:
migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqdc7kw8r6tq43pwapyvkj5laljan9bzb21taift/ Vexbobzh7q8sudp5udpxebkzojx2l28y7xs1d9v3tdpfki2lr7pazwbmdmn8rihrddnpupjnlaguqjg9oopn8j7ynpcxca1iybolc2kehmjn5uwoanqq +ca6agnkqly2h4j6widaqab
For the RSA algorithm's public key, we learned that there are two main information: modulus (modulus) and exponent (exponent)
With these two pieces of information, we can generate the public key with the following code snippet and then encrypt the data using the RSA Library
Import= RSA. PublicKey (modulus, exponent)print key
Now all we need to do is to present the modulus and exponent from this string.
In the process of research, in addition to the form of this string, we can see the most should belong to the PUBLIC.PEM, Private.pem the file format of the public key.
So what's the format of PEM, the search down, basically what information it contains and what the data are, and how do I know that it's information, and those are data?
Compare some of the information, I think the basic clear (originally there is a very good article, seemingly can't find the link, only saved a picture)
We opened the Pem file and looked and found
-----BEGIN RSA Public KEY-----Migjaogbapvzr7eov/ Gfh77lx2sp1fdkp63mygpaukomwv9fpfuuajvio5038p3kjhl5o14+ln8nxluyitzgyksunukvqxwkwskhow8el3m6ykytk5ur+ Feg8lbqpnoxlct9a9vh2pngbnr9wwm2ycmqbppqrc3ci7ylicjwgduorgoz6pmpagmbaae=-----END RSA Public KEY-----
This section of begin and this end is very much like the string we found, we copy the string from the beginning to the middle of the Public.pem file, and then use the following code encryption to try it out:
Import Rsawith Open ('public.pem','r') as F: ='cnblogs'= rsa.encrypt (Message.encode (), PubKey) Print crypto
This paragraph seems to be an error at all, and this copy of the past format is not correct, the original is a newline,
Query it seems to be a newline is also a kind of information, and our string from where to break the line is not know
So, is there any other connection between this string and PEM?
Before there is speculation that the string may be base64 encrypted, then I will decrypt to see:
Import"migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqdc7kw8r6tq43pwapyvkj5laljan9bzb21taift /vexbobzh7q8sudp5udpxebkzojx2l28y7xs1d9v3tdpfki2lr7pazwbmdmn8rihrddnpupjnlaguqjg9oopn8j7ynpcxca1iybolc2kehmjn5uwoanqq +ca6agnkqly2h4j6widaqab"= base64.b64decode (pubkey)print b64_str print len (b64_str)
Get a bunch of garbled, we convert this string garbled into 16 binary
Find the end is "\x01\x00\x01", 10001, see more RSA public key, know this number, is mostly exponent.
Then look at the decoded length, 162, we find the offset table, found that the modulus of the offset position is 159, the length is 3, add up just 162
That means the string is the result of exponential and modulus encryption, even more simple than the information in a typical PEM file.
According to this idea, we find the exponent and modulus against the offset table:
#/usr/bin/python#Encoding:utf-8ImportBase64defStr2key (s):#Decoding the stringB_str =Base64.b64decode (s)ifLen (B_STR) < 162: returnFalse hex_str="' #Bitwise conversion to 16 binary forXinchb_str:h= Hex (ord (x)) [2:] H= H.rjust (2,'0') Hex_str+=h#find the start end position of modulo and exponentM_start = 29 * 2E_start= 159 * 2M_len= 128 * 2E_len= 3 * 2modulus= Hex_str[m_start:m_start +M_len] Exponent= Hex_str[e_start:e_start +E_len]returnmodulus,exponentif __name__=="__main__": PubKey="migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqdc7kw8r6tq43pwapyvkj5laljan9bzb21taift/ Vexbobzh7q8sudp5udpxebkzojx2l28y7xs1d9v3tdpfki2lr7pazwbmdmn8rihrddnpupjnlaguqjg9oopn8j7ynpcxca1iybolc2kehmjn5uwoanqq +ca6agnkqly2h4j6widaqab"Key=Str2key (PubKey)PrintKey
The results were as follows:
('C2EE4C3CAFAB6AE37A7002962F909E656A58DA37D0596F6D530087D3FEF7B16E86F31FB43C49474FE6E0CF5C404ACCE8F1D8BDBCCB B5ecd5df6fded74f7ca2362d1ecf033581983327f2b887ac30cda54a499e500652a246f68a0f9fc8fb60da5cc426b58b26ce95cda41219899f9bb0a1a 9d0abe080e9a80d92a972d87e23eb'010001')
Now we use the key we just got to encrypt the string:
Import RSA ' Cnblogs ' = Int (key[0], +) = Int (key[1], +) = RSA. PublicKey (modulus, exponent) = rsa.encrypt (message, rsa_pubkey) = Base64.b64encode (crypto) print b64str
You can get an RSA encryption, Base64 encoded string.
To summarize: The main is in a string of strings, against an offset table, the need to extract the number of positions.
RSA string format Public key conversion Python RSA library recognizable public key form