I am working on a program in Python that sort-of encrypts a message. I have converted the string (which will vary based on user input) to a list and have a number. I want to automatically change each letter in the list with the number. For example, if the number is three, every A
changes into a D
. I have a dictionary with the vales for letters, such as {"a" : 1}, {"b" : 2},
etc.
I can not seem to get how I could change the letters (without knowing what they are) and possibly in accordance with my dictionary.
What I have so far (dictionary is somewhere else):
def numtolist(n):
seedstring = str(n)
numlist = []
for digit in seedstring:
numlist.append(int(digit))
return numlist
currentnumber = seed^2
newmessage = str()
for letter in messageList:
numtolist(currentnumber)
num1 = numlist[0]
If the transform is as simple as an alphabetic shift you can simply do it by:
The basic idea is that each character corresponds to an ascii number which can be gotten by
ord
(i.e.ord('a') = 97
). Thechr
method reverses this:chr(97) = 'a'
.The method is briefly: you get the ascii value, scale to a 0-25 alphabetic range, add your shift, wrap values that overflow the alphabetic range, scale back to ascii, and get a character back via
chr
.You can compact this method a lot, I was verbose in the name of education:
If you wanted to use the same random shift for a whole string you could split this into a couple methods:
If you want to be able to decode it later you need a reversal method and to store the shift value:
If your shift is random you can just pass that as the argument:
or rearrange the methods