Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
breezy-codes committed Dec 5, 2024
1 parent bc99cb1 commit f53bd3e
Show file tree
Hide file tree
Showing 13 changed files with 1,834 additions and 48 deletions.
6 changes: 0 additions & 6 deletions .idea/.gitignore

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/cryptography-guide.iml

This file was deleted.

13 changes: 0 additions & 13 deletions .idea/inspectionProfiles/Project_Default.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/inspectionProfiles/profiles_settings.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

17 changes: 17 additions & 0 deletions .vscode/my-running-guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# to update the book -

cd /home/breezy/Documents/GitHub/Jupyter-Books

jupyter-book build cryptography-guide

or rebuild with

jupyter-book build --all cryptography-guide

# to push the book to GitHub Pages -

cd /home/breezy/Documents/GitHub/Jupyter-Books/cryptography-guide

make sure in main -

ghp-import -n -p -f _build/html
10 changes: 9 additions & 1 deletion _toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ parts:
- file: maths/number-theory/prime-factorisation
- file: maths/number-theory/gcd-lcm
- file: maths/number-theory/prime-introduction
- file: maths/number-theory/converting-values
- file: maths/modular-arithmetic/mod-arith-intro
sections:
- file: maths/modular-arithmetic/modular-arithmetic
Expand All @@ -30,8 +31,15 @@ parts:
chapters:
- file: ciphers/ceaser-cipher
- file: ciphers/hill-cipher
- file: ciphers/morse-code
- file: ciphers/base64
- caption: RSA Algorithm
numbered: false
chapters:
- file: RSA-Algorithm/RSA-example
- file: RSA-Algorithm/RSA-with-large-num
- file: RSA-Algorithm/RSA-with-large-num
- caption: Steganography
numbered: false
chapters:
- file: steganography/bitmap-steganography
- file: steganography/bitmap-caesar
89 changes: 89 additions & 0 deletions ciphers/base64.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Base64"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"BASE64_CHARS = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def base64_encode(input_string):\n",
" encoded_string = \"\"\n",
" value = 0\n",
" bits = -6\n",
" base64_mask = 0x3F\n",
"\n",
" for character in input_string.encode():\n",
" value = (value << 8) + character\n",
" bits += 8\n",
"\n",
" while bits >= 0:\n",
" encoded_string += BASE64_CHARS[(value >> bits) & base64_mask]\n",
" bits -= 6\n",
" \n",
" if bits > -6:\n",
" encoded_string += BASE64_CHARS[((value << 8) >> (bits + 8)) & base64_mask]\n",
" \n",
" while len(encoded_string) % 4:\n",
" encoded_string += \"=\"\n",
" \n",
" return encoded_string"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def base64_decode(input_string):\n",
" decoded_string = bytearray()\n",
" value = 0\n",
" bits = -8\n",
"\n",
" for character in input_string:\n",
" if character not in BASE64_CHARS:\n",
" if character == \"=\":\n",
" break\n",
" continue\n",
" \n",
" value = (value << 6) + BASE64_CHARS.index(character)\n",
" bits += 6\n",
"\n",
" if bits >= 0:\n",
" decoded_string.append((value >> bits) & 0xFF)\n",
" bits -= 8\n",
" \n",
" return decoded_string.decode()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "jupyter-books",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
200 changes: 200 additions & 0 deletions ciphers/morse-code.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# **Morse Code**\n",
"\n",
"Morse code is a method used in telecommunication to encode text characters as sequences of two different signal durations, called dots and dashes. Named after Samuel Morse, one of the inventors of the telegraph, Morse code is widely used in amateur radio, aviation, and military communications due to its efficiency and reliability.\n",
"\n",
"In this guide, we will explore Morse code and demonstrate how to create programs for encoding text into Morse code and decoding Morse code back into text."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Encoding Text to Morse Code\n",
"\n",
"Encoding text to Morse code involves converting each character into its corresponding Morse code sequence. Each letter, number, or symbol has a unique representation in Morse code, and words are separated by spaces or `/`. How Encoding Works:\n",
"\n",
"- Each character in the input text is matched with its Morse code representation using a dictionary.\n",
"- Characters not present in the dictionary are replaced with a placeholder like `?`.\n",
"- Words are separated by a `/`, and individual characters are separated by spaces.\n",
"\n",
"### Steps for Encoding\n",
"\n",
"<Steps>\n",
"1. Convert the text to uppercase to ensure proper dictionary matching.\n",
"2. Loop through each character in the text and find its Morse code equivalent.\n",
"3. Combine the Morse code sequences into a single string, separating characters with spaces and words with `/`.\n",
"</Steps>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Morse Code Dictionary\n",
"MORSE_CODE_DICT = {\n",
" 'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', \n",
" 'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---',\n",
" 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---',\n",
" 'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-',\n",
" 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--',\n",
" 'Z': '--..', '1': '.----', '2': '..---', '3': '...--', '4': '....-', \n",
" '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.', \n",
" '0': '-----', ',': '--..--', '.': '.-.-.-', '?': '..--..', \n",
" \"'\": '.----.', '!': '-.-.--', '/': '-..-.', '(': '-.--.', \n",
" ')': '-.--.-', '&': '.-...', ':': '---...', ';': '-.-.-.', \n",
" '=': '-...-', '+': '.-.-.', '-': '-....-', '_': '..--.-', \n",
" '\"': '.-..-.', '$': '...-..-', '@': '.--.-.', ' ': '/'\n",
"}\n",
"\n",
"# Function to encode text to Morse code\n",
"def encode_to_morse(text):\n",
" encoded_message = []\n",
" for char in text.upper():\n",
" if char in MORSE_CODE_DICT:\n",
" encoded_message.append(MORSE_CODE_DICT[char])\n",
" else:\n",
" encoded_message.append('?') # Unknown character\n",
" return ' '.join(encoded_message)\n",
"\n",
"print(\"Enter the text to encode using morse code:\")\n",
"text = input()\n",
"encoded_text = encode_to_morse(text)\n",
"print(\"Each Morse code character is separated by a space, and each word is separated by a /\")\n",
"print(f\"The Morse code for '{text}' is:\")\n",
"print(encoded_text)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Expected Output\n",
"\n",
"When you run this program, this is how the text \"Hello and welcome to SplashKit, its great to have you.\" would be encoded in Morse code:\n",
"\n",
"```shell\n",
"Enter the text to encode using morse code:\n",
"Hello and welcome to SplashKit, its great to have you.\n",
"Each Morse code character is separated by a space, and each word is separated by a /\n",
"The Morse code for 'Hello and welcome to SplashKit, its great to have you.' is:\n",
".... . .-.. .-.. --- / .- -. -.. / .-- . .-.. -.-. --- -- . / - --- / ... .--. .-.. .- ... .... -.- .. - --..-- / .. - ... / --. .-. . .- - / - --- / .... .- ...- . / -.-- --- ..- .-.-.-\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Decoding Morse Code to Text\n",
"\n",
"Decoding Morse code back to text requires reversing the process. The Morse code input is split into sequences representing individual characters, which are then mapped back to their corresponding text representation. Spaces in Morse code are converted back to spaces in the text. How Decoding Works:\n",
"\n",
"- Each Morse code sequence is matched with its corresponding character using a reversed dictionary.\n",
"- Morse code for spaces (`/`) is converted back to spaces in the text.\n",
"- Invalid sequences are replaced with a placeholder like `?`.\n",
"\n",
"### Steps for Decoding\n",
"\n",
"<Steps>\n",
"1. Split the Morse code input into individual sequences using spaces.\n",
"2. Map each sequence to its corresponding character from the reversed dictionary.\n",
"3. Combine the characters to form the decoded text, handling `/` as spaces.\n",
"</Steps>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Morse Code Dictionary\n",
"MORSE_CODE_DICT = {\n",
" 'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', \n",
" 'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---',\n",
" 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---',\n",
" 'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-',\n",
" 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--',\n",
" 'Z': '--..', '1': '.----', '2': '..---', '3': '...--', '4': '....-', \n",
" '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.', \n",
" '0': '-----', ',': '--..--', '.': '.-.-.-', '?': '..--..', \n",
" \"'\": '.----.', '!': '-.-.--', '/': '-..-.', '(': '-.--.', \n",
" ')': '-.--.-', '&': '.-...', ':': '---...', ';': '-.-.-.', \n",
" '=': '-...-', '+': '.-.-.', '-': '-....-', '_': '..--.-', \n",
" '\"': '.-..-.', '$': '...-..-', '@': '.--.-.', ' ': '/'\n",
"}\n",
"\n",
"# Function to decode Morse code to text\n",
"def decode_from_morse(morse_code):\n",
" reversed_dict = {v: k for k, v in MORSE_CODE_DICT.items()}\n",
" decoded_message = []\n",
" for code in morse_code.split(' '):\n",
" if code in reversed_dict:\n",
" decoded_message.append(reversed_dict[code])\n",
" elif code == '/': # Morse code for space\n",
" decoded_message.append(' ')\n",
" else:\n",
" decoded_message.append('?') # Unknown Morse code\n",
" return ''.join(decoded_message)\n",
"\n",
"print(\"Enter the Morse code to decode:\")\n",
"text = input()\n",
"decoded_text = decode_from_morse(text)\n",
"print(\"Each Morse code character is separated by a space, and each word is separated by a /\")\n",
"print(f\"The decoded text for '{text}' is:\")\n",
"print(decoded_text)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Expected Output\n",
"\n",
"When you run this program, this is how the Morse code `.... . .-.. .-.. --- / .- -. -.. / .-- . .-.. -.-. --- -- . / - --- / ... .--. .-.. .- ... .... -.- .. - --..-- / .. - ... / --. .-. . .- - / - --- / .... .- ...- . / -.-- --- ..- .-.-.-` would be decoded back to text:\n",
"\n",
"```shell\n",
"Enter the Morse code to decode:\n",
".... . .-.. .-.. --- / .- -. -.. / .-- . .-.. -.-. --- -- . / - --- / ... .--. .-.. .- ... .... -.- .. - --..-- / .. - ... / --. .-. . .- - / - --- / .... .- ...- . / -.-- --- ..- .-.-.-\n",
"Each Morse code character is separated by a space, and each word is separated by a /\n",
"The decoded text for '.... . .-.. .-.. --- / .- -. -.. / .-- . .-.. -.-. --- -- . / - --- / ... .--. .-.. .- ... .... -.- .. - --..-- / .. - ... / --. .-. . .- - / - --- / .... .- ...- . / -.-- --- ..- .-.-.-' is:\n",
"HELLO AND WELCOME TO SPLASHKIT, ITS GREAT TO HAVE YOU.\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Applications of Morse Code\n",
"\n",
"Morse code remains relevant in various fields due to its simplicity and reliability. Some of its applications include:\n",
"\n",
"- **Emergency Communications**: Using short and long signals to communicate distress (e.g., SOS: `... --- ...`).\n",
"- **Amateur Radio**: Sending messages over long distances where voice communication may be impractical.\n",
"- **Aviation**: Identifying navigational aids with Morse code signals.\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "jupyter-books",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit f53bd3e

Please sign in to comment.