Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Python----RSA asymmetric encryption algorithm uses

Shulou Source: shulou.com Published: 2022-06-03 05:59:34 11月24日 Update

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.

Tags: Encryption public key character string item algorithm data file symmetry clarity security front end blog persistence interface number server guarantee contact update Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Technology macOS OPPO Reno Apple