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
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,772 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  need help one time pad encryption implementation ! nad556 1 1,911 Nov-28-2020, 06:11 PM
Last Post: nad556
  encryption and decryption with python ibrahim 1 1,768 May-16-2020, 03:14 PM
Last Post: Larz60+
  File encryption itzik 5 2,793 Nov-05-2019, 12:29 PM
Last Post: Gribouillis
  Vernam encryption method for files JohnCTX 1 2,385 Sep-18-2019, 04:31 PM
Last Post: JohnCTX
  Regarding encryption and decryption naressh1994 1 2,389 Jan-25-2019, 07:26 AM
Last Post: buran
  AES encryption - does not match between arduino and python 3 crypto guillaume55 0 3,989 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 4,760 Aug-31-2018, 12:16 PM
Last Post: perfringo
  lab experiment / encryption pythan 0 2,435 Jun-09-2018, 07:19 PM
Last Post: pythan
  Attempting to port XTea Encryption from C to Python sonic1015 1 3,269 Jun-06-2017, 07:12 PM
Last Post: sonic1015

Forum Jump:

User Panel Messages

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