{"id":82247,"date":"2021-06-25T15:47:17","date_gmt":"2021-06-25T10:17:17","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=82247"},"modified":"2026-06-03T15:46:06","modified_gmt":"2026-06-03T10:16:06","slug":"python-message-encode-decode","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-message-encode-decode\/","title":{"rendered":"Message Encode Decode using Python with GUI"},"content":{"rendered":"<p>In today&#8217;s world, data can easily be stolen and used by hackers. With data encryption and decryption we can secure our messages. This python project will encode and decode messages in real-time.<\/p>\n<p>Encoding is the process of converting a text into an unrecognizable form and Decoding is the reverse process, ie converting the encoded data back to the original text.<\/p>\n<h3>Python Message Encode Decode Project<\/h3>\n<p>In this python project, we are going to encode and decode strings (messages). We are going to use tkinter and base64 libraries.<\/p>\n<p>This python project provides a gui using tkinter where user can work easily:<\/p>\n<ul>\n<li>The user has to enter a message to encode or decode.<\/li>\n<li>User must select the option encode or decode, it depends on the operation he wants to perform.<\/li>\n<li>Providing the same key is mandatory, ie while decoding user must enter the same key which he used for encoding<\/li>\n<\/ul>\n<h3>Project Prerequisites<\/h3>\n<p>To develop message encode decode project you require the basic concepts of python, tkinter, and base64.<\/p>\n<p>To install tkinter and base64, please run below commands:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">pip install tkinter\r\npip install base64\r\n<\/pre>\n<h3>Download Message Encode and Decode Python Program<\/h3>\n<p>Please download the source code of python Message Encode and Decode: <a href=\"https:\/\/drive.google.com\/file\/d\/1MrN1i1XuYesC9MeDbnpfHJw5Y2BtTRYQ\/view?usp=drive_link\"><strong>Message Encode Decode Project Code<\/strong><\/a><\/p>\n<h3>Project File Structure<\/h3>\n<p>First, let&#8217;s look into the message encode and decode python program overview:<\/p>\n<ol>\n<li>Importing the base64 and tkinter modules.<\/li>\n<li>Creating the GUI using tkinter.<\/li>\n<li>Creating the functions and the button.<\/li>\n<\/ol>\n<h4>1. Creating the Program<\/h4>\n<p>Import the required tkinter and random packages.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">from tkinter import *\r\nimport base64\r\nfrom tkinter import messagebox\r\n<\/pre>\n<p><strong>Explanation:<\/strong><\/p>\n<p>Let&#8217;s begin with the start of the program by importing the required libraries<\/p>\n<ul>\n<li><strong>Tkinter library:<\/strong> Tkinter is the most popular GUI library in Python. Tkinter is used to create simple GUI applications in python<\/li>\n<li><strong>base64:<\/strong> This library provides a function to encode binary data to ASCII characters and decode the ASCII characters back to binary data.<\/li>\n<\/ul>\n<h4>2. Creating the GUI:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">root = Tk()\r\n\r\nroot.geometry(\"600x600\")\r\n\r\nroot.title(\"TechVidvan Message Encryption and Decryption\")\r\n\r\n\r\nMsg = StringVar()\r\nkey = StringVar()\r\nmode = StringVar()\r\nResult = StringVar()\r\n\r\nlabel = Label(root, text='Enter Message', font=('Helvetica',10))\r\nlabel.place(x=10,y=0)\r\n\r\nmes = Entry(root,textvariable=Msg, font=('calibre',10,'normal'))\r\nmes.place(x=200,y=0)\r\n\r\nlabel1 = Label(root, text='e for encrypt and d for decrypt', font=('Helvetica',10))\r\nlabel1.place(x=10,y=50)\r\n\r\nl_mode = Entry(root, textvariable=mode, font=('calibre',10,'normal'))\r\nl_mode.place(x=200,y=50)\r\n\r\nlabel2 = Label(root, text='Enter key', font=('Helvetica',10))\r\nlabel2.place(x=10,y=100)\r\n\r\nl_key = Entry(root, textvariable=key, font=('calibre',10,'normal'))\r\nl_key.place(x=200,y=100)\r\n\r\nlabel3 = Label(root, text='Result', font=('Helvetica',10))\r\nlabel3.place(x=10,y=150)\r\n\r\nres = Entry(root,textvariable=Result, font=('calibre',10,'normal'))\r\nres.place(x=200,y=150)<\/pre>\n<p><strong>Explanation:<\/strong><\/p>\n<p>The main function part involves creation of the Tk root widget. We create the GUI of this program.<\/p>\n<ul>\n<li><strong>Label():<\/strong> This function creates a label that can make text appear on the GUI.<\/li>\n<li><strong>Pack():<\/strong> The pack() method declares the position of widgets with respect to one and another.<\/li>\n<li><strong>Grid():<\/strong> This method allows managing the layout of the widget.<\/li>\n<\/ul>\n<h4>3. Creating the function and the buttons:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def encode(key, msg):\r\n    enc = []\r\n    for i in range(len(msg)):\r\n        key_c = key[i % len(key)]\r\n        enc_c = chr((ord(msg[i]) +\r\n                     ord(key_c)) % 256)\r\n        enc.append(enc_c)\r\n    return base64.urlsafe_b64encode(\"\".join(enc).encode()).decode()\r\n\r\n\r\n\r\ndef decode(key, enc):\r\n    dec = []\r\n    enc = base64.urlsafe_b64decode(enc).decode()\r\n    for i in range(len(enc)):\r\n        key_c = key[i % len(key)]\r\n        dec_c = chr((256 + ord(enc[i]) - ord(key_c)) % 256)\r\n\r\n        dec.append(dec_c)\r\n    return \"\".join(dec)\r\n\r\n\r\ndef Results():\r\n    msg = Msg.get()\r\n    k = key.get()\r\n    m = mode.get()\r\n    m.lower()\r\n    if (m == 'e'):\r\n        Result.set(encode(k, msg))\r\n    elif(m== 'd'):\r\n        Result.set(decode(k, msg))\r\n    else:\r\n        messagebox.showinfo('TechVidvan', 'Wrong mode entered. Try again.')\r\n\r\n\r\n\r\ndef qExit():\r\n    root.destroy()\r\n\r\n\r\n\r\ndef Reset():\r\n    Msg.set(\"\")\r\n    key.set(\"\")\r\n    mode.set(\"\")\r\n    Result.set(\"\")\r\n\r\n\r\nbtnshow = Button(root, text='Show Message', foreground='green', command=Results)\r\nbtnshow.place(x=10,y=200)\r\n\r\nbtnreset = Button(root, text='Reset', foreground='red', command=Reset)\r\nbtnshow.place(x=150,y=200)\r\n\r\nbtnexit = Button(root, text='Exit', foreground='black', command=qExit)\r\nbtnshow.place(x=300,y=200)\r\n\r\n\r\nroot.mainloop()\r\n<\/pre>\n<p><strong>Explanation:<\/strong><\/p>\n<ul>\n<li><strong>Encode:<\/strong> encode the string and return the encoded string.\n<ul>\n<li>Enc is an empty list<\/li>\n<li>The loop runs till the end of the message<\/li>\n<li>i% of len(key) returns the remainder of division between i and len (key) and that remainder will be used as an index of key and the value of key at that index is stored in key_c<\/li>\n<li>ord()- generates integer unicode corresponding to the string argument of single unicode character<\/li>\n<li>chr()- This function takes an integer argument and converts it into string.<\/li>\n<li>base64.urlsafe_b64encode encodes a string.<\/li>\n<li>The join() method is used to joins each element of list, string, and tuple by a string separator (can be a custom separator) and gives final concatenated string.<\/li>\n<li>encode()- returns the utf-8 encoded string.<\/li>\n<li>decode()- return the decoded string.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Decode:<\/strong> Decode the string and return the decoded string.\n<ul>\n<li>Dec is an empty list.<\/li>\n<li>256 + ord(message[i]) \u2013 ord(key_c)) % 256 gives the remainder of addition of 256 with subtraction of ord(message[i]) \u2013 ord( key_c) and then division with 256 and passes the final remainder to chr() function<\/li>\n<li>chr() function convert integer value to string and store to dec_c<\/li>\n<li>Return the decoded string.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Results-<\/strong> Checks which function needs to be run and updates the result.<\/li>\n<li><strong>Reset<\/strong>&#8211; Resets the input area.<\/li>\n<li><strong>qExit-<\/strong> Ends the program.<\/li>\n<li><strong>Button()<\/strong>&#8211; This function creates a button and sets a function to perform when pressed.<\/li>\n<li><strong>Btntotal<\/strong> when pressed calls the result function.<\/li>\n<li><strong>Btnreset<\/strong> when pressed calls the reset function.<\/li>\n<li><strong>Btnexit<\/strong> when pressed calls the qExit function.<\/li>\n<li><strong>Mainloop()-<\/strong> This function updates the GUI and waits for the events to happen.<\/li>\n<\/ul>\n<h3>Python Message Encode Decode Output<\/h3>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/python-message-encode-decode-output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-82255\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/python-message-encode-decode-output.png\" alt=\"python message encode decode output\" width=\"1920\" height=\"1012\" \/><\/a><\/p>\n<h3>Summary<\/h3>\n<p>We have successfully created python message encryptor and decryptor., which encode and decode the messages in real-time. We used popular libraries such as tkinter and base64. We learned how to create a GUI, create buttons and call the functions when the buttons were pressed. We also learned how to encode and decode the strings in python.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In today&#8217;s world, data can easily be stolen and used by hackers. With data encryption and decryption we can secure our messages. This python project will encode and decode messages in real-time. Encoding is&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":82263,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[3675,3676,483],"class_list":["post-82247","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python-message-encode","tag-python-message-encode-decode","tag-python-project"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Message Encode Decode using Python with GUI - TechVidvan<\/title>\n<meta name=\"description\" content=\"Encode and decode messages in python. Create python project with gui to encrypt and decrypt the messages in real-time\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-message-encode-decode\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Message Encode Decode using Python with GUI - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Encode and decode messages in python. Create python project with gui to encrypt and decrypt the messages in real-time\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-message-encode-decode\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-25T10:17:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-06-03T10:16:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/python-project-encode-decode-messages.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Message Encode Decode using Python with GUI - TechVidvan","description":"Encode and decode messages in python. Create python project with gui to encrypt and decrypt the messages in real-time","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/python-message-encode-decode\/","og_locale":"en_US","og_type":"article","og_title":"Message Encode Decode using Python with GUI - TechVidvan","og_description":"Encode and decode messages in python. Create python project with gui to encrypt and decrypt the messages in real-time","og_url":"https:\/\/techvidvan.com\/tutorials\/python-message-encode-decode\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-06-25T10:17:17+00:00","article_modified_time":"2026-06-03T10:16:06+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/python-project-encode-decode-messages.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-message-encode-decode\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-message-encode-decode\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Message Encode Decode using Python with GUI","datePublished":"2021-06-25T10:17:17+00:00","dateModified":"2026-06-03T10:16:06+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-message-encode-decode\/"},"wordCount":707,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-message-encode-decode\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/python-project-encode-decode-messages.jpg","keywords":["python message encode","python message encode decode","Python project"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-message-encode-decode\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-message-encode-decode\/","url":"https:\/\/techvidvan.com\/tutorials\/python-message-encode-decode\/","name":"Message Encode Decode using Python with GUI - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-message-encode-decode\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-message-encode-decode\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/python-project-encode-decode-messages.jpg","datePublished":"2021-06-25T10:17:17+00:00","dateModified":"2026-06-03T10:16:06+00:00","description":"Encode and decode messages in python. Create python project with gui to encrypt and decrypt the messages in real-time","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-message-encode-decode\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-message-encode-decode\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-message-encode-decode\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/python-project-encode-decode-messages.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/python-project-encode-decode-messages.jpg","width":1200,"height":628,"caption":"python project encode decode messages"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-message-encode-decode\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Message Encode Decode using Python with GUI"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/82247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=82247"}],"version-history":[{"count":2,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/82247\/revisions"}],"predecessor-version":[{"id":448124,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/82247\/revisions\/448124"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/82263"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=82247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=82247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=82247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}