Python rsa encryption/decryption (codec, base64 codec), rsabase64

Source: Internet
Author: User

Python rsa encryption/decryption (codec, base64 codec), rsabase64

Recently, there is a need to study the security of RSA encryption and decryption. Baidu has an example article on the Internet, and few articles have introduced how to save, transmit, and print encrypted text information. Directly in a script, the encrypted text information is assigned to the variable, and then the decryption is immediately called. After carefully thinking about the RSA encryption and decryption process, make sure there are two ends, one end is: the encryption end, and the other end is the decryption end, which is generally not on the same machine. Here, I only simulate saving in the file and then read it out. The same is true for how to transmit data over the network.

 

 

1 #/usr/bin/env python 2 #-*-coding: UTF-8-*-3 import rsa 4 import sys 5 import base64 6 7 # print python and windows 8 print ("---- 1 ----") 9 print (sys. version) 10 print (sys. getdefaultencoding () 11 print (sys. getfilesystemencoding () 12 13 # use a pair of keys and save them. pem format file, of course, you can also directly use 14 print ("---- 2 ----") 15 (pubkey, privkey) = rsa. newkeys (1024) 16 pub = pubkey. save_pkcs1 () 17 print (type (pub) 18 pubfile = open ('Public. pem', 'W + ') 19 pubfile. write (pub. decode ('utf-8') 20 pubfile. close () 21 22 print ("---- 3 ----") 23 pri = privkey. save_pkcs1 () 24 print (type (pri) 25 prifile = open ('private. pem', 'W + ') 26 prifile. write (pri. decode ('utf-8') 27 prifile. close () 28 29 # load public key and key 30 print ("---- 4 ----") 31 message = 'dpabdbgdpftrwwgydvdlsadlfsal % 46645645s '32 print ('message:', type (message )) 33 with open ('Public. pem ') as publickfile: 34 p = publickfile. read () 35 print (type (p) 36 pubkey = rsa. publicKey. load_pkcs1 (p. encode ('utf-8') 37 with open ('private. pem ') as privatefile: 38 p = privatefile. read () 39 print (type (p) 40 privkey = rsa. privateKey. load_pkcs1 (p. encode ('utf-8') 41 42 # use the public key to encrypt and then use the private key to decrypt 43 crypto = rsa. encrypt (message. encode ('utf-8'), pubkey) 44 print (crypto) 45 46 print ("---- 5 ----") 47 print ('crypto: ', type (crypto )) 48 print ('Cry _ base64: ', base64.encodestring (crypto) 49 print ('Cry _ base64_utf8:', base64.encodestring (crypto ). decode ('utf-8') 50 # Save to local file 51 cry_file = open('cry_file.txt ', 'W +') 52 cry_file.write (base64.encodestring (crypto ). decode ('utf-8') 53 cry_file.close () 54 55 print ("---- 6 ----") 56 # Read 57 cry_file = open('cry_file.txt ', 'R ') 58 cry_text = ''59 for I in cry_file.readlines (): 60 cry_text + = i61 62 print ('Cry _ text_type: ', type (cry_text )) 63 print ('Cry _ text: ', cry_text) 64 print ('Cry _ base64:', cry_text.encode ('utf-8 ')) 65 crypto_tra = base64.decodestring (cry_text.encode ('utf-8') 66 67 print ("---- 7 ----") 68 assert crypto = crypto_tra69 print (crypto) 70 71 print ("---- 8 ----") 72 plaintext = rsa. decrypt (crypto, privkey) 73 assert message = plaintext. decode ('utf-8') 74 print (plaintext. decode ('utf-8 '))

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.