Untitled
unknown
plain_text
2 years ago
2.4 kB
5
Indexable
# Python code to implement
# Vigenere Cipher
# This function generates the
# key in a cyclic manner until
# it's length isn't equal to
# the length of original text
def generateKey(string, key):
key = list(key)
if len(string) == len(key):
return(key)
else:
for i in range(len(string) -
len(key)):
key.append(key[i % len(key)])
return("" . join(key))
# This function returns the
# encrypted text generated
# with the help of the key
def cipherText(string, key):
cipher_text = []
for i in range(len(string)):
print(ord(string[i]))
print(ord(key[i]))
x = (ord(string[i]) + ord(key[i])) % 26
x += ord('A')
cipher_text.append(chr(x))
return("" . join(cipher_text))
# This function decrypts the
# encrypted text and returns
# the original text
def originalText(cipher_text, key):
orig_text = []
for i in range(len(cipher_text)):
x = (ord(cipher_text[i]) - ord(key[i]) + 256) % 256
#x += ord('A')
orig_text.append(chr(x))
return("" . join(orig_text))
def findKeyword(plaintext, ciphertext):
# Assicurati che il plaintext e il ciphertext abbiano la stessa lunghezza
if len(plaintext) != len(ciphertext):
return "Errore: Il plaintext e il ciphertext devono avere la stessa lunghezza."
keyword = ""
# Calcola la keyword carattere per carattere
for i in range(len(plaintext)):
# Calcola la differenza tra i caratteri corrispondenti nel plaintext e nel ciphertext
diff = (ord(ciphertext[i]) - ord(plaintext[i])) % 256
# Converti la differenza nel carattere corrispondente
#char = chr(diff + ord('A'))
char = chr(diff)
# Aggiungi il carattere alla keyword
keyword += char
return keyword
# Driver code
if __name__ == "__main__":
string = "PWNX{YVR"
#keyword = "AYUSH"
#cipher_text = cipherText(string,key)
#print("Ciphertext :", cipher_text)
#print("Original/Decrypted Text :",
# originalText(cipher_text, key))
plaintext = "PWNX{YVR"
ciphertext = "±ÀÀ»íº¼Æ"
keyword = findKeyword(plaintext, ciphertext)
print("Keyword trovata:", keyword)
ciphertext = "±ÀÀ»íªÚ¾»¬ÝÒ·ÅÓìÑÁÊÜ꺼Ƣ¯æÒá©ßÁËÏÚà"
key = generateKey(ciphertext, keyword)
print(key)
print("Original/Decrypted Text :",
originalText(ciphertext, key))
# This code is contributed
# by Pratik Somwanshi
Editor is loading...
Leave a Comment