HomeLinuxPython Encrypt String

Python Encrypt String


There are a number of cases the place the programmer must encrypt the data of the person to safeguard it. The data is mostly saved in a string variable, and subsequently, to safeguard this info, the programmer encrypts the string. There are numerous packages/modules which give strategies to encrypt and decrypt the strings.

This put up will likely be your information to encrypting strings in Python utilizing numerous modules and can comprise the next content material:

Methodology 1: Use the Cryptography Module to Encrypt Strings

The “Cryptography” incorporates a package deal named “Fernet” that can be utilized to encrypt strings in Python. Nevertheless, you’ll have to set up the “cryptography” module by utilizing the next command within the terminal:

After getting put in the module, begin by importing the “fernet” package deal and creating your string to be encrypted:

from cryptography.fernet import Fernet

stringVar = “LinuxHint’s Secret”

After that, generate a key utilizing the generate_key() methodology add retailer it inside a brand new variable:

keyVar = Fernet.generate_key()

Create a brand new Fernet occasion with the assistance of the generated key:

Use the Fernet occasion “ferVar” to name the encrypt() methodology and go within the string with encode() methodology utilized on it:

encString = ferVar.encrypt(stringVar.encode())

Lastly, print each the unique string and the encrypted string onto the terminal utilizing the print() methodology:

print(“Unique String: “, stringVar)
print(“Encrypted string: “, encString)

The whole code snippet for this instance is as:

from cryptography.fernet import Fernet

stringVar = “LinuxHint’s Secret”
keyVar = Fernet.generate_key()
ferVar = Fernet(keyVar)
encString = ferVar.encrypt(stringVar.encode())

print(“Unique String: “, stringVar)
print(“Encrypted string: “, encString)

Whenever you executed this code snippet in your machine, it’ll show the next outcomes on the terminal:

You have got efficiently encrypted your string in Python utilizing the cryptography library. Nevertheless, to decrypt the string, use the next command:

decString = ferVar.decrypt(encString).decode()
print(nDecrypted string: “, decString)

Append this code within the above code snippet, and execute it to get the next final result on the terminal:

As you’ll be able to see from the output picture above, you had been in a position to not solely encrypt a string in Python but additionally decrypt it utilizing the cryptography library.

Methodology 2: Use the RSA Library to Encrypt a String in Python

One other library that can be utilized to encrypt and decrypt strings in Python is the “rsa” library. To make use of this library, you’ll have to set up it utilizing the next command:

The “rsa” library makes use of private and non-private keys to encrypt and decrypt strings, and these strings may be generated utilizing the newkeys(512) methodology. Due to this fact, import the rsa library and create private and non-private key variables:

import rsa
 
pubkeyVar, priKeyVar = rsa.newkeys(512)

After that, create the string to be encrypted:

stringVar = “LinuxHint Confidential”

Encrypt the string utilizing the encrypt() methodology of rsa by offering the string and the general public key within the arguments:

encString = rsa.encrypt(stringVar.encode(),pubkeyVar)

As soon as that’s carried out, print the unique string and the encrypted string on the terminal utilizing the next strains:

print(“The Unique string: “, stringVar)
print(“The Encrypted string: “, encString)

Whenever you execute this code, it’ll produce the next outcomes on the terminal:

You have got efficiently encrypted a string in Python utilizing the “rsa” library. To decrypt the identical string, append the next strains within the above code snippet:

decMessage = rsa.decrypt(encString, priKeyVar).decode()
print(nThe Decrypted string: “, decMessage)

Whenever you execute this code snippet, this time it’ll produce the next outcomes:

You have got efficiently realized learn how to encrypt strings utilizing the rsa library.

Conclusion

The person can use the cryptography and the rsa library to encrypt and decrypt strings in Python. To make use of the strategies of those libraries, you’ll have to set up these libraries with the assistance of the “pip set up” command. The remainder of the method has been completely defined on this information.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments