Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

vigenere cipher with all printable characters 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!!
Comment

PREVIOUS NEXT
Code Example
Python :: function to perform pairs bootstrap estimates on linear regression parameters 
Python :: find the sitepckages for anaconda 
Python :: matplotlib temperature celsius 
Python :: analog of join in pathlibn 
Python :: df describe 
Python :: Does np.tile Work in More Than 2 Dimensions 
Python :: calculate area under the curve in python 
Python :: dockerize django app 
Python :: list comprehensions 
Python :: Is there a do ... until in Python 
Python :: instalar sympy en thonny 
Python :: python status code to string 
Python :: Missing data counts and percentage 
Python :: os.path.dirname(__file__) 
Python :: django form label in template 
Python :: gtts python 
Python :: install requests-html in jupyter notebook 
Python :: seaborn boxplot change filling 
Python :: selenium proxy with authentication 
Python :: django set cookie 
Python :: Python NumPy squeeze function Example 
Python :: You will be provided a file path for input I, a file path for output O, a string S, and a string T. 
Python :: activate python venv in windows 
Python :: how to check mix types in pandas column 
Python :: pandas fillna with mode 
Python :: group a dataset 
Python :: save model with best validation loss keras 
Python :: how to get the memory location of a varible in python 
Python :: python code to increase cpu utilization 
Python :: how to connect ip camera to opencv python 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =