diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..fcd9d37 --- /dev/null +++ b/.flake8 @@ -0,0 +1,3 @@ +[flake8] +max-line-length = 120 +exclude = .git,__pycache__,venv,.venv diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..25f2019 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,28 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + +jobs: + build: + runs-on: ubuntu-24.04 + strategy: + fail-fast: false + matrix: + python-version: ['3.9', '3.12'] + steps: + - uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt flake8 + - name: Byte-compile sources + run: python -m py_compile MessageEncodeDecode.py + - name: Lint with flake8 + run: flake8 . diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..60d1128 --- /dev/null +++ b/.gitignore @@ -0,0 +1,32 @@ +# Byte-compiled / cache +__pycache__/ +*.py[cod] +*$py.class + +# Virtual environments +.venv/ +venv/ +env/ +ENV/ + +# Environment / secrets +.env +.env.* + + +# Packaging / build +build/ +dist/ +*.egg-info/ + +# Tests / tooling caches +.pytest_cache/ +.mypy_cache/ +.coverage +htmlcov/ + +# Editors / OS +.vscode/ +.idea/ +.DS_Store +Thumbs.db diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9adad07 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 Diganta Datta + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/MessageEncodeDecode.py b/MessageEncodeDecode.py index 8d92bf6..42d8899 100644 --- a/MessageEncodeDecode.py +++ b/MessageEncodeDecode.py @@ -1,65 +1,100 @@ -from tkinter import * -import base64 - -root = Tk() -root.geometry('500x300') -root.resizable(0,0) -root.title("DataCrypter - Message Encode and Decode") - -Label(root, text ='ENCODE DECODE', font = 'arial 20 bold').pack() -Label(root, text ='Diganta Datta', font = 'arial 20 bold').pack(side =BOTTOM) - -Text = StringVar() -private_key = StringVar() -mode = StringVar() -Result = StringVar() - -def Encode(key,message): - enc=[] - for i in range(len(message)): - key_c = key[i % len(key)] - enc.append(chr((ord(message[i]) + ord(key_c)) % 256)) - return base64.urlsafe_b64encode("".join(enc).encode()).decode() - -def Decode(key,message): - dec=[] - message = base64.urlsafe_b64decode(message).decode() - for i in range(len(message)): - key_c = key[i % len(key)] - dec.append(chr((256 + ord(message[i])- ord(key_c)) % 256)) - return "".join(dec) - -def Mode(): - if(mode.get() == 'e'): - Result.set(Encode(private_key.get(), Text.get())) - elif(mode.get() == 'd'): - Result.set(Decode(private_key.get(), Text.get())) - else: - Result.set('Invalid Mode') - -def Exit(): - root.destroy() - -def Reset(): - Text.set("") - private_key.set("") - mode.set("") - Result.set("") - -Label(root, font= 'arial 12 bold', text='MESSAGE').place(x= 60,y=60) -Entry(root, font = 'arial 10', textvariable = Text, bg = 'ghost white').place(x=290, y = 60) - -Label(root, font = 'arial 12 bold', text ='KEY').place(x=60, y = 90) -Entry(root, font = 'arial 10', textvariable = private_key , bg ='ghost white').place(x=290, y = 90) - -Label(root, font = 'arial 12 bold', text ='MODE(e-encode, d-decode)').place(x=60, y = 120) -Entry(root, font = 'arial 10', textvariable = mode , bg= 'ghost white').place(x=290, y = 120) -Entry(root, font = 'arial 10 bold', textvariable = Result, bg ='ghost white').place(x=290, y = 150) - -Button(root, font = 'arial 10 bold', text = 'RESULT' ,padx =2,bg ='LightGray' ,command = Mode).place(x=60, y = 150) - -Button(root, font = 'arial 10 bold' ,text ='RESET' ,width =6, command = Reset,bg = 'LimeGreen', padx=2).place(x=80, y = 190) - -Button(root, font = 'arial 10 bold',text= 'EXIT' , width = 6, command = Exit,bg = 'OrangeRed', padx=2, pady=2).place(x=180, y = 190) - -root.mainloop() \ No newline at end of file +"""DataCrypter - a small Tkinter app to encode and decode messages with a key. + +Encoding adds each character of the key (repeated as needed) to each character +of the message modulo 256, then Base64 (URL-safe) encodes the result. Decoding +reverses these steps with the same key. + +This is a simple obfuscation technique, not strong encryption. +""" +import base64 +import binascii +import tkinter as tk + + +def Encode(key, message): + """Encode ``message`` using ``key`` and return a URL-safe Base64 string.""" + if not key: + raise ValueError('A key is required.') + enc = [] + for i in range(len(message)): + key_c = key[i % len(key)] + enc.append(chr((ord(message[i]) + ord(key_c)) % 256)) + return base64.urlsafe_b64encode("".join(enc).encode()).decode() + + +def Decode(key, message): + """Decode a string produced by :func:`Encode` using the same ``key``.""" + if not key: + raise ValueError('A key is required.') + dec = [] + message = base64.urlsafe_b64decode(message).decode() + for i in range(len(message)): + key_c = key[i % len(key)] + dec.append(chr((256 + ord(message[i]) - ord(key_c)) % 256)) + return "".join(dec) + + +def main(): + """Build and run the Tkinter user interface.""" + root = tk.Tk() + root.geometry('500x300') + root.resizable(False, False) + root.title("DataCrypter - Message Encode and Decode") + + tk.Label(root, text='ENCODE DECODE', font='arial 20 bold').pack() + tk.Label(root, text='Diganta Datta', font='arial 20 bold').pack(side=tk.BOTTOM) + + text = tk.StringVar() + private_key = tk.StringVar() + mode = tk.StringVar() + result = tk.StringVar() + + def run_mode(): + selected = mode.get().strip().lower() + if selected not in ('e', 'd'): + result.set('Invalid Mode') + return + if not private_key.get(): + result.set('Key is required') + return + try: + if selected == 'e': + result.set(Encode(private_key.get(), text.get())) + else: + result.set(Decode(private_key.get(), text.get())) + except (binascii.Error, UnicodeDecodeError, ValueError): + result.set('Invalid input for decoding') + + def exit_app(): + root.destroy() + + def reset(): + text.set("") + private_key.set("") + mode.set("") + result.set("") + + tk.Label(root, font='arial 12 bold', text='MESSAGE').place(x=60, y=60) + tk.Entry(root, font='arial 10', textvariable=text, bg='ghost white').place(x=290, y=60) + + tk.Label(root, font='arial 12 bold', text='KEY').place(x=60, y=90) + tk.Entry(root, font='arial 10', textvariable=private_key, bg='ghost white').place(x=290, y=90) + + tk.Label(root, font='arial 12 bold', text='MODE(e-encode, d-decode)').place(x=60, y=120) + tk.Entry(root, font='arial 10', textvariable=mode, bg='ghost white').place(x=290, y=120) + tk.Entry(root, font='arial 10 bold', textvariable=result, bg='ghost white').place(x=290, y=150) + + tk.Button(root, font='arial 10 bold', text='RESULT', padx=2, bg='LightGray', + command=run_mode).place(x=60, y=150) + + tk.Button(root, font='arial 10 bold', text='RESET', width=6, command=reset, + bg='LimeGreen', padx=2).place(x=80, y=190) + + tk.Button(root, font='arial 10 bold', text='EXIT', width=6, command=exit_app, + bg='OrangeRed', padx=2, pady=2).place(x=180, y=190) + + root.mainloop() + + +if __name__ == '__main__': + main() diff --git a/README.md b/README.md index 44970cf..546bc5c 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,72 @@ -# Python-Message-Encoder-Decorder +# Python Message Encoder / Decoder (DataCrypter) -Message encoding and decoding is the process to first convert the original text to the random and meaningless text called ciphertext. This process is called encoding. Decoding is the process to convert that ciphertext to the original text. This process is also called the Encryption-Decryption process.Secure your Information by Encoding the messages. Encoding is the process that transforms the text or information to the unrecognizable form and decryption is the process to convert the encrypted message into original form. +A small desktop app built with Tkinter that encodes and decodes text messages using a shared key. Type a message, a key and a mode, and press **RESULT**. The same key must be used to decode a message that was encoded. -This objective of this project is to encode and decode messages using a common key. This project will be built using the Tkinter and base64 library. -In this project, users have to enter the message to encode or decode. Users have to select the mode to choose the encoding and decoding process. The same key must be used to process the encoding and decoding for the same message. +The goal of the project is to encode and decode messages using a common key, using the Tkinter and `base64` modules from the Python standard library. -## System Requirements +> Note: this is a simple key-based obfuscation technique for learning purposes. It is **not** strong encryption and should not be used to protect sensitive information. -Programming Language: Python -Packages: Tkinter, Base64 +## Features + +- Encode a message with a key and get a URL-safe Base64 string +- Decode that string back to the original message with the same key +- Simple Tkinter window with MESSAGE, KEY and MODE fields, and RESULT, RESET and EXIT buttons +- Friendly messages for an invalid mode, a missing key, or text that cannot be decoded +- No third-party dependencies + +## How it works + +- **Encode**: each character of the message is shifted by the matching character of the key (the key repeats as needed): `(ord(message_char) + ord(key_char)) % 256`. The resulting text is encoded as URL-safe Base64. +- **Decode**: the Base64 text is decoded and the shift is reversed with the same key. + +Because the shift is taken modulo 256, characters outside the Latin-1 range (code points above 255) will not survive an encode/decode round trip. + +## Tech stack + +- Python 3 +- Tkinter (GUI) and `base64` (both in the standard library) + +## Setup + +```bash +git clone https://github.com/digantadatta/Python-Message-Encoder-Decorder.git +cd Python-Message-Encoder-Decorder +``` + +No packages need to be installed (see `requirements.txt`). Tkinter is bundled with the official Python installers for Windows and macOS; on Debian/Ubuntu install it with `sudo apt install python3-tk`. + +## Run + +```bash +python MessageEncodeDecode.py +``` + +## Usage example + +1. Enter `Hello World` in **MESSAGE**, `mykey` in **KEY**, and `e` in **MODE**, then press **RESULT**. The result field shows `wrXDnsOXw5HDqMKNw5DDmsOXw6XDkQ==`. +2. To decode, paste that result into **MESSAGE**, keep the key `mykey`, set **MODE** to `d`, and press **RESULT**. The result field shows `Hello World`. +3. **RESET** clears all fields; **EXIT** closes the window. + +You can also use the functions from Python: + +```python +from MessageEncodeDecode import Encode, Decode + +token = Encode("mykey", "Hello World") +print(Decode("mykey", token)) # Hello World +``` + +## Project structure + +``` +. +├── MessageEncodeDecode.py # Encode/Decode functions and the Tkinter UI +├── requirements.txt # No third-party dependencies +├── LICENSE +└── .github/workflows/ci.yml # CI: compile and flake8 + +``` + +## License + +Released under the [MIT License](LICENSE). diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5925598 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +# This project uses only the Python standard library (tkinter, base64). +# tkinter ships with the official Python installers for Windows and macOS. +# On Debian/Ubuntu install it with: sudo apt install python3-tk +# No third-party packages are required.