Python Forum
Errors in my encryption algorithm - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Errors in my encryption algorithm (/thread-6991.html)



Errors in my encryption algorithm - SlimeBOOS - Dec-16-2017

So.. I wanted to make an algorithm that would encrypt the name of a file. AND I DID IT... i think , because sometimes when i run the algorithm to decrypt a file name it messes up a couple of letters Wall and i don't know why. So could you help me?
BTW: i don't encrypt the "."
Example of encryption:
fileName="FileName.txt"
key="123"
Output:
newFileName="fsxkUjBt.x26"

Example of decryption:
fileName="fsxkUjBt.x26"
key="123"
Output:
newFileName="LileNate.txt"


CODE

import random

fileName="FileName.txt"
key="123"
mode='c'
#c - encrypt
#d - decrypt

#calculate the seed
keySum=sum(ord(i) for i in key)
#set the seed
random.seed(keySum)
newFileName=''
newChr=''

for i in fileName:
            #if the current charecter to cipher is a dot skip it
            if i == '.':
                newFileName+='.'
                continue
            
            #get the new charecter
            if mode == 'c':
                newChr=chr(ord(i)+random.randint(0,26))
            elif mode == 'd':
                newChr=chr(ord(i)-random.randint(0,26))

            #make the charecter valid if invalid
            while True:
                if ord(newChr) < 48:
                    newChr=chr(ord(newChr)+75)
                    i='{'
                    continue
                elif ord(newChr) > 57 and ord(newChr) < 65:
                    if ord(newChr) > ord(i):
                        newChr=chr(ord(newChr)+7)
                        continue
                    else:
                        newChr=chr(ord(newChr)-7)
                        continue
                elif ord(newChr) > 90 and ord(newChr) < 97:
                    if ord(newChr) > ord(i):
                        newChr=chr(ord(newChr)+6)
                        continue
                    else:
                        newChr=chr(ord(newChr)-6)
                        continue
                elif ord(newChr) > 122:
                    newChr=chr(ord(newChr)-75)
                    i='/'
                    continue
                break
            
            #add the new valid charecter
            newFileName+=newChr



RE: Errors in my encryption algorithm - SlimeBOOS - Dec-16-2017

Solved