pybase64 encode and decode Messages using Python

  • Post author:
  • Post comments:0 Comments
  • Reading time:57 mins read

Python module pybase64 Helps us to encode and decode messages using Python with few lines of code. When data is sent or processed through a text-only system, encoding prevents it from becoming corrupted. We’ll talk about Base64 encoding and decoding, as well as how to use it to encode and decode binary and text data, in this post.

What is meant by ASCII Characters?

ASCII is an acronym for “American Standard Code for Information Interchange,” a character encoding that employs numeric codes to represent characters. Upper and lowercase English letters, digits, and punctuation symbols are among them.

What exactly is Pybase64?

pybase64 helps us to encode or decode messages. It is a sort of byte-to-ASCII character conversion.6 bits of data are represented by each Base64 character. It’s also worth noting that, for obvious reasons, it’s not intended for encryption.

The following procedures should be taken to convert a string to a Base64 character:

  • Each character in the string’s ASCII value is returned.
  • Calculate the ASCII values’ 8-bit binary equivalents 
  • Convert the 8-bit characters chunk into 6-bit chunks by re-grouping the digits
  • Convert the 6-bit binary groups to their decimal values
  • Align the relevant Base64 values for each decimal number using the Base64 encoding table.

Let’s Move on to installing pybase64 using the pip command
pip install pybase64 – This will help us to install pybase64 
After you successfully installed this library Now let’s import this by the following command


import base64

Now let’s write a code to encode a message using python 


message_e = input("Enter the message you want to encode: ")
message_e_bytes = message_e.encode("ascii")

base64_bytes = base64.b64encode(message_e_bytes)
base64_string = base64_bytes.decode("ascii")

print(f"Encoded message: {base64_string}")

output:


input = Follow ideasorblogs for more
    
Output = Rm9sbG93IGlkZWFzb3JibG9ncyBmb3IgbW9yZQ==

Now let’s write a code to decode the same message using python 


base64_string = input("Enter the message you want to decode: ")
base64_bytes = base64_string.encode("ascii")

message_d_bytes = base64.b64decode(base64_bytes)
message_d = message_d_bytes.decode("ascii")

print(f"Decoded Message: {message_d}")

Output:


input = Rm9sbG93IGlkZWFzb3JibG9ncyBmb3IgbW9yZQ==
    
Output = Follow ideasorblogs for more

Let’s Just modify the code and allow the user to select the option which operation want to perform now. We can simply do that by the following code


import base64


user_input = 'random'


def my_list():
    print("Choose the option to continue: ")
    print("E. To encode an Message:")
    print("D. To Decode an Message:")
    print("3. Exit:")

while user_input != '3':
    my_list()
    user_input = input("what you want to do?: ")

    if user_input == 'E':


        message_e = input("Enter the message you want to encode: ")
        message_e_bytes = message_e.encode("ascii")

        base64_bytes = base64.b64encode(message_e_bytes)
        base64_string = base64_bytes.decode("ascii")

        print(f"Encoded message: {base64_string}")




    elif user_input == 'D':
        base64_string = input("Enter the message you want to decode: ")
        base64_bytes = base64_string.encode("ascii")

        message_d_bytes = base64.b64decode(base64_bytes)
        message_d = message_d_bytes.decode("ascii")

        print(f"Decoded message: {message_d}")

    elif user_input == '3':
        print("Program Exited")
    else:
        print("please enter a vaild value")

Publisher

Publisher

Publisher @ideasorblogs

Leave a Reply