# Ciphers
Python module for implementing ciphers
## Installation
Install using
```powershell
pip install ciphers_module
```
Import using
```python
import ciphers_module
```
## Methods
### Get Letter Value
Gets the A-Z value of a character
```python
def get_letter_value(letter: str) -> int:
'''Get the value of an English letter (A = 0, B = 1, C = 2 ...)'''
return ord(letter) - 65
```
To Run
```python
get_letter_value(letter)
```
### Get Letter From Value
Gets the character from an A-Z value
```python
def get_letter_from_value(value: int) -> str:
'''Get the English letter from a value (A = 0, B = 1, C = 2 ...)'''
return chr(value + 65)
```
To Run
```python
get_letter_from_value(letter)
```
### Caeser Cipher
Performs a Caeser Cipher on a given string with given shift
Negative shift means shifting left
```python
def caeser_cipher(text: str, shift: int, decode: bool = False) -> str:
'''
Caeser Cipher\n
Shifts {text} {shift} amount in positive/negative direction (Right/Left respectively)\n
Set {decode} to True to decode {text} with shift {shift}
'''
# Make everything Upper Case
text.upper()
# Begin
result: str = ""
for letter in text:
# Get Value of each Letter
value: int = get_letter_value(letter)
# Get Letter from Value
result += get_letter_from_value(value + shift) if not decode else get_letter_from_value(value - shift) # Handle Decoding
return result
```
To Run
```python
caeser_cipher(text, shift, decode) # decode is automatically false
```
### Vigenere Cipher
Performs a Vigenere Cipher on a given string with given key
```python
def vigenere_cipher(text: str, key: str, decode: bool = False) -> str:
'''
Vigenere Cipher\n
Uses a Vigenere Cipher on {text} with key {key}\n
Set {decode} to True to decode {text} with key {key}
'''
# Make everything Upper Case
text.upper()
key.upper()
# Make Lists of Characters
text_lst: list[str] = list(text)
key_lst: list[str] = list(key)
# Edit Length of Key
if len(key_lst) < len(text_lst):
key_lst = list(islice(cycle(key_lst), len(text_lst)))
if len(key_lst) > len(text_lst):
key_lst = key_lst[:len(key_lst)]
result: str = ""
for index, letter in enumerate(text_lst):
# Get Values of each Letter
letter_value: int = get_letter_value(letter)
key_value: int = get_letter_value(key_lst[index]) if not decode else -get_letter_value(key_lst[index]) # Handle Decoding
# Get Letter from Value
new_letter: str = get_letter_from_value((letter_value + key_value) % 26)
result += new_letter
return result
```
To Run
```python
vigenere_cipher(text, key, decode) # decode is automatically false
```
### Rail Fence Cipher
Performs a Rail Fence Cipher on a given string with given amount of Rails
```python
def rail_fence_cipher(text: str, rails: int, decode: bool = False):
'''
Rail Fence Cipher\n
Uses a Rail Fence (Zig-Zag) Cipher on {text} with {rails} rails\n
Set {decode} to True to decode {text} with {rails} rails
'''
# Make everything Upper Case
text.upper()
# Make Rail Fence
rail_fence = [[""]*len(text) for _ in range(rails)]
# Variables to move the cursor
direction = -1
row = 0
if decode: # Decoding
# Fill the rail_fence with placeholders
for col in range(len(text)):
rail_fence[row][col] = '*'
# Change direction if we've hit the top or bottom rail
if row == 0 or row == rails - 1:
direction *= -1
# Move to the next row
row += direction
# Fill the rail rail_fence with the ciphertext
i = 0
for row in range(rails):
for col in range(len(text)):
if rail_fence[row][col] == '*':
rail_fence[row][col] = text[i]
i += 1
# Extract the plaintext from the rail_fence
result = [rail_fence[row][col] for col in range(len(text)) for row in range(rails) if rail_fence[row][col] is not None]
else: # Encoding
# Fill the rail rail_fence
for col in range(len(text)):
rail_fence[row][col] = text[col]
# Change direction if we've hit the top or bottom rail
if row == 0 or row == rails - 1:
direction *= -1
# Move to the next row
row += direction
# Extract the text from the rail_fence
result = [rail_fence[row][col] for row in range(rails) for col in range(len(text)) if rail_fence[row][col] is not None]
return "".join(result)
```
To Run
```python
rail_fence_cipher(text, shift, decode) # decode is automatically false
```
Following Methods are in the ASCII class
### ASCII Decimal Cipher
Encode/Decodes a string in Decimal using ASCII notation
```python
def decimal(text: str, decode: bool = False) -> str:
'''
ASCII Decimal\n
Converts a string to and from decimal using ASCII
'''
result: str = ""
if not decode:
for letter in text:
value: str = str(ord(letter))
result += f"{value} "
return result
for number in text.split():
try:
value = chr(int(number))
result += value
except ValueError:
print("Not a number")
return result
```
To Run
```python
ascii.decimal(text, decode) # decode is automatically false
```
### ASCII Binary Cipher
Encode/Decodes a string in Binary using ASCII notation
```python
def binary(text: str, decode: bool = False) -> str:
'''
ASCII Binary\n
Converts a string to and from binary using ASCII
'''
result: str = ""
if not decode:
for letter in text:
value: str = bin(ord(letter)).removeprefix("0b")
result += f"0{value} "
return result
for byte in text.split():
try:
value = chr(int(byte, 2))
result += value
except ValueError:
print(f"Not Binary: {byte}")
return result
```
To Run
```python
ascii.binary(text, decode) # decode is automatically false
```
### ASCII Octal Cipher
Encode/Decodes a string in Octal using ASCII notation
```python
def octal(text: str, decode: bool = False) -> str:
'''
ASCII Octal\n
Converts a string to and from octal using ASCII
'''
result: str = ""
if not decode:
for letter in text:
value: str = oct(ord(letter)).removeprefix("0o")
result += f"{value} "
return result
for octa in text.split():
try:
value = chr(int(octa, 8))
result += value
except ValueError:
print(f"Not Octal: {octa}")
return result
```
To Run
```python
ascii.octal(text, decode) # decode is automatically false
```
### ASCII Hexadecimal Cipher
Encode/Decodes a string in Hexadecimal using ASCII notation
```python
def hexadecimal(text: str, decode: bool = False) -> str:
'''
ASCII Hexadecimal\n
Converts a string to and from hexadecimal using ASCII
'''
result: str = ""
if not decode:
for letter in text:
value: str = hex(ord(letter)).removeprefix("0x")
result += f"{value} "
return result
for hexa in text.split():
try:
value = chr(int(hexa, 16))
result += value
except ValueError:
print(f"Not Hexadecimal: {hexa}")
return result
```
To Run
```python
ascii.hexadecimal(text, decode) # decode is automatically false
```
### Morse Code
Encode/Decodes a string in Morse Code
```python
def morse_code(text: str, decode: bool = False) -> str:
'''
Morse Code\n
Encodes/Decodes a string in Morse Code
'''
code: dict[str, str] = {
"A": ".-",
"B": "-...",
"C": "-.-.",
"D": "-..",
"E": ".",
"F": "..-.",
"G": "--.",
"H": "....",
"I": "..",
"J": ".---",
"K": "-.-",
"L": ".-..",
"M": "--",
"N": "-.",
"O": "---",
"P": ".--.",
"Q": "--.-",
"R": ".-.",
"S": "...",
"T": "-",
"U": "..-",
"V": "...-",
"W": ".--",
"X": "-..-",
"Y": "-.--",
"Z": "--..",
"1": ".----",
"2": "..---",
"3": "...--",
"4": "....-",
"5": ".....",
"6": "-....",
"7": "--...",
"8": "---..",
"9": "----.",
"0": "-----",
" ": "/",
}
input_text: str = text.upper()
if not decode:
input_tokens: list[str] = [*input_text]
result: str = ""
for token in input_tokens:
try:
result += code[token] + " "
except KeyError:
result += token + " "
return result
input_tokens: list[str] = input_text.split()
result: str = ""
for token in input_tokens:
try:
result += list(code.keys())[list(code.values()).index(token)]
except ValueError:
result += token
return result
```
To run
```python
morse_code(text, decode) # decode is automatically false
```
Raw data
{
"_id": null,
"home_page": null,
"name": "ciphers-module",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "python, cipher, encode, encrypt, decrypt, decode",
"author": "Avyukt27",
"author_email": "<avyukt.aggarwal007@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/ec/ec/16ce7640bb12ca0ccc07f6c91b88bc1499fc27d1f70db812d808f1e7d5f4/ciphers_module-2.0.2.tar.gz",
"platform": null,
"description": "# Ciphers\r\n\r\nPython module for implementing ciphers\r\n\r\n## Installation\r\n\r\nInstall using\r\n\r\n```powershell\r\npip install ciphers_module\r\n```\r\n\r\nImport using\r\n\r\n```python\r\nimport ciphers_module\r\n```\r\n\r\n## Methods\r\n\r\n### Get Letter Value\r\n\r\nGets the A-Z value of a character\r\n\r\n```python\r\ndef get_letter_value(letter: str) -> int:\r\n '''Get the value of an English letter (A = 0, B = 1, C = 2 ...)'''\r\n return ord(letter) - 65\r\n```\r\n\r\nTo Run\r\n\r\n```python\r\nget_letter_value(letter)\r\n```\r\n\r\n### Get Letter From Value\r\n\r\nGets the character from an A-Z value\r\n\r\n```python\r\ndef get_letter_from_value(value: int) -> str:\r\n '''Get the English letter from a value (A = 0, B = 1, C = 2 ...)'''\r\n return chr(value + 65)\r\n```\r\n\r\nTo Run\r\n\r\n```python\r\nget_letter_from_value(letter)\r\n```\r\n\r\n### Caeser Cipher\r\n\r\nPerforms a Caeser Cipher on a given string with given shift\r\nNegative shift means shifting left\r\n\r\n```python\r\ndef caeser_cipher(text: str, shift: int, decode: bool = False) -> str:\r\n '''\r\n Caeser Cipher\\n\r\n Shifts {text} {shift} amount in positive/negative direction (Right/Left respectively)\\n\r\n Set {decode} to True to decode {text} with shift {shift}\r\n '''\r\n # Make everything Upper Case\r\n text.upper()\r\n\r\n # Begin\r\n result: str = \"\"\r\n for letter in text:\r\n # Get Value of each Letter\r\n value: int = get_letter_value(letter)\r\n # Get Letter from Value\r\n result += get_letter_from_value(value + shift) if not decode else get_letter_from_value(value - shift) # Handle Decoding\r\n\r\n return result\r\n```\r\n\r\nTo Run\r\n\r\n```python\r\ncaeser_cipher(text, shift, decode) # decode is automatically false\r\n```\r\n\r\n### Vigenere Cipher\r\n\r\nPerforms a Vigenere Cipher on a given string with given key\r\n\r\n```python\r\ndef vigenere_cipher(text: str, key: str, decode: bool = False) -> str:\r\n '''\r\n Vigenere Cipher\\n\r\n Uses a Vigenere Cipher on {text} with key {key}\\n\r\n Set {decode} to True to decode {text} with key {key}\r\n '''\r\n # Make everything Upper Case\r\n text.upper()\r\n key.upper()\r\n\r\n # Make Lists of Characters\r\n text_lst: list[str] = list(text)\r\n key_lst: list[str] = list(key)\r\n\r\n # Edit Length of Key\r\n if len(key_lst) < len(text_lst):\r\n key_lst = list(islice(cycle(key_lst), len(text_lst)))\r\n if len(key_lst) > len(text_lst):\r\n key_lst = key_lst[:len(key_lst)]\r\n\r\n result: str = \"\"\r\n\r\n for index, letter in enumerate(text_lst):\r\n # Get Values of each Letter\r\n letter_value: int = get_letter_value(letter)\r\n key_value: int = get_letter_value(key_lst[index]) if not decode else -get_letter_value(key_lst[index]) # Handle Decoding\r\n # Get Letter from Value\r\n new_letter: str = get_letter_from_value((letter_value + key_value) % 26)\r\n result += new_letter\r\n\r\n return result\r\n```\r\n\r\nTo Run\r\n\r\n```python\r\nvigenere_cipher(text, key, decode) # decode is automatically false\r\n```\r\n\r\n### Rail Fence Cipher\r\n\r\nPerforms a Rail Fence Cipher on a given string with given amount of Rails\r\n\r\n```python\r\ndef rail_fence_cipher(text: str, rails: int, decode: bool = False):\r\n '''\r\n Rail Fence Cipher\\n\r\n Uses a Rail Fence (Zig-Zag) Cipher on {text} with {rails} rails\\n\r\n Set {decode} to True to decode {text} with {rails} rails\r\n '''\r\n # Make everything Upper Case\r\n text.upper()\r\n \r\n # Make Rail Fence\r\n rail_fence = [[\"\"]*len(text) for _ in range(rails)]\r\n\r\n # Variables to move the cursor\r\n direction = -1\r\n row = 0\r\n\r\n if decode: # Decoding\r\n # Fill the rail_fence with placeholders\r\n for col in range(len(text)):\r\n rail_fence[row][col] = '*'\r\n\r\n # Change direction if we've hit the top or bottom rail\r\n if row == 0 or row == rails - 1:\r\n direction *= -1\r\n\r\n # Move to the next row\r\n row += direction\r\n\r\n # Fill the rail rail_fence with the ciphertext\r\n i = 0\r\n for row in range(rails):\r\n for col in range(len(text)):\r\n if rail_fence[row][col] == '*':\r\n rail_fence[row][col] = text[i]\r\n i += 1\r\n\r\n # Extract the plaintext from the rail_fence\r\n result = [rail_fence[row][col] for col in range(len(text)) for row in range(rails) if rail_fence[row][col] is not None]\r\n\r\n else: # Encoding\r\n # Fill the rail rail_fence\r\n for col in range(len(text)):\r\n rail_fence[row][col] = text[col]\r\n\r\n # Change direction if we've hit the top or bottom rail\r\n if row == 0 or row == rails - 1:\r\n direction *= -1\r\n\r\n # Move to the next row\r\n row += direction\r\n\r\n # Extract the text from the rail_fence\r\n result = [rail_fence[row][col] for row in range(rails) for col in range(len(text)) if rail_fence[row][col] is not None]\r\n\r\n return \"\".join(result)\r\n```\r\n\r\nTo Run\r\n\r\n```python\r\nrail_fence_cipher(text, shift, decode) # decode is automatically false\r\n```\r\n\r\nFollowing Methods are in the ASCII class\r\n\r\n### ASCII Decimal Cipher\r\n\r\nEncode/Decodes a string in Decimal using ASCII notation\r\n\r\n```python\r\ndef decimal(text: str, decode: bool = False) -> str:\r\n '''\r\n ASCII Decimal\\n\r\n Converts a string to and from decimal using ASCII\r\n '''\r\n result: str = \"\"\r\n if not decode:\r\n for letter in text:\r\n value: str = str(ord(letter))\r\n result += f\"{value} \"\r\n return result\r\n for number in text.split():\r\n try:\r\n value = chr(int(number))\r\n result += value\r\n except ValueError:\r\n print(\"Not a number\")\r\n return result\r\n```\r\n\r\nTo Run\r\n\r\n```python\r\nascii.decimal(text, decode) # decode is automatically false\r\n```\r\n\r\n### ASCII Binary Cipher\r\n\r\nEncode/Decodes a string in Binary using ASCII notation\r\n\r\n```python\r\ndef binary(text: str, decode: bool = False) -> str:\r\n '''\r\n ASCII Binary\\n\r\n Converts a string to and from binary using ASCII\r\n '''\r\n result: str = \"\"\r\n if not decode:\r\n for letter in text:\r\n value: str = bin(ord(letter)).removeprefix(\"0b\")\r\n result += f\"0{value} \"\r\n return result\r\n for byte in text.split():\r\n try:\r\n value = chr(int(byte, 2))\r\n result += value\r\n except ValueError:\r\n print(f\"Not Binary: {byte}\")\r\n return result\r\n```\r\n\r\nTo Run\r\n\r\n```python\r\nascii.binary(text, decode) # decode is automatically false\r\n```\r\n\r\n### ASCII Octal Cipher\r\n\r\nEncode/Decodes a string in Octal using ASCII notation\r\n\r\n```python\r\ndef octal(text: str, decode: bool = False) -> str:\r\n '''\r\n ASCII Octal\\n\r\n Converts a string to and from octal using ASCII\r\n '''\r\n result: str = \"\"\r\n if not decode:\r\n for letter in text:\r\n value: str = oct(ord(letter)).removeprefix(\"0o\")\r\n result += f\"{value} \"\r\n return result\r\n for octa in text.split():\r\n try:\r\n value = chr(int(octa, 8))\r\n result += value\r\n except ValueError:\r\n print(f\"Not Octal: {octa}\")\r\n return result\r\n```\r\n\r\nTo Run\r\n\r\n```python\r\nascii.octal(text, decode) # decode is automatically false\r\n```\r\n\r\n### ASCII Hexadecimal Cipher\r\n\r\nEncode/Decodes a string in Hexadecimal using ASCII notation\r\n\r\n```python\r\ndef hexadecimal(text: str, decode: bool = False) -> str:\r\n '''\r\n ASCII Hexadecimal\\n\r\n Converts a string to and from hexadecimal using ASCII\r\n '''\r\n result: str = \"\"\r\n if not decode:\r\n for letter in text:\r\n value: str = hex(ord(letter)).removeprefix(\"0x\")\r\n result += f\"{value} \"\r\n return result\r\n for hexa in text.split():\r\n try:\r\n value = chr(int(hexa, 16))\r\n result += value\r\n except ValueError:\r\n print(f\"Not Hexadecimal: {hexa}\")\r\n return result\r\n```\r\n\r\nTo Run\r\n\r\n```python\r\nascii.hexadecimal(text, decode) # decode is automatically false\r\n```\r\n\r\n### Morse Code\r\n\r\nEncode/Decodes a string in Morse Code\r\n\r\n```python\r\ndef morse_code(text: str, decode: bool = False) -> str:\r\n '''\r\n Morse Code\\n\r\n Encodes/Decodes a string in Morse Code\r\n '''\r\n code: dict[str, str] = {\r\n \"A\": \".-\",\r\n \"B\": \"-...\",\r\n \"C\": \"-.-.\",\r\n \"D\": \"-..\",\r\n \"E\": \".\",\r\n \"F\": \"..-.\",\r\n \"G\": \"--.\",\r\n \"H\": \"....\",\r\n \"I\": \"..\",\r\n \"J\": \".---\",\r\n \"K\": \"-.-\",\r\n \"L\": \".-..\",\r\n \"M\": \"--\",\r\n \"N\": \"-.\",\r\n \"O\": \"---\",\r\n \"P\": \".--.\",\r\n \"Q\": \"--.-\",\r\n \"R\": \".-.\",\r\n \"S\": \"...\",\r\n \"T\": \"-\",\r\n \"U\": \"..-\",\r\n \"V\": \"...-\",\r\n \"W\": \".--\",\r\n \"X\": \"-..-\",\r\n \"Y\": \"-.--\",\r\n \"Z\": \"--..\",\r\n \"1\": \".----\",\r\n \"2\": \"..---\",\r\n \"3\": \"...--\",\r\n \"4\": \"....-\",\r\n \"5\": \".....\",\r\n \"6\": \"-....\",\r\n \"7\": \"--...\",\r\n \"8\": \"---..\",\r\n \"9\": \"----.\",\r\n \"0\": \"-----\",\r\n \" \": \"/\",\r\n }\r\n \r\n input_text: str = text.upper()\r\n if not decode:\r\n input_tokens: list[str] = [*input_text]\r\n\r\n result: str = \"\"\r\n for token in input_tokens:\r\n try:\r\n result += code[token] + \" \"\r\n except KeyError:\r\n result += token + \" \"\r\n \r\n return result\r\n \r\n input_tokens: list[str] = input_text.split()\r\n\r\n result: str = \"\"\r\n for token in input_tokens:\r\n try:\r\n result += list(code.keys())[list(code.values()).index(token)]\r\n except ValueError:\r\n result += token\r\n\r\n return result\r\n```\r\n\r\nTo run\r\n\r\n```python\r\nmorse_code(text, decode) # decode is automatically false\r\n```\r\n",
"bugtrack_url": null,
"license": null,
"summary": null,
"version": "2.0.2",
"project_urls": null,
"split_keywords": [
"python",
" cipher",
" encode",
" encrypt",
" decrypt",
" decode"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "344f11afc4ccd8927c80e89e97c1d02ba41776e68d2c21a5be721ad19542737e",
"md5": "c9854aa063bfd5924b2d02e7e5314f3b",
"sha256": "0eae33be792d02ed1be973ad249c54c97d1026d6ea8f415bc6e6899e3fadda65"
},
"downloads": -1,
"filename": "ciphers_module-2.0.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c9854aa063bfd5924b2d02e7e5314f3b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 11863,
"upload_time": "2024-07-08T08:13:30",
"upload_time_iso_8601": "2024-07-08T08:13:30.310126Z",
"url": "https://files.pythonhosted.org/packages/34/4f/11afc4ccd8927c80e89e97c1d02ba41776e68d2c21a5be721ad19542737e/ciphers_module-2.0.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ecec16ce7640bb12ca0ccc07f6c91b88bc1499fc27d1f70db812d808f1e7d5f4",
"md5": "f8ab622ff91c702125e0d56b5b2c5773",
"sha256": "853bc0c3a3ccd2576d12188fb114507aa32e5ce136b97f3864c421a37bba2fa7"
},
"downloads": -1,
"filename": "ciphers_module-2.0.2.tar.gz",
"has_sig": false,
"md5_digest": "f8ab622ff91c702125e0d56b5b2c5773",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4373,
"upload_time": "2024-07-08T08:13:33",
"upload_time_iso_8601": "2024-07-08T08:13:33.141481Z",
"url": "https://files.pythonhosted.org/packages/ec/ec/16ce7640bb12ca0ccc07f6c91b88bc1499fc27d1f70db812d808f1e7d5f4/ciphers_module-2.0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-08 08:13:33",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "ciphers-module"
}