简体   繁体   English

使用 python 混淆文本文件 - 通过反转单词并在它们之间插入特定数量的随机字符

[英]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例如,如果用户选择strength = 2,它将在文本文件中的每个字母之间插入2个随机字符:猫坐---> 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 Output 强度 = 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.在 Line Encrypt 方法中,您错误地使用了 Encrypt Strength,您正在使用反向行覆盖要作为 EncryptStrength 放置的字符数。


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.正如 Ritesh 提到的那样,您正在混淆 EncrypStrength 并覆盖它。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Python 中查找文本中两个单词之间的字符数 - Finding the number of characters between two words in a text in Python 如何在文本中查找特定单词并使用 python 计算它们? - How to find specific words in a text and count them using python? 如何从文本文件中获取两个特定单词之间的所有单词,然后使用python将其写入新的文本文件中 - How to get all the words between two specific words from a text file and write it in a new text file using python Python:在任意字符之间的其他字符之间插入字符 - Python: Inserting characters between other characters at random points 计算Python标点符号之间的单词数 - Counting number of words between punctuation characters in Python 使用 Python 计算文本文件中的行数、单词数和字符数 - Counting lines, words, and characters within a text file using Python 使用python多次打印文本文件中两个单词之间的文本 - Print Text between two words in a text file multitime using python python中文件插入时不按顺序插入 - Failure to follow the order of the words when inserting them in the file in python 使用python计算文件中单词之间的空格数? - Counting the number of spaces between words in a file using python? 反转文本并找到相同的单词-python - Reversing a text and find same words - python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM