base64 is a python library that can be used to encrypt and decrypt strings and characters, that can have a multiple use case.
One common use case is instead of directly pasting a plain text credential parameters into a file or as a parameter and that can be later decrypted using the decode statements within the program.
Within this blog post, we will cover.
- How can we encrypt strings using base64
- How can be decrypt the above base64 encrypted string using bas64 decoder.
Please note the two important points before we use this module.
- base64 encode and decode functions both require a bytes-like object. In order to get our string into bytes, we must encode it first using Python's built in encode function. Most commonly, the UTF-8 encoding is used.
- Encryption of same string using Linux command line interface and python shell differs, please use the same environment for both the encryption and decryption.
Here in this example, we will encrypt our string "cyberkeeda@123" and later we will decrypt it.
Encryption
# Encryption Block
import base64
base64.b64encode(bytes("cyberkeeda@123", "utf-8"))
Output for the above.
b'Y3liZXJrZWVkYUAxMjM='
Decryption
Below code can be use to decrypt the above, please use only the string content enclosed within string to decrypt, so for the above example output consider string leaving the b ( byte ) identifier.
# Decryption Block
import base64
base64.b64decode("Y3liZXJrZWVkYUAxMjM=").decode("utf-8")
Output for the above.
'cyberkeeda@123'
Hope this small piece of snippet will help you in some context.