Python rsa encryption and decryption, pythonrsa

Source: Internet
Author: User

Python rsa encryption and decryption, pythonrsa

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.

The ciphertext encrypted with RSA cannot be directly displayed in text, because there are some binary data that cannot be encoded and displayed with text information. For storage, network transmission, and printing without garbled characters, base64 encoding must be used for conversion. base64 encoding/decoding can convert binary data that cannot be directly encoded using the current file information to conventional binary data.

#/Usr/bin/env python #-*-coding: UTF-8-*-import rsaimport sysimport base64 # print the python version and the windows system encoding print ("---- 1 ----") print (sys. version) print (sys. getdefaultencoding () print (sys. getfilesystemencoding () # use a pair of keys and save them. pem format file, of course, you can also directly use 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 key and key 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') # use the public key to encrypt and then use the private key to decrypt 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 it to the local file cry_file = open('cry_file.txt ', 'W +') cry_file.write (base64.encodestring (crypto ). decode ('utf-8') cry_file.close () print ("---- 6 ----") # Read cry_file = open('cry_file.txt ', 'R ') cry_text = ''for I in cry_file.readlines (): cry_text + = iprint ('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_traprint (crypto) print ("---- 8 ----") plaintext = rsa. decrypt (crypto, privkey) assert message = plaintext. decode ('utf-8') print (plaintext. decode ('utf-8 '))

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

Related Article

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.