Python----RSA Asymmetric encryption algorithm using

Source: Internet
Author: User
Tags decrypt asymmetric encryption

Recently in the project's interface continuous automation testing, long time no update blog.

The project is exposed to many encryption-related data, and many projects use asymmetric encryption algorithms to ensure data security for both front-end and server interactions.

Here's how to use the RSA encryption algorithm under Python:

import rsa (publickey,privatekey)=rsa,newkeys(1000)#对数字1000加密得到公钥和私钥pub = publickey.save_pkcs1()#获取公钥#将公钥保存到文件*************filepub = open("public.pem",‘w+‘)filepub.write(pub.encode(‘utf-8‘))filepub.close()pri = privatekey.save_pkcs1()#获取私钥#将私钥保存到文件***********filepri = open(‘private.pem‘,‘w+‘)filepri.write(pri.encode(‘utf-8‘))filepri.close()string = "laomomoblog"#待加密的字符串#取出公钥with open(‘publick.pem‘,‘r‘) as file_pub:     f_pub = file_pub.read()     pubkey = rsa.PublicKey.load_pkcs1(f_pub)#取出私钥with open(‘private.pem‘,‘r‘) as file_pri:     f_pri =file_pri.read()     prikey = rsa.PrivateKey.load_pkcs1(f_pri)#加密字符串stringcrypt = rsa.encryt(string.encode(‘utf-8‘),pubkey)#使用公钥去加密字符串#解密de_crypt = rsa.decrypt(crypt,prikey)#用私钥去解密#解出来的de_crypt与string应该是相等的,判断一下assert string,de_crypt

This should be more clear how to use, how to encrypt, how to decrypt.

Python----RSA Asymmetric encryption algorithm using

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.