0

I've been trying how to decode my encryption from "kl" to "a", or "klfg" to "ab"

I have tried str.find(), str.replace() and many other options, but I couldn't get any good result

My code:

 print ("Welcome")
 message = input("Entry text")
 encoded= ""

 for char in message:
    if char== "a" or char == "A":
       encoded += "kl"
    elif char== "b" or char == "B":
       encoded += "fg"

    elif char== "z" or char == "Z":
       encoded += "wt"

 print (encoded)
Gustav Rasmussen
  • 3,720
  • 4
  • 23
  • 53
  • 2
    Your example doesn't show any decoding, just encoding. Can you edit your post to show some of the things you have tried for decoding? – 0x5453 Jul 27 '20 at 20:06
  • May your encoded string contain any non-alphabetic characters (spaces, punctuation, digits, etc.)? Or can you just [chop it into two-character substrings](https://stackoverflow.com/q/9475241/10077)? – Fred Larson Jul 27 '20 at 20:11
  • See the documentation for `itertools`, which provides a recipe `grouper` for iterating over a sequence more than one element at a time. – chepner Jul 27 '20 at 20:14
  • Are your encodings always unique 2 character sequences? – tdelaney Jul 27 '20 at 20:16

3 Answers3

1

You can get rid of the "if..." by building dictionaries to handle the mapping between unencoded and encoded versions of the string. Then its just a question of building the dictionary keys. When encoding, lower and upper case map to the same encoded value. When decoding, grab 2 characters at a time.

encoding = (("A", "kl"), ("B","fg"), ("C","wt"))
encodemap = {frm:to for frm,to in encoding}
decodemap = {to:frm for frm,to in encoding}

def encoder(message):
    return "".join(encodemap.get(c.upper(), "??") for c in message)

def decoder(encrypted):
    return "".join(decodemap.get(encrypted[i:i+2], "?") 
        for i in range(0, len(encrypted), 2))


e = encoder("abcABC")
print(e)
d = decoder(e)
print(d)
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • Thanks tdelaney! Also, answering your previous question "Are your encodings always unique 2 character sequences?" Yes, I was trying to encrypt every letter to an unique pair, but thanks again because I couldn't figure out how to decode it! – Luis Fernando Ojeda Varrasso Jul 28 '20 at 09:07
0

So if I'm reading your question correct, you're looking to decode an encrypted message like for example "klklfg", which is what you get if you input aab into your code.

If you're then looking to decode that message you can do so using the enumerate function which gives you the index as well as the character in a string

decoded = ''

for i, char in enumerate(encoded):
    if char == 'k' and encoded[i + 1] == 'l':
        decoded += 'a'
    if char == 'f' and encoded[i + 1] == 'g':
        decoded += 'b'
print(decoded)

Pasting this code after your code will decrypt your encrypted message! Again, I'm not exactly sure if that was your question but here's my answer anyways.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

Not very efficient, but I think it will solve your problem:

print ("Welcome")
decrypted = ["a", "b", "z"]
encrypted = ["kl", "fg", "wt"]

encryptor = dict(zip(decrypted, encrypted))
decryptor = dict(zip(encrypted, decrypted))

message = input("Entry text:\n")

encoded = ""
for char in message:
  encoded += encryptor.get(char, "?")
print("Encoded:", encoded)

decoded = ""
start = 0
end = 1
while start < end and start < len(encoded):
  while end <= len(encoded):
    substr = encoded[start:end]
    if substr in encrypted:
      decoded += decryptor.get(substr, "?")
      start = end
      end = start + 1
    else:
      end += 1

print("Decoded:", decoded)
Daniser
  • 162
  • 4
  • The "?" in `encryptor.get(char, "?")` is used to indicate a substring that can't be encrypted/decrypted. It's a default value for dictionary lookup if a key is not found. – Daniser Jul 27 '20 at 20:29