Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import numpy as np
- import math
- alphabet = 'abcdefghijklmnopqrstuvwxyz '
- def func_c(i):
- return alphabet.index(i)
- def alphabet_coded():
- res = []
- for i in alphabet:
- res.append(func_c(i))
- return res
- coded_alphabet = alphabet_coded()
- def generate_key(num):
- res = []
- result = []
- for i in range(3):
- res.append(random.choice(range(len(alphabet)+1)))
- for i in range(3, num + 1):
- res.append((res[i - 1] + res[i - 3]) % len(alphabet))
- for i in range(len(res) - 1):
- result.append((res[i] + res[i + 1]) % len(alphabet))
- return result
- def cypher(message, key):
- res = []
- # key = generate_key(len(message))
- for i in range(len(message)):
- res.append((func_c(message[i]) + key[i]) % len(alphabet))
- print(message)
- print(key)
- # print(res)
- return res
- def decypher(coded_message, key):
- res = ''
- for i in range(len(coded_message)):
- res += alphabet[(coded_message[i] + len(alphabet) - key[i]) % len(alphabet)]
- return res
- def main():
- # message = input('Enter the message: ')
- f = open('Input.txt')
- message = f.read()
- # print(message)
- # print(alphabet)
- for i in alphabet:
- print(i, end = " ")
- print()
- print(coded_alphabet)
- key = generate_key(len(message))
- coded = cypher(message, key)
- print(coded)
- decoded = decypher(coded, key)
- print(decoded)
- if __name__ == '__main__':
- main()
- alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIKLMNOPQRSTUVWXYZ .,!:;*'"
- key_size = 0
- def encrypt(text,keyword):
- keyword_matrix = keyword_to_matrix(keyword)
- keyword_matrix = np.array(keyword_matrix)
- text_matrix = np.array(text_to_matrix(text))
- result = [] # final result
- for i in range(len(text_matrix)):
- row = [] # the new row in new matrix
- for j in range(len(keyword_matrix)):
- product = 0 # the new element in the new row
- for v in range(len(text_matrix[i])):
- product += (text_matrix[i][v] * keyword_matrix[v][j])
- row.append(product ) # append sum of product into the new row
- result.append(row) # append the new row into the final result
- result= np.array(result)
- result %=len(alphabet)
- print(keyword_matrix,'\n')
- print(text_matrix,'\n')
- print(result,'\n')
- f = open("out.txt",'w')
- for i in result:
- for j in i:
- f.write(alphabet[j])
- f.close()
- #print(result)
- def decode(keyword):
- keyword_matrix = keyword_to_matrix(keyword)
- keyword_matrix = np.array(keyword_matrix)
- text = read_file("out.txt")
- text_matrix = np.array(text_to_matrix(text))
- keyword_matrix_inv = np.linalg.inv(keyword_matrix)
- # print(np.matmul(keyword_matrix, keyword_matrix_inv))
- #result = np.matmul(text_matrix,keyword_matrix_inv)
- result = [] # final result
- for i in range(len(text_matrix)):
- row = [] # the new row in new matrix
- for j in range(len(keyword_matrix_inv)):
- product = 0 # the new element in the new row
- for v in range(len(text_matrix[i])):
- product += (text_matrix[i][v] * keyword_matrix_inv[v][j]) % len(alphabet)
- row.append(product % len(alphabet)) # append sum of product into the new row
- result.append(row)
- print(np.linalg.det(keyword_matrix))
- print(text_matrix,'\n')
- print(keyword_matrix, '\n')
- print(keyword_matrix_inv,'\n')
- print(result,'\n')
- #print(np.matmul(keyword_matrix,keyword_matrix_inv))
- #print(len(alphabet))
- #result = math.floor(result)
- f = open("result.txt",'w')
- result = np.floor(result)
- print(result, '\n')
- for i in result:
- for j in i:
- f.write(alphabet[int(j)])
- def text_to_matrix(text):
- size = len(text)
- matrix = []
- row = []
- count = 0
- for i in range(size//key_size):
- for j in range(key_size):
- row.append(alphabet.index(text[count]))
- count += 1
- matrix.append(row)
- row = []
- return matrix
- def keyword_to_matrix(keyword):
- global key_size
- key_size = int(len(keyword) ** (1 / 2))
- key_matrix = []
- row = []
- count = 0
- for i in range(key_size):
- for j in range(key_size):
- row.append(alphabet.index(keyword[count]))
- count += 1
- key_matrix.append(row)
- row = []
- return key_matrix
- def read_file(path):
- f = open(path, "r")
- matrix = []
- text = ''
- lines = f.readlines()
- for i in lines:
- text += i
- text = text.replace('\n', ' ')
- return text
- def main():
- keyword = "qwertyuio"
- text = read_file('text.txt')
- encrypt(text,keyword)
- decode(keyword)
- if __name__=="__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement