0

So I created a custom encryption algorithm that I call Grid. Here's the code:

    print('')
    print("Welcome to the Project Grid encryption tool.")
    print("Note that this encryption method is not completely secure.")
    print("Anyone with this program can learn the encryption.")
    print("Encrypted documents may be confused if numbers are used.")
    print("Happy Encrypting!")
    print("")
    e = raw_input()
    o = e.lower()
    list(o)
    o = [s.replace('a','11') for s in o]  #all of this is just to do the     encryption
o = [s.replace('b','12') for s in o]
o = [s.replace('c','13') for s in o]
o = [s.replace('d','14') for s in o]
o = [s.replace('e','15') for s in o]
o = [s.replace('f','21') for s in o]
o = [s.replace('g','22') for s in o]
o = [s.replace('h','23') for s in o]
o = [s.replace('i','24') for s in o]
o = [s.replace('j','25') for s in o]
o = [s.replace('k','31') for s in o]
o = [s.replace('l','32') for s in o]
o = [s.replace('m','33') for s in o]
o = [s.replace('n','34') for s in o]
o = [s.replace('o','35') for s in o]
o = [s.replace('p','41') for s in o]
o = [s.replace('q','42') for s in o]
o = [s.replace('r','43') for s in o]
o = [s.replace('s','44') for s in o]
o = [s.replace('t','45') for s in o]
o = [s.replace('u','51') for s in o]
o = [s.replace('v','52') for s in o]
o = [s.replace('w','53') for s in o]
o = [s.replace('x','54') for s in o]
o = [s.replace('y','55') for s in o]
o = [s.replace('z','61') for s in o]    
o = [s.replace('.', '') for s in o]   #This just removes any punctuation, so as to not mess the cypher up.
o = [s.replace('?', '') for s in o]
o = [s.replace('!', '') for s in o]
o = [s.replace("'", '') for s in o]
o = [s.replace('"', '') for s in o]
o = [s.replace(',', '') for s in o]
o = [s.replace(' ', '.') for s in o]

f = ''.join(o)
print f

with open('encrypted.txt', 'a') as the_file:   #writes the encryption to a file
    the_file.write(f + ' <br> ')

Here's my problem: I'm working on a decryption algorithm, but I'm having a quite a bit of trouble with converting the number lines that it returns back into the letter - containing codes and therefore can't make it actually decrypt. Here's the decryption code.

print('')
print("Welcome to the Project Grid decryption tool.")
print("Happy Decrypting!")
print("")
r = raw_input()
r = [s.replace('.', '62') for s in r]
x = [int(i) for i in r]
n = 2
o = [x[i:i+n] for i in range(0, len(x), n)]
print o

o = [s.replace('11','a') for s in o]
o = [s.replace('12','b') for s in o]
o = [s.replace('13','c') for s in o]
o = [s.replace('14','d') for s in o]
o = [s.replace('15','e') for s in o]
o = [s.replace('21','f') for s in o]
o = [s.replace('22','g') for s in o]
o = [s.replace('23','h') for s in o]
o = [s.replace('24','i') for s in o]
o = [s.replace('25','j') for s in o]
o = [s.replace('31','k') for s in o]
o = [s.replace('32','l') for s in o]
o = [s.replace('33','m') for s in o]
o = [s.replace('34','n') for s in o]
o = [s.replace('35','o') for s in o]
o = [s.replace('41','p') for s in o]
o = [s.replace('42','q') for s in o]
o = [s.replace('43','r') for s in o]
o = [s.replace('44','s') for s in o]
o = [s.replace('45','t') for s in o]
o = [s.replace('51','u') for s in o]
o = [s.replace('52','v') for s in o]
o = [s.replace('53','w') for s in o]
o = [s.replace('54','x') for s in o]
o = [s.replace('55','y') for s in o]
o = [s.replace('61','z') for s in o]
o = [s.replace('62',' ') for s in o]    

f = ''.join(o)
print f

with open('decrypted.txt', 'a') as the_file:
    the_file.write(f + ' <br> ')

Any help would be appreciated. Also, any slimming down that you do could help too, just because I feel like there has to be a better way to do it. Thanks in advance guys! -Richard

1
  • 1
    There is a better way... you should look into for loops and use a dictionary to map plaintext characters to their numeric string cyphertext, and vice versa. Commented Jan 23, 2015 at 6:46

1 Answer 1

3

The problem with your algorihm is that the numbers you convert to are not independent/orthogonal or whatever it might be called. Take for example the word pass, that will be encoded to 41114444. Then along comes your decoding algorithm starting with decoding all a=11 first, meaning after first symbol of decoding your semi decoded string will be 4a14444. Next it will match 14 = dresulting in 4ad444 and finally match 44 = s, so pass has become 4ads4 .

To fix this (using the codewords you have in the example) you need to scan the encoded string left to right during decoding and then decode one symbol at the time

def conv_sym(o):
    o = o.replace('11','a')
    o = o.replace('12','b')
    o = o.replace('13','c') 
    o = o.replace('14','d')
    o = o.replace('15','e') 
    o = o.replace('21','f') 
    o = o.replace('22','g') 
    o = o.replace('23','h') 
    o = o.replace('24','i') 
    o = o.replace('25','j') 
    o = o.replace('31','k') 
    o = o.replace('32','l')   
    o = o.replace('33','m') 
    o = o.replace('34','n') 
    o = o.replace('35','o') 
    o = o.replace('41','p') 
    o = o.replace('42','q') 
    o = o.replace('43','r')
    o = o.replace('44','s')
    o = o.replace('45','t') 
    o = o.replace('51','u') 
    o = o.replace('52','v') 
    o = o.replace('53','w')
    o = o.replace('54','x') 
    o = o.replace('55','y') 
    o = o.replace('61','z') 
    o = o.replace('62',' ') 
    return o

enc_str = "41114444"
dec_str = ""
while len(enc_str) > 0:
    sym = enc_str[:2]
    dec_str += conv_sym(sym)
    enc_str = enc_str[2:]
print (dec_str)

But a much better way is to store your symbol table in a dictionary and then just look up the correct translation there. Here is the same example as above but using this approach

dec_table = {'11': 'a', '41': 'p', '44': 's'}
enc_str = "41114444"
dec_str = ""

while len(enc_str) > 0:
    sym = enc_str[:2]
    dec_str += dec_table[sym]
    enc_str = enc_str[2:]

print (dec_str)

This approach can be taken also when encoding

orig_str = "pass"
enc_str = ""
dec_str = ""

enc_table = {'s': '44', 'a': '11', 'p': '41'}
dec_table = dict (zip(enc_table.values(),enc_table.keys()))

print ("Original: ", orig_str)
# Encode
for i in orig_str:
    enc_str += enc_table[i];

print ("Encoded: ", enc_str)

while len(enc_str) > 0:
    sym = enc_str[:2]
    dec_str += dec_table[sym]
    enc_str = enc_str[2:]

print ("Decoded: ", dec_str)

output of this is

Original:  pass
Encoded:  41114444
Decoded:  pass
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.