Reputation: 19
I would like help with one cipher, specifically such that I have words/letters and each shift by a certain number
example word Example
+1 input number output is
example
+2 input numbers output is
Eapexml
I have this script but it only works for two shifit
def encrypt(message,shift):
return "%s%s" % (message[::shift],message[1::shift])
print(encrypt("123456",2))
135246
and I really need other combinations
(preferably according to how they are written in the object)
number [1,2,3,4,5,6,7,8,9,10,11]
this is just an example, I will also use other numbers like prime etc...
but I don't know how to write the script, could someone help me?
Upvotes: 0
Views: 211
Reputation: 75840
Wanted to see if this is possible with a regular expression for fun:
import regex as re
def encrypt(message,shift):
rgx = re.compile(fr'(?:.{{{shift-1}}}|.\G)\K.')
return re.sub(rgx, '', message) + ''.join(re.findall(rgx, message))
for i in range(1, 5):
print(encrypt('example', i))
Prints:
example
eapexml
exmpeal
exaplem
Upvotes: 1
Reputation: 26993
I think this is what you're trying to do:
def encrypt(message, shift):
return ''.join(message[i::shift] for i in range(shift)) if shift > 1 else message
for i in range(1, 5):
print(encrypt('ABCDEFGH', i))
Output:
ABCDEFGH
ACEGBDFH
ADGBEHCF
AEBFCGDH
Observation:
This is an awful encryption algorithm if only because the first letter never changes
Upvotes: 3