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

How to use rsa encryption algorithm in python3

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge about how to use the rsa encryption algorithm in python3. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article.

Foreword:

Rsa encryption is an encryption algorithm. At present, the encryption algorithm is to encrypt data, passwords and so on. The first contact with the rsa encryption algorithm is the secret-free login setting in linux. At that time, I always thought that key encryption was the same as token, but it was not found until now that it was a way of data encryption. In fact, it is also understandable that under the understanding of Linux design philosophy: "everything is a file", they can be understood as similar things. All you have to do is compare the a string with the b string, and if you return trun, you can proceed to the next step, otherwise an exception will be returned.

Here, in fact, there is a logic, that is, first encrypt the data, and then transmit the data. after receiving the data, we find that the data is garbled, or encrypted data, so if you want to see the actual data, you need to decrypt the data.

Therefore, in the conventional encryption process, we can already obtain the data in many interfaces, but the obtained data is garbled. Generally, if you want to continue to increase security, you can add signatures when obtaining data, that is, when returning data to the service of the access interface, verify it, and reject him when he sends a request to receive data.

In other words, the current interface encryption actually starts from two points:

When receiving a data request (before the data is returned)

Second, the data itself is encrypted (after the data is returned) (return the data if requested, but I bet he won't interpret it even if he gets the data. Dog head)

For now, all the encryption schemes I can think of are to encrypt strings or identifiers, that is, events that happen before the data is returned. I don't know if other bosses have any other coquettish operations.

Technical test implementation:

Here, the rsa package is used directly, and the python third-party library implements encryption with a pycryptodome, and both packages can be implemented. Use rsa here.

The code logic is simple, and several tests have been done:

First, generate the key directly, then encrypt and decrypt it.

Second, Mr. into a key, and then write the key to the file to retain, and then read it out for use

Third, generate the key directly, then write the key into a string, put it in the code, and use it directly.

The above three, in fact, logically speaking, should be supported, if not, then there should be something wrong with the format somewhere in the process. Regular processing is required.

In the process of use, there is a place to pay special attention, that is, the string that needs to be encrypted needs to be encoding, and the decryption needs to be decode. Don't forget that. Otherwise, you will find that the encryption will make an error.

Import rsadef secorty_data (): (pub_key,sec_key) = rsa.newkeys (1024) pub = pub_key.save_pkcs1 () file = open ('. / pub.pem','wb+') file.write (pub) sec = sec_key.save_pkcs1 () files = open ('. / sec.pem','wb+') files.write (sec) def create_rsa (): file = open ('. / pub.pem') 'rb') content = file.read () print (content) file1 = open ('. / sec.pem','rb') content1 = file1.read () print (content1) pub_key = rsa.PublicKey.load_pkcs1 (content) sec_key = rsa.PrivateKey.load_pkcs1 (content1) message ='it's a little hot today But the whole is still very good 'data = rsa.encrypt (message.encode (), pub_key) print (data) result = rsa.decrypt (data,sec_key) print (result.decode ()) if _ name__ = =' _ main__': secorty_data () create_rsa () above is all the content of this article entitled "how to use rsa encryption algorithm in python3". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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

Development

Wechat

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

12
Report