cipher encoder in python using dictionaries
<p>I'm trying to practice using dictionaries and functions on python. I am trying to write a program that encrypts a simple phrase or sentence with an encrypted alphabet: </p>

<pre><code>Original alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Encrypted alphabet: TDLOFAGJKRICVPWUXYBEZQSNMH. 
</code></pre>

<p>The user is to enter the phrase or sentence to be encrypted so it could contain upper or lowercase alphabet, spaces, commas, and periods. The output, though, should only be uppercase letters with spaces and punctuation marks. </p>

<pre><code>cipher ={"A":"T","a":"T","B":"D","b":"D","C":"L","c":"L","D":"O","d":"O","E":"F","e":"F","F":"A","f":"A","G":"G","g":"G","H":"J","h":"J","I":"K","i":"K","J":"R","j":"R","K":"I","k":"I","L":"C","l":"C","M":"V","m":"V","N":"P","n":"P","O":"W","o":"W","P":"U","p":"U","Q":"X","q":"X","R":"Y","r":"Y","S":"B","s":"B","T":"E","t":"E","U":"Z","u":"Z","V":"Q","v":"Q","W":"S","w":"S","X":"N","x":"N","Y":"M","y":"M","Z":"H","z":"H"}

def encode(str,cipher):
    result=""
    for c in str:
        result = result + cipher[c]
    return result
</code></pre>

<p>Sometimes it's better to ask forgiveness then permission. You could remove half your key: values and replace them with str.upper(), that way small letters become big letters. If you call dict().get(key) you get the value of the key or you get None. The or operator between the dict and character evaluates to the character if the cipher.get(character) returns None. </p>

<pre><code>def get_chiper(plaintext):
    cipher = {"A": "T", "B": "D", "C": "L", "D": "O", "E": "F",
              "F": "A", "G": "G", "H": "J", "I": "K", "J": "R",
              "K": "I", "L": "C", "M": "V", "N": "P", "O": "W",
              "P": "U", "Q": "X", "R": "Y", "S": "B", "T": "E",
              "U": "Z", "V": "Q", "W": "S", "X": "N", "Y": "M",
              "Z": "H"}
    return "".join(cipher.get(character.upper()) or character
               for character in plaintext)
</code></pre>

<hr>

<p>And the full encoding and decoding could be done with the same function by reversing the dict. </p>

<pre><code>def encode(plaintext, cipher):
    return "".join(cipher.get(character.upper()) or character
                   for character in plaintext)


def decode(secret, encoding_cipher):
    decode_cipher = {value: key for key, value in encoding_cipher.items()}
    return encode(secret, decode_cipher)


def main():
    cipher = {"A": "T", "B": "D", "C": "L", "D": "O", "E": "F",
              "F": "A", "G": "G", "H": "J", "I": "K", "J": "R",
              "K": "I", "L": "C", "M": "V", "N": "P", "O": "W",
              "P": "U", "Q": "X", "R": "Y", "S": "B", "T": "E",
              "U": "Z", "V": "Q", "W": "S", "X": "N", "Y": "M",
              "Z": "H"}

    plaintext = "hello foo from bar"
    secret = encode(plaintext, cipher)
    plaintext = decode(secret, cipher)
    print(secret, plaintext)


if __name__ == '__main__':
    main()
</code></pre>

python dictionary encryption