Python Encryption Techniques for Secure Data Management

Ench, Lastly, each character in the message is converted into a cipher text by the above two lines and is appended to the empty string we created earlier. In the last line, we print the encrypted message stored in the variable encmsg. The output is given below. Encryption Using Ascii Based on the ASCII chart, the Unicode Integer value of the character H is 72. When we add the key value that is 3, it becomes 75. The character associated with the integer 75 in the chart is K. So, the encrypted message has K as its first character. The same approach is followed for all the characters in the message. Similarly, the exclamatory mark(!) at the end of the message has an integer value of 33. Now when we add the key value, it becomes 36. The character associated with 36 in the chart is $. Hence, the last character in the output is $. Encryption Using String Reversal Let us make the previous approach a little complex. In this method, we will follow the same steps as in the previous approach but reverse the encrypted message at the end. Let us see how we can do that. 123456789 msg = input(“Enter a message to encrypt: “) encmsg = “” for ch in msg:     asc = ord(ch) + 3     ench = chr(asc)     encmsg += ench
encmsg= encmsg[::-1] print(“The encrypted message is:”,encmsg) Observe the eighth line. We have taken the encrypted message (encmsg) and applied reversal on it with the help of the slicing operator. Visit this article to learn more about string reversal in python. Let us see the output. Encryption Using String Reversal If you compare the two outputs, the encrypted message is the same. But, the order of arrangement is the opposite. The above-discussed methods do not provide promising encryption for the data. Let us learn about other secure methods to encrypt the message. Symmetric Key Encryption In symmetric key encryption, only one key is used for encryption and decryption. The key used in this type of encryption is called a secret key. The secret key that the sender and receiver use could be a specific code or a random string of letters or numbers produced by a random number generator. Symmetric key encryption can provide secrecy but cannot ensure authenticity. In this approach, we will look at Fernet the algorithm available in the cryptography module and generate a key using the fernet object. But first, you need to install the cryptography package. !pip install cryptography Once you have installed it, you can now import the Fernet object. Let us see the code. 123456789 from cryptography.fernet import Fernet key=Fernet.generate_key() ferr=Fernet(key) msg=ferr.encrypt(b”I’m sure you will never understand this message!”) ciphmsg=ferr.decrypt(msg) orgmsg=ciphmsg.decode() print(“The original message is:\n”,orgmsg) print(“The encypted message is:\n”,msg) print(“The cipher text is:\n”,ciphmsg) from cryptography.fernet import Fernet: Fernet is a class of the cryptography.fernet module. We are importing the Fernet class in this line. key=Fernet.generate_key(): We are generating an encryption key that will be used to encrypt the message. This encryption key is stored in a variable called key ferr=Fernet(key): We are creating a new variable fer for storing an instance of the key generated earlier. msg=ferr.encrypt(bIm sure you will never understand this message!): The message we are trying to encrypt is I’m sure you will never understand this message! This message is stored in a variable called msg. The encrypt() method is used to encode the message. This message is then converted into bytes which are represented by the b in front of the message as a prefix ciphmsg=ferr.decrypt(msg): The message encrypted earlier is now being decrypted using the decrypt() method. As seen in the previous line, the message is converted into bytes. So by default, the decrypted message will also be in the form of bytes. This message is stored in a variable called ciphmsg. orgmsg=ciphmsg.decode(): The byte form of the decrypted message is now converted into readable text with the help of decode(). orgmsg is a variable created for storing the message after conversion. In the following three lines, we are printing the original message we have given, the encrypted form, and finally, the cipher text. Encryption Using Symmetric Key Conclusion Even though cryptography is hard, it is always considered safe to send messages to trusted receivers through cryptic and secret codes that might be useful in a data breach. Sending or receiving encrypted messages can also ensure confidentiality, integrity, and security. Cryptography is not a royal technology anymore. To protect and secure our data and information, we can also implement the methods and algorithms of cryptography in our daily life. In this post, we have seen the definitions of cryptography, encryption, and decryption and a flow chart of data transmission with the help of encryption and decryption keys. Coming to the encryption programs, we have learned how to encrypt our messages using Unicode Integer and character values using the ASCII protocols. We have also observed a reversal of the message as an approach to encrypt the message. But these methods do not guarantee a perfect encryption mechanism. Hence, we have learned the modules usage and installation and how to perform symmetric key encryption using the algorithms available in the package. We have tried using the Fernet algorithm for symmetric key encryption, which uses only one key for encryption and decryption. References To learn more about the cryptography package, visit this official site of PyPI.

Can you add some examples to clarify how the Fernet method works in Python?

SICORPS