Python Forum
Errors in my encryption algorithm
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Errors in my encryption algorithm
#1
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
Reply
#2
Solved
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  need help one time pad encryption implementation ! nad556 1 530 Nov-28-2020, 06:11 PM
Last Post: nad556
  encryption and decryption with python ibrahim 1 613 May-16-2020, 03:14 PM
Last Post: Larz60+
  File encryption itzik 5 919 Nov-05-2019, 12:29 PM
Last Post: Gribouillis
  Vernam encryption method for files JohnCTX 1 819 Sep-18-2019, 04:31 PM
Last Post: JohnCTX
  Regarding encryption and decryption naressh1994 1 1,063 Jan-25-2019, 07:26 AM
Last Post: buran
  AES encryption - does not match between arduino and python 3 crypto guillaume55 0 2,437 Sep-23-2018, 11:14 AM
Last Post: guillaume55
  Python function that uses a word as the encryption key, rather than an integer wak_stephanie 4 2,408 Aug-31-2018, 12:16 PM
Last Post: perfringo
  lab experiment / encryption pythan 0 1,348 Jun-09-2018, 07:19 PM
Last Post: pythan
  Attempting to port XTea Encryption from C to Python sonic1015 1 1,964 Jun-06-2017, 07:12 PM
Last Post: sonic1015
  Testing homemade encryption program in python LEJONKINGEN 1 2,043 Mar-04-2017, 04:09 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020