RSA encryption/decryption, signature/authentication under Python

Source: Internet
Author: User
Tags decrypt

The original is the PY2 environment, and my environment is py3, so the original code has been modified: decode (), encode ()

ImportRSA#Generate key(PubKey, Privkey) = Rsa.newkeys (1024)#Save KeyWith open ('Public.pem','w+') as F:f.write (PUBKEY.SAVE_PKCS1 (). Decode ()) with open ('Private.pem','w+') as F:f.write (PRIVKEY.SAVE_PKCS1 (). Decode ())#Import KeyWith open ('Public.pem','R') as F:pubkey=RSA. PUBLICKEY.LOAD_PKCS1 (F.read (). Encode ()) with open ('Private.pem','R') as F:privkey=RSA. PRIVATEKEY.LOAD_PKCS1 (F.read (). Encode ())#plaintextMessage ='Hello'#Public Key CryptographyCrypto =Rsa.encrypt (Message.encode (), PubKey)#private Key DecryptionMessage =Rsa.decrypt (crypto, Privkey). Decode ()Print(message)#private key SignatureSignature = Rsa.sign (Message.encode (), Privkey,'SHA-1')#Public Key ValidationRsa.verify (Message.encode (), signature, PubKey)
Application Scenarios
ImportRSA#Generate key(PubKey, Privkey) = Rsa.newkeys (1024)# =================================#Scenario 0: Key Save import# =================================#Save KeyWith open ('Public.pem','w+') as F:f.write (PUBKEY.SAVE_PKCS1 (). Decode ()) with open ('Private.pem','w+') as F:f.write (PRIVKEY.SAVE_PKCS1 (). Decode ())#Import KeyWith open ('Public.pem','R') as F:pubkey=RSA. PUBLICKEY.LOAD_PKCS1 (F.read (). Encode ()) with open ('Private.pem','R') as F:privkey=RSA. PRIVATEKEY.LOAD_PKCS1 (F.read (). Encode ())# =================================#Scenario One: Data disclosure issues#in order to open up the market, the company manager assigned a group of sales agents around the world to investigate business opportunities. #The salesmen were all very capable, and soon they found a good business opportunity. #Time is Money! They must report to the manager immediately by email. #This is the trouble: The network is not safe! #all kinds of data are caught, email password leaked ... It's horrible! The various means of business rivals are horrible! #How to let the salesman's email safely to the company manager's hands? (even if the data is caught, the mailbox password is compromised ...)#It's not safe, so what? #  #That's right! Smart you must have thought: encryption. # =================================#Clear text: Business opportunity found by salesmanMessage ='here is the opportunity: ...'#The clerk uses the public key of the company manager to encrypt the plaintext in advance, and get ciphertextCrypto_email_text =Rsa.encrypt (Message.encode (), PubKey)#then, the clerk sends the cipher by email#..... #e-mail in the network transmission ... (All kinds of data are caught, email password leaked)#There is no way, or be a conscientious to see this email:Print(Crypto_email_text)#What the hell? Do not understand Ah! #Finally, the company manager also received the salesman sent an email. Open, and only see a bunch of strange characters! #no problem, the company manager uses his private key to decrypt the ciphertext received, it can get clear textMessage =Rsa.decrypt (Crypto_email_text, Privkey). Decode ()#then, you can see the important opportunity information.Print(message)# =================================#Scenario Two: Identity verification issues#in order to open up the market, the company manager assigned a group of salesmen to explore business opportunities. #In this process, the company manager often through email to the salesman issued important instructions#However, the network is not safe! For example: Packet is modified, mailbox password leaked ...#Commercial competitors can forge/modify the important instructions of the company manager through various means! # #that morning, the salesman opened the mailbox as usual, found the company manager of an email: ordered him to return home immediately. #No, it's not. Yesterday said to expand business here, how today is changed? #is this email sent by the manager of the company? #What to do? # #That's right! Smart You must also think of: signature. # =================================#Clear Text: instructions from the company managerMessage ='This is an important instruction: ...'#Company manager private key SignatureCrypto_email_text = Rsa.sign (Message.encode (), Privkey,'SHA-1')#clerk at the same time received instructions plaintext, ciphertext, and then use the public key authentication, to identifyRsa.verify (Message.encode (), Crypto_email_text, PubKey)

Disclaimer: Original Blog link address: http://www.cnblogs.com/hhh5460/p/5243410.html

RSA encryption/decryption, signature/authentication under Python

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.