Forum Archive

Trying to make an encrypted message system

Spencer1221

Hey so I'm new to Python and this app is helping me learn fast. I am wanting to make a module to encrypt messages and to decrypt messages. I'm having some difficulty with solving this. So it would help if someone could have some steps to help me out. Thanks :) love this app though

ccc

Try bit flipping... https://github.com/cclauss/Ten-lines-or-less/blob/master/bit_filpper.py

JonB

Another option: translate (here's a caesar cipher from stackoverflow. )

#! Python2

import string

def caesar(plaintext, shift):
    alphabet = string.ascii_lowercase
    shifted_alphabet = alphabet[shift:] + alphabet[:shift]
    table = string.maketrans(alphabet, shifted_alphabet)
    return plaintext.translate(table)

For a simple two-way cipher, rot13 comes built in, using the encode method of str types.

>>> print "Gur rntyr syvrf ng zvqavtug".encode("rot13")
'The eagle flies at midnight'
Spencer1221

Thank you so much everyone! This has helped :)

Spencer1221

Thanks so much everyone this was a good learning experience! I made this system with your examples.

import string

char_list = list(string.ascii_letters) + list(string.digits) + list(string.punctuation)


def encrypt(message, key):
    alphabet = string.ascii_letters
    shifted_alphabet = alphabet[key:] + alphabet[:key]
    table = string.maketrans(alphabet, shifted_alphabet)
    return message.translate(table)


def decrypt(message, key):
    alphabet = string.ascii_letters
    shifted_alphabet = alphabet[-key:] + alphabet[:-key]
    table = string.maketrans(alphabet, shifted_alphabet)
    return message.translate(table)
Webmaster4o

@Spencer1221 If you put that code inside triple back ticks (`) it will show as formatted code:

```
your code here
```
Spencer1221

Oh ok thanks @Webmaster4o

JonB

now: make your code work with both upper and lowercase alphabet!