Quyidagi kod Vigenere shifrlash algoritmini Python dasturida bajarish uchun yaratilngan:
class VigenereCipher:
def __init__(self, key):
self.key = key
def encrypt(self, text):
result = ""
key_index = 0
key = self.key.upper()
for char in text:
if char.isalpha():
if char.isupper():
result += chr((ord(char) + ord(key[key_index]) - 2 * ord('A')) % 26 + ord('A'))
else:
result += chr((ord(char) + ord(key[key_index]) - 2 * ord('a')) % 26 + ord('a'))
key_index = (key_index + 1) % len(self.key)
else:
result += char
return result
def decrypt(self, text):
result = ""
key_index = 0
key = self.key.upper()
for char in text:
if char.isalpha():
if char.isupper():
result += chr((ord(char) - ord(key[key_index]) + 26) % 26 + ord('A'))
else:
result += chr((ord(char) - ord(key[key_index]) + 26) % 26 + ord('a'))
key_index = (key_index + 1) % len(self.key)
else:
result += char
return result
# Testimiz
vigenere = VigenereCipher("SECRET")
encrypted_text = vigenere.encrypt("Assalomu alaykum, dunyo!")
print("Shifrlangan matn:", encrypted_text)
decrypted_text = vigenere.decrypt(encrypted_text)
print("Qaytarilgan matn:", decrypted_text)
Kodi Vigenere Cipher klassi yaratiladi, uning konstruktorida kalit so'zi saqlanadi. Algoritmning shifrlash va qaytarilish funksiyalari ma'lumotni kiritishning keyingi qismida ishlatiladi. Shifrlash funksiyasi yordamida matn tekshiriladi, shifrlangan va kelib chiqqan indeks yordamida matnda har bir belgi shifrlangan holda qaytariladi. Qaytarish funksiyasi shifrlangan matnni oldi va matning asli qaytarildi. # Testimiz qismida, VigenereCipher klassi sinfdan obyekt yaratiladi va ma'lumotlar to'g'risida yozilgan.
Quyidagi kod Oʻrin almashtirish shifrlash algoritmini Python dasturida bajarish uchun yaratingan:
class PlayfairCipher:
def __init__(self, key):
self.key = key.upper().replace("J", "I")
self.matrix = self.generate_matrix()
def encrypt(self, text):
text = text.upper().replace("J", "I").replace(" ", "")
pairs = self.generate_pairs(text)
result = ""
for pair in pairs:
encrypted_pair = self.encrypt_pair(pair)
result += encrypted_pair
return result
def decrypt(self, text):
pairs = self.generate_pairs(text)
result = ""
for pair in pairs:
decrypted_pair = self.decrypt_pair(pair)
result += decrypted_pair
return result
def generate_matrix(self):
matrix = []
for char in self.key:
if char not in matrix:
matrix.append(char)
alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ"
for char in alphabet:
if char not in matrix:
matrix.append(char)
matrix = [matrix[i:i+5] for i in range(0, 25, 5)]
return matrix
def generate_pairs(self, text):
pairs = []
i = 0
while i < len(text):
if i == len(text) - 1:
pairs.append(text[i] + "X")
i += 1
elif text[i] == text[i+1]:
pairs.append(text[i] + "X")
i += 1
else:
pairs.append(text[i:i+2])
i += 2
return pairs
def encrypt_pair(self, pair):
char1, char2 = pair[0], pair[1]
row1, col1 = self.find_char(char1)
row2, col2 = self.find_char(char2)
if row1 == row2:
encrypted_pair = self.matrix[row1][(col1 + 1) % 5] + self.matrix[row2][(col2 + 1) % 5]
elif col1 == col2:
encrypted_pair = self.matrix[(row1 + 1) % 5][col1] + self.matrix[(row2 + 1) % 5][col2]
else:
encrypted_pair = self.matrix[row1][col2] + self.matrix[row2][col1]
return encrypted_pair
def decrypt_pair(self, pair):
char1, char2 = pair[0], pair[1]
row1, col1 = self.find_char(char1)
row2, col2 = self.find_char(char2)
if row1 == row2:
decrypted_pair = self.matrix[row1][(col1 - 1) % 5] + self.matrix[row2][(col2 - 1) % 5]
elif col1 == col2:
decrypted_pair = self.matrix[(row1 - 1) % 5][col1] + self.matrix[(row2 - 1) % 5][col2]
else:
decrypted_pair = self.matrix[row1][col2] + self.matrix[row2][col1]
return decrypted_pair
def find_char(self, char):
for row in range(5):
for col in range(5):
if self.matrix[row][col] == char:
return row, col
# Testimiz
playfair_cipher = PlayfairCipher("EXAMPLEKEY")
encrypted_text = playfair_cipher.encrypt("Assalomu alaykum, dunyo!")
print("Shifrlangan matn:", encrypted_text)
decrypted_text = playfair_cipher.decrypt(encrypted_text)