Advertisement
SportyScripter

Hill_cipher

Jan 17th, 2025
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | Source Code | 0 0
  1. tekst_jawny = "Najlepsze kasztany rosna na placu pigale latem"
  2. # key = "okon"
  3.  
  4.  
  5. def create_key_matrix(tekst_jawny):
  6.     key = tekst_jawny[0:4].lower()
  7.     alphabet = "abcdefghijklmnopqrstuvwxyz"
  8.     rows, cols = len(key) / 2, len(key) / 2
  9.     index = 0
  10.     matrix_key = [[0 for _ in range(int(cols))] for _ in range(int(rows))]
  11.     for i in range(int(rows)):
  12.         for j in range(int(cols)):
  13.             matrix_key[i][j] = alphabet.index(key[index])
  14.             index += 1
  15.     return matrix_key
  16.  
  17.  
  18. def create_cipher_matrix(tekst_jawny):
  19.     matrix = create_key_matrix(tekst_jawny)
  20.     alphabet = "abcdefghijklmnopqrstuvwxyz"
  21.     encrypted_tekst = ""
  22.     tekst_jawny = tekst_jawny.replace(" ", "").lower()
  23.     index = 0
  24.     print(len(tekst_jawny))
  25.     while index + 1 < len(tekst_jawny):
  26.         j = 0
  27.         for row in range(2):
  28.             print(row)
  29.             a = alphabet.index(tekst_jawny[index])
  30.             b = alphabet.index(tekst_jawny[index + 1])
  31.             value = ((matrix[row][j] * a) + (matrix[row][j + 1] * b)) % 26
  32.             print(
  33.                 f"{matrix[row][j]} * {a} + {matrix[row][j+1]} * {b} % 26 = {value} litera a = {tekst_jawny[index]} litera b = {tekst_jawny[index+1]}"
  34.             )
  35.             encrypted_tekst += alphabet[value]
  36.         index += 2
  37.  
  38.     return encrypted_tekst
  39.  
  40.  
  41. matrix_text = create_matrix(tekst_jawny)
  42. print(matrix_text)
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement