Dec-16-2017, 05:18 PM
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
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"
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
