Using this program to take out spaces, punctuation, and make letters lower case...
def pre_process(s): #Enter: "Jim's secret password."
s= s.replace("'","")
s= s.replace('.','')
s= s.lower()
s= s.replace(" ","")
return s
How can I encrypt a message so that the letters each shift by an amount equal to the corresponding letter in the alphabet? For example m shifted 5 times becomes r, but w shifted 5 times becomes b. This is my current code:
def shift(ch,k):
return chr(ord('a')+(ord(ch)-ord('a')+k) % 26)
Sort of an explanation:
Unless
k
varies, you can usestr.translate
to speed up encryption:I'd also suggest replacing the magic number
26
withlen(string.ascii_lowercase)
for example.Decryption can be done using the same function, but with a different key. The relation between them is that
enc_delta + dec_delta = 0 modulo 26
. From this results thatdec_delta = -enc_delta % 26
. Therefore:You changed question.
Following to according to old question: How to make shift('w','f') return 'b'?
a
, code isdiff = a2-start
next = a1+diff
z
or not.z
then new value is next value.a
code:
output:
Decrytion/Encryption Python
code:
output: