Search code examples
pythonpython-3.xcaesar-cipher

How to replace letters with numbers and re-convert at anytime (Caesar cipher)?


I've been coding this for almost 2 days now but cant get it. I've coded two different bits trying to find it.

Code #1 So this one will list the letters but wont change it to the numbers (a->1, b->2, ect)

import re
text = input('Write Something- ')
word = '{}'.format(text)
for letter in word:
    print(letter)
    #lists down

Outcome-
Write something- test
t
e
s
t

Then I have this code that changes the letters into numbers, but I haven't been able to convert it back into letters. Code #2

u = input('Write Something')
a = ord(u[-1])
print(a)
#converts to number and prints ^^
enter code here
print('')
print(????)
#need to convert from numbers back to letters.

Outcome:
Write Something- test
116

How can I send a text through (test) and make it convert it to either set numbers (a->1, b->2) or random numbers, save it to a .txt file and be able to go back and read it at any time?


Solution

  • What youre trying to achieve here is called "caesar encryption".

    You for example say normally you would have: A=1, a=2, B=3, B=4, etc...

    then you would have a "key" which "shifts" the letters. Lets say the key is "3", so you would shift all letters 3 numbers up and you would end up with: A=4, a=5, B=6, b=7, etc...

    This is of course only ONE way of doing a caesar encryption. This is the most basic example. You could also say your key is "G", which would give you:

    A=G, a=g, B=H, b=h, etc.. or
    A=G, a=H, B=I, b=J, etc...

    Hope you understand what im talking about. Again, this is only one very simple example way.

    Now, for your program/script you need to define this key. And if the key should be variable, you need to save it somewhere (write it down). Put your words in a string, and check and convert each letter and write it into a new string.

    You then could say (pseudo code!):

    var key = READKEYFROMFILE;
    string old = READKEYFROMFILE_OR_JUST_A_NORMAL_STRING_:)
    string new = "";
    
    for (int i=0, i<old.length, i++){
    get the string at i;
    compare with your "key";
    shift it;
    write it in new;
    }
    

    Hope i could help you.

    edit:

    You could also use a dictionary (like the other answer says), but this is a very static (but easy) way.

    Also, maybe watch some guides/tutorials on programming. You dont seem to be that experienced. And also, google "Caesar encryption" to understand this topic better (its very interesting).

    edit2:

    Ok, so basically:

    You have a variable, called "key" in this variable, you store your key (you understood what i wrote above with the key and stuff?)

    You then have a string variable, called "old". And another one called "new".

    In old, you write your string that you want to convert. New will be empty for now.

    You then do a "for loop", which goes as long as the ".length" of your "old" string. (that means if your sentence has 15 letters, the loop will go through itself 15 times and always count the little "i" variable (from the for loop) up).

    You then need to try and get the letter from "old" (and save it for short in another vairable, for example char temp = "" ).

    After this, you need to compare your current letter and decide how to shift it. If thats done, just add your converted letter to the "new" string.

    Here is some more precise pseudo code (its not python code, i dont know python well), btw char stands for "character" (letter):

    var key = g;
    string old =  "teststring";
    string new = "";
    char oldchar = "";
    char newchar = "";
    
    for (int i=0; i<old.length; i++){
    oldchar = old.charAt[i];
    newchar = oldchar //shift here!!!
    new.addChar(newchar);
    }
    

    Hope i could help you ;)

    edit3:

    maybe also take a look at this:

    https://inventwithpython.com/chapter14.html

    Caesar Cipher Function in Python

    https://www.youtube.com/watch?v=WXIHuQU6Vrs