Poison Cryptography: Encoding and Decoding
Introduction
Poison Cryptography is a simple encryption technique that involves substituting characters to encode and decode messages. In this example, we'll walk through both encoding and decoding processes using Python.
Code 1: Decoding (Decrypting)
Functionality
Code 1 includes functions for decoding messages encoded with Poison Cryptography:
- Poisonal: This function takes an encoded message and a decryption key as input. It reverses the Poison Cryptography by substituting characters and numbers with their original values, ultimately returning the original text.
- ReversePoison: Responsible for reversing the Poison Cryptography by mapping characters back to their original values.
Example
import string
def Poisonal(text, key):
dec = []
start = ord('a')
m = len(key)
for i in range(len(text)):
l = text[i]
k = key[i % m]
shift = ord(k) - start
pos = start + (ord(l) - start - shift) % 26 # Reverse the shift
dec.append(chr(pos))
return ''.join(dec)
def ReversePoison(S):
D = ""
poisonChars = "azbycxdwevfugthsirjqkplomn"
poisonNums = "0918273645"
for c in S:
if c in poisonChars:
o = poisonChars.index(c)
D += string.ascii_lowercase[o]
elif c in poisonNums:
o = poisonNums.index(c)
D += str(o)
else:
D += c
return D
encoded_text = "xdislqwqlqlchb"
key = "hhej"
# Decrypt using Poisonal
decrypted_text = Poisonal(encoded_text, key)
# Reverse the Poison Cryptography
original_text = ReversePoison(decrypted_text)
print("Decoded Text is:", original_text)
Code 2: Encoding (Encrypting)
Functionality
Code 2 focuses on encoding messages using Poison Cryptography:
- Poisonal: Similar to Code 1, this function is used for character substitution during the encryption process.
- Poison: The
Poisonfunction in Code 2 takes user input, encodes it using Poison Cryptography, generates a random key for encryption, and returns both the encoded message and the key.
Example
import string, random
def Poisonal(text, key):
cip = []
start = ord('a')
m = len(key)
for i in range(len(text)):
l = text[i]
k = key[i % m]
shift = ord(k) - start
pos = start + (ord(l) - start + shift) % 26
cip.append(chr(pos))
return ''.join(cip)
def Poison(S):
E = ""
poisonChars = "azbycxdwevfugthsirjqkplomn"
poisonNums = "0918273645"
Key = ''.join(random.choices(string.ascii_lowercase, k=4))
for c in S:
if c in string.ascii_lowercase:
o = ord(c) - 97
E += poisonChars[o]
elif c in string.digits:
o = ord(c) - 48
E += poisonNums[o]
else:
E += c
Encode = Poisonal(E, Key)
return Encode, Key
String = input("Enter Text to Encode: ")
Encode, Key = Poison(String.lower())
print("Encoded Message:", Encode)
print("Encryption Key:", Key)
Summary
- Decoding (Code 1): Reverse Poison Cryptography to decrypt an encoded message using a given decryption key.
- Encoding (Code 2): Encode a message using Poison Cryptography, generating a random key for encryption.
By combining these two code snippets, you can understand the fundamentals of Poison Cryptography, including both encryption and decryption processes.