Cipher Vigenere dengan semua karakter yang dapat dicetak Python
"""
This is a very basic method for encrypting and decrypting vigenere ciphers with a given key
Includes ALL CHARACTERS within the ASCII Value range of 32-127 (All printable characters) unlike other vigenere cipher methods I've seen so far that only do uppercase/lowercase letters which is why I'm posting this
Sorry about the messy code, I'm mostly self-taught and do not have a lot of experience with Python but it works, I swear
"""
def repeat_string(a_string, target_length):
number_of_repeats = target_length // len(a_string) + 1
a_string_repeated = a_string * number_of_repeats
a_string_repeated_to_target = a_string_repeated[:target_length]
return a_string_repeated_to_target
def encrypt(key, test):
repeated_string = repeat_string(key, len(test))
finalthing = ""
asciiText = [ord(c) for c in test] # Converts each invidual character in the text into an array list of their ASCII values
asciiKey = [ord(c) for c in repeated_string]
for x in range(0, len(asciiText), 1):
asciiText[x] = asciiText[x] + asciiKey[x]
while asciiText[x] > 126: # Wraps the new value back around into the printable range of ASCII values if out of range
asciiText[x] -= 95
while asciiText[x] < 32:
asciiText[x] += 95
for x in range(0, len(asciiText), 1):
finalthing += chr(asciiText[x])
return finalthing
def decrypt(key, test): # Exact same function as encrypt() except the text is being subtracted by the key to get the original message
repeated_string = repeat_string(key, len(test)) # I could definitely just add another variable and an if statement to make this only one function but I'm far too lazy
finalthing = ""
asciiText = [ord(c) for c in test]
asciiKey = [ord(c) for c in repeated_string]
for x in range(0, len(asciiText), 1):
asciiText[x] = asciiText[x] - asciiKey[x]
while asciiText[x] > 126:
asciiText[x] -= 95
while asciiText[x] < 32:
asciiText[x] += 95
for x in range(0, len(asciiText), 1):
finalthing += chr(asciiText[x])
return finalthing
# Example
encrypted = encrypt("code", "Scissor Seven is a painfully underrated show!!")
print(encrypted) # Output: Wsnyw w&Wu{kr0ny$q%veysly|q $&sji#wgxui&wxt}%1
decrypted = decrypt("code", encrypted)
print(decrypted) # Output: Scissor Seven is a painfully underrated show!!
Thoughtful Trout