crypto/custom_encryption
This commit is contained in:
parent
f7af8a5dd3
commit
d6dcd35aa4
|
@ -0,0 +1,64 @@
|
|||
from random import randint
|
||||
import sys
|
||||
|
||||
|
||||
def generator(g, x, p):
|
||||
return pow(g, x) % p
|
||||
|
||||
|
||||
def encrypt(plaintext, key):
|
||||
cipher = []
|
||||
for char in plaintext:
|
||||
cipher.append(((ord(char) * key*311)))
|
||||
return cipher
|
||||
|
||||
|
||||
def is_prime(p):
|
||||
v = 0
|
||||
for i in range(2, p + 1):
|
||||
if p % i == 0:
|
||||
v = v + 1
|
||||
if v > 1:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
|
||||
def dynamic_xor_encrypt(plaintext, text_key):
|
||||
cipher_text = ""
|
||||
key_length = len(text_key)
|
||||
for i, char in enumerate(plaintext[::-1]):
|
||||
key_char = text_key[i % key_length]
|
||||
encrypted_char = chr(ord(char) ^ ord(key_char))
|
||||
cipher_text += encrypted_char
|
||||
return cipher_text
|
||||
|
||||
|
||||
def test(plain_text, text_key):
|
||||
p = 97
|
||||
g = 31
|
||||
if not is_prime(p) and not is_prime(g):
|
||||
print("Enter prime numbers")
|
||||
return
|
||||
a = randint(p-10, p)
|
||||
b = randint(g-10, g)
|
||||
print(f"a = {a}")
|
||||
print(f"b = {b}")
|
||||
u = generator(g, a, p)
|
||||
v = generator(g, b, p)
|
||||
key = generator(v, a, p)
|
||||
b_key = generator(u, b, p)
|
||||
shared_key = None
|
||||
if key == b_key:
|
||||
shared_key = key
|
||||
else:
|
||||
print("Invalid key")
|
||||
return
|
||||
semi_cipher = dynamic_xor_encrypt(plain_text, text_key)
|
||||
cipher = encrypt(semi_cipher, shared_key)
|
||||
print(f'cipher is: {cipher}')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
message = sys.argv[1]
|
||||
test(message, "trudeau")
|
|
@ -0,0 +1,3 @@
|
|||
a = 97
|
||||
b = 22
|
||||
cipher is: [151146, 1158786, 1276344, 1360314, 1427490, 1377108, 1074816, 1074816, 386262, 705348, 0, 1393902, 352674, 83970, 1141992, 0, 369468, 1444284, 16794, 1041228, 403056, 453438, 100764, 100764, 285498, 100764, 436644, 856494, 537408, 822906, 436644, 117558, 201528, 285498]
|
|
@ -0,0 +1,39 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
def generator(g, x, p):
|
||||
return pow(g, x) % p
|
||||
|
||||
# xor encryption is symmetric, apart
|
||||
# from the reversing part
|
||||
def dynamic_xor_decrypt(plaintext, text_key):
|
||||
cipher_text = ""
|
||||
key_length = len(text_key)
|
||||
for i, char in enumerate(plaintext):
|
||||
key_char = text_key[i % key_length]
|
||||
encrypted_char = chr(ord(char) ^ ord(key_char))
|
||||
cipher_text += encrypted_char
|
||||
return ''.join(reversed(cipher_text))
|
||||
|
||||
def decrypt(cipher, key):
|
||||
plaintext = ''
|
||||
for char in cipher:
|
||||
plaintext += chr(char // (key * 311))
|
||||
return plaintext
|
||||
|
||||
cipher = [151146, 1158786, 1276344, 1360314, 1427490, 1377108, 1074816, 1074816, 386262, 705348, 0, 1393902, 352674, 83970, 1141992, 0, 369468, 1444284, 16794, 1041228, 403056, 453438, 100764, 100764, 285498, 100764, 436644, 856494, 537408, 822906, 436644, 117558, 201528, 285498]
|
||||
|
||||
if __name__ == '__main__':
|
||||
p = 97
|
||||
g = 31
|
||||
a = 97
|
||||
b = 22
|
||||
u = generator(g, a, p)
|
||||
v = generator(g, b, p)
|
||||
key = generator(v, a, p)
|
||||
b_key = generator(u, b, p)
|
||||
assert key == b_key
|
||||
decrypted = decrypt(cipher, key)
|
||||
xor_decrypted = dynamic_xor_decrypt(decrypted, 'trudeau')
|
||||
print(xor_decrypted)
|
||||
|
||||
|
Loading…
Reference in New Issue