简体   繁体   中英

obfuscation of a text file using python - by reversing the words and inserting a specific number of random characters between them

Beginner Coding problem I am supposed to write a code that reverses the contents of a file and then inserts a number of random characters based on a strength the user chooses. It then creates a new file containing the obstructed file.

For example, if the user chooses strength = 2, it will insert 2 random characters between each letter in the text file: The cat sits ---> sgyt6gilns t7faxdc e3dh1kT

Right now my program inserts too many characters in between and I can't figure out why. This is what it's doing:

input: CAT

Output of strength = 1: TeAEADQoC

import string
import random

def getRandomChar():
    alpha = string.ascii_letters + string.digits
    return random.choice(alpha)

def randomString(EncrypStrength): 
    count = 0
    result = ''
    while count < len(EncrypStrength):
        result += getRandomChar()
        count += 1
    return result

def ReverseString(OrigFile):
    return OrigFile[::-1]    

def LineEncrypt(line, EncrypStrength):
    EncrypStrength = ReverseString(line)
    
    index = 0 
    newline = EncrypStrength[index]
    index += 1
    
    while index < len(EncrypStrength):
        newline += randomString(EncrypStrength)
        newline += EncrypStrength[index]
        index += 1
    
    return newline    

def main():
    
    OrigFile =input('Original File Name:')
    EncryptedFile = input("obfuscated File Name:")
    EncrypStrength = int(input('Enter the Encryption Strength:'))
    
    Orig = open(OrigFile, 'r')
    Encrypted = open(EncryptedFile, 'w') 

    line = Orig.readline()
    
    while line!= '':
        encryptLine = LineEncrypt(line, EncrypStrength)
        Encrypted.write(encryptLine +"\n")
        line = Orig.readline()
           
    Orig.close()
    Encrypted.close()


if __name__=="__main__":
    main()

In Line Encrypt method you are using incorrectly Encrypt Strength, you are overriding the number of characters to put as EncryptStrength with reversed line.


def LineEncrypt(line, EncrypStrength):
    reversedString = ReverseString(line)
    
    index = 0 
    newline = reversedString[index]
    index += 1
    
    while index < len(reversedString):
        newline += randomString(EncrypStrength)
        newline += reversedString[index]
        index += 1

You are confusing EncrypStrength and overriding it as Ritesh mentioned. Here is the full corrected code, I hope it will work as you expected.

import string
import random

def getRandomChar():
    alpha = string.ascii_letters + string.digits
    return random.choice(alpha)

def randomString(EncrypStrength): 
    count = 0
    result = ''
    while count < EncrypStrength:
        result += getRandomChar()
        count += 1
    return result

def ReverseString(OrigFile):
    return OrigFile[::-1]    

def LineEncrypt(line, EncrypStrength):
    RevStr = ReverseString(line)
    
    index = 0 
    newline = RevStr[index]
    index += 1
    
    while index < len(RevStr):
        newline += randomString(EncrypStrength)
        newline += RevStr[index]
        index += 1
    
    return newline    

def main():
    
    OrigFile =input('Original File Name:')
    EncryptedFile = input("obfuscated File Name:")
    EncrypStrength = int(input('Enter the Encryption Strength:'))
    
    Orig = open(OrigFile, 'r')
    Encrypted = open(EncryptedFile, 'w') 

    line = Orig.readline()
    
    while line!= '':
        encryptLine = LineEncrypt(line, EncrypStrength)
        Encrypted.write(encryptLine +"\n")
        line = Orig.readline()
           
    Orig.close()
    Encrypted.close()


if __name__=="__main__":
    main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM