tigertv / secretpy

Classical ciphers: Caesar, ADFGX, ROT13 and etc.
https://pypi.org/project/secretpy/
MIT License
61 stars 10 forks source link

Autokey broken? #2

Closed mattbruv closed 4 years ago

mattbruv commented 4 years ago
from secretpy import Autokey
from secretpy import CryptMachine
from secretpy.cmdecorators import UpperCase, SaveSpaces, NoSpaces
from secretpy import alphabets

cipher = Autokey()
key = "test"
msg = "this is a secret message"

cm = CryptMachine(cipher, key)
out = cm.encrypt(msg)

print(out)

This should work, but it doesn't for some reason... Shouldn't the autokey "key" be a string instead of an integer? It's documented as an integer.

tigertv commented 4 years ago

Hi, @mattbruv ! This doesn't work because the default alphabet doesn't have 'spaces'. If you remove all spaces in msg the code works. Also, you can use NoSpaces, it removes spaces from message:

from secretpy import Autokey
from secretpy import CryptMachine
from secretpy.cmdecorators import UpperCase, SaveSpaces, NoSpaces
from secretpy import alphabets

cipher = Autokey()
key = "test"
msg = "this is a secret message"

cm = NoSpaces(CryptMachine(cipher, key))
out = cm.encrypt(msg)

print(out)

output: mlalbzikmurwxovwlmkw

And if you want save the spaces in message you can use SaveSpaces:

from secretpy import Autokey
from secretpy import CryptMachine
from secretpy.cmdecorators import UpperCase, SaveSpaces, NoSpaces
from secretpy import alphabets

cipher = Autokey()
key = "test"
msg = "this is a secret message"

cm = SaveSpaces(NoSpaces(CryptMachine(cipher, key)))
out = cm.encrypt(msg)

print(out)

This returns encrypted message with spaces: mlal bz i kmurwx ovwlmkw