python rsa encryption and decryption

  • 2020-05-27 06:01:47
  • OfStack

Recently, there is a need to study the security of encryption and decryption of RSA. Baidu on the Internet 1 under the example of the article, there are few articles on how to save, transmission, printing encrypted text information, are thousands of 1 law. Directly in 1 script, the encrypted text message is assigned to a variable, and then immediately invoked for decryption. After carefully thinking about the encryption and decryption process of RSA, it is determined that there are two ends, one end is: the encryption end, the other end is the decryption end, and the first end is generally different from the same machine. In this case, I just simulated saving it in a file and then reading it out; Much the same is true of how to transmit over the Internet.

The ciphertext encrypted with RSA cannot be displayed directly in text, because there are some binary data that cannot be encoded in text information. For saving, network transmission and printing non-scrambled code, base64 coding needs to be converted; base64 codec can convert some binary data which cannot be directly encoded with the file information into regular binary data.


 #/usr/bin/env python
# -*- coding: utf-8 -*-
import rsa
import sys
import base64
#  print  python  version   with  windows  System coding 
print("---- 1 ----")
print(sys.version)
print(sys.getdefaultencoding())
print(sys.getfilesystemencoding())
#  Mr Into 1 Key, and then save .pem Format files, of course, can also be used directly 
print("---- 2 ----")
(pubkey, privkey) = rsa.newkeys(1024)
pub = pubkey.save_pkcs1()
print(type(pub))
pubfile = open('public.pem','w+')
pubfile.write(pub.decode('utf-8'))
pubfile.close()
print("---- 3 ----")
pri = privkey.save_pkcs1()
print(type(pri))
prifile = open('private.pem','w+')
prifile.write(pri.decode('utf-8'))
prifile.close()
# load Public keys and keys 
print("---- 4 ----")
message = 'dPabdbGDpFTrwwgydVafdlsadlfsal%46645645s'
print('message:',type(message))
with open('public.pem') as publickfile:
 p = publickfile.read()
 print(type(p))
 pubkey = rsa.PublicKey.load_pkcs1(p.encode('utf-8'))
with open('private.pem') as privatefile:
 p = privatefile.read()
 print(type(p))
 privkey = rsa.PrivateKey.load_pkcs1(p.encode('utf-8'))
#  Encrypt with the public key and decrypt with the private key 
crypto = rsa.encrypt(message.encode('utf-8'),pubkey)
print(crypto)
print("---- 5 ----")
print('crypto:',type(crypto))
print('cry_base64:',base64.encodestring(crypto))
print('cry_base64_utf8:',base64.encodestring(crypto).decode('utf-8'))
#  Save to a local file 
cry_file = open('cry_file.txt','w+')
cry_file.write(base64.encodestring(crypto).decode('utf-8'))
cry_file.close()
print("---- 6 ----")
#  Read from a local file 
cry_file = open('cry_file.txt','r')
cry_text = ''
for i in cry_file.readlines():
 cry_text += i
print('cry_text_type:',type(cry_text))
print('cry_text:',cry_text)
print('cry_base64:',cry_text.encode('utf-8'))
crypto_tra = base64.decodestring(cry_text.encode('utf-8'))
print("---- 7 ----")
assert crypto == crypto_tra
print(crypto)
print("---- 8 ----")
plaintext = rsa.decrypt(crypto,privkey)
assert message == plaintext.decode('utf-8')
print(plaintext.decode('utf-8'))

Related articles: