Instantly share code, notes, and snippets.

@dssstr dssstr/vigenere.py
Last active Nov 27, 2018

Embed
What would you like to do?
Simple Vigenere Cipher written in Python 3.5.
def encrypt(plaintext, key):
key_length = len(key)
key_as_int = [ord(i) for i in key]
plaintext_int = [ord(i) for i in plaintext]
ciphertext = ''
for i in range(len(plaintext_int)):
value = (plaintext_int[i] + key_as_int[i % key_length]) % 26
ciphertext += chr(value + 65)
return ciphertext
def decrypt(ciphertext, key):
key_length = len(key)
key_as_int = [ord(i) for i in key]
ciphertext_int = [ord(i) for i in ciphertext]
plaintext = ''
for i in range(len(ciphertext_int)):
value = (ciphertext_int[i] - key_as_int[i % key_length]) % 26
plaintext += chr(value + 65)
return plaintext
@flipperbw

This comment has been minimized.

Copy link

flipperbw commented Jan 3, 2018

I think there are limitations here with lower case and capital letters. You'd need to check for .lower(), and also simply pass the character through if it doesn't match A-Z.

I wrote one that handles all default ASCII characters (95):

def vig(txt='', key='', typ='d'):
    if not txt:
        print 'Needs text'
        return
    if not key:
        print 'Needs key'
        return
    if typ not in ('d', 'e'):
        print 'Type must be "d" or "e"'
        return

    k_len = len(key)
    k_ints = [ord(i) for i in key]
    txt_ints = [ord(i) for i in txt]
    ret_txt = ''
    for i in range(len(txt_ints)):
        adder = k_ints[i % k_len]
        if typ == 'd':
            adder *= -1

        v = (txt_ints[i] - 32 + adder) % 95

        ret_txt += chr(v + 32)

    print ret_txt
    return ret_txt

For example:

q = vig('Let\'s check this out!', 'super secret key', 'e')
`{&-'@wnivq55to.4&'z4
vig(q, 'super secret key', 'd')
Let's check this out!
@flipperbw

This comment has been minimized.

Copy link

flipperbw commented Jan 3, 2018

Okay, even better:

universe = [c for c in (chr(i) for i in range(32,127))]

uni_len = len(universe)

def vign(txt='', key='', typ='d'):
    if not txt:
        print 'Needs text.'
        return
    if not key:
        print 'Needs key.'
        return
    if typ not in ('d', 'e'):
        print 'Type must be "d" or "e".'
        return
    if any(t not in universe for t in key):
        print 'Invalid characters in the key. Must only use ASCII symbols.'
        return

    ret_txt = ''
    k_len = len(key)

    for i, l in enumerate(txt):
        if l not in universe:
            ret_txt += l
        else:
            txt_idx = universe.index(l)

            k = key[i % k_len]
            key_idx = universe.index(k)
            if typ == 'd':
                key_idx *= -1

            code = universe[(txt_idx + key_idx) % uni_len]

            ret_txt += code

    print ret_txt
    return ret_txt
@PwnKitteh

This comment has been minimized.

Copy link

PwnKitteh commented Mar 6, 2018

@flipperbw ,
I'm trying to make a similar program. Would you mind reposting your code with comments; I'm having a bit of a hard time following it.
Thanks.

@deed02392

This comment has been minimized.

Copy link

deed02392 commented May 1, 2018

I implemented this some years ago, along with a tabula recta generator so you can do it by hand (for fun!)

https://github.com/deed02392/vigenere

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment