Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python----RSA asymmetric encryption algorithm uses

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Recently, I have been working on the continuous automated testing of the interface of the project, and I haven't updated my blog for a long time.

The project is exposed to a lot of encryption-related data, and many projects use asymmetric encryption algorithms to ensure the data security of the interaction between the front end and the server.

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

Import rsa (publickey,privatekey) = rsa,newkeys (1000) # encrypt the digital 1000 to get the public key and private key pub = publickey.save_pkcs1 () # get the public key # Save the public key to the file * filepub = open ("public.pem" 'wicked') filepub.write (pub.encode ('utf-8')) filepub.close () pri = privatekey.save_pkcs1 () # get the private key # Save the private key to the file * filepri = open (' private.pem','w+') filepri.write (pri.encode ('utf-8')) filepri.close () string = "laomomoblog" # string to be encrypted # remove the public key with open (' publick.pem') 'r') as file_pub: f_pub = file_pub.read () pubkey = rsa.PublicKey.load_pkcs1 (f_pub) # take out the private key with open ('private.pem','r') as file_pri: f_pri = file_pri.read () prikey = rsa.PrivateKey.load_pkcs1 (f_pri) # encrypted string stringcrypt = rsa.encryt (string.encode (' utf-8')) Pubkey) # use the public key to encrypt the string # decrypt de_crypt = rsa.decrypt (crypt,prikey) # decrypt with the private key # the deciphered de_crypt and string should be equal Judge assert string,de_crypt.

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report