Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

python - I am trying to decrypt my fille but only receiving 1 character

    #this will import randInt for random number generator and sleep
from random import randint
from time import sleep
    #function

def encrypt(plaintext,key):
        #naming variables
        cypherText=''
        #encryption
        for char in plaintext:
            #this if is used so if the user does a space the program will continue the encryption. Without it the program will simply encrpyt the first word before the space
            if char== ' ':
                cypherText=cypherText+char
                #if characters are all upper case the program will use the ASCII values from 97 with negative index
            elif char.isupper():
                cypherText= cypherText+ chr((ord(char)+key-65)%26+65)
                #if characters all lower case the program  will use the ASCII values from 65 with negative index
            else:
                cypherText= cypherText+ chr((ord(char)+key-97)%26+97)
        #this will return the Text out of the function
        fh=open(input("Enter File Name with .txt 
"),'w')
        fh.write(cypherText)
        fh.close()
        encryptedtext=plaintext
        return cypherText

def decrypt(plaintext,key):
        #naming variables
        decryptedtext=' '
        for char in plaintext:
            if char==' ':
                plaintext=plaintext+char
            #this will decrypt upper case characters
            elif (char.isupper()):
                decryptedText += chr((ord(char) -65 + key)% 26 + 65)
            #this will decrypt lower case characters
            else:
                decryptedText += chr((ord(char) -97 + key)% 26 + 97)

        return decryptedText

    #here the user will enter the text
    #here the program will display the menu



print("------- M A I N  M E N U -------
 A. Generate Random Key
 B. Encrypt File 
 C. Decrypt File 
 X. Quit")
option=''
while (option.lower()!='x'):
    option=input("Enter your choice
 A. B. C. X.
")
    if(option.lower()=='a'):
            key=randint(1,25)
            print("---------------")
            print("The Key generated is: " ,key)
            print("---------------")

    elif (option.lower()=='b'):
            sleep(1)
            key=int(input("Please enter the Encryption Key"))
            print("The key is ",key)
            plaintext=input("Enter the text you want to encrypt 
")
            print("The encryption is sucessfully complete:",encrypt(plaintext,key))
            
    elif(option.lower()=='c'):
        key=int(input("Please enter the same key you used for the encryption"))
        with open(input('Enter file name to decrypt')) as file:
            plaintext=file.read()
            print("The file was sucessfully decrypted: ",decrypt(plaintext,key))
            
    elif(option.lower()=='x'):
        print("The program is now quitting...")
        sleep(3)
        quit()
           

            

This program is supposed to be an encrpyter and decrypter. Encrypting works fine and saves to a file but for whatever reason the decryption part only shows a character. I am using the ASCII method here. I have checked indentation and everything. I'm kind of a beginner and tried everything I could to make this work Thanks :)

question from:https://stackoverflow.com/questions/65940052/i-am-trying-to-decrypt-my-fille-but-only-receiving-1-character

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you have to use += for example cypherText += cypherText+char, because when you use = you are resetting the whole string.

Another this is, I've already used a decoder so ill show you the code I've used and maybe you can change it to fit you, here it is:

                key += 1
                newMessage = ''
                for character in message:
                  if character in noCapAlphabet:
                    position = noCapAlphabet.find(character)
                    newPosition = (position - key) % 26
                    newCharacter = noCapAlphabet[newPosition]
                    newMessage += newCharacter
                    
                  elif character in number:
                    position = number.find(character)
                    newPosition = (position - key) % 10
                    newCharacter = number[newPosition]
                    newMessage += newCharacter
                    
                  elif character in capAlphabet:
                    position = capAlphabet.find(character)
                    newPosition = (position - key) % 26
                    newCharacter = capAlphabet[newPosition]
                    newMessage += newCharacter

                  else:
                    newMessage += character

noCapAlphabet, number and capAlphabet are arrays


Edit, the function can work as well:

message = 'ifmmp'
noCapAlphabet = 'abcdefghijklmnopqrstuvwxyz'
capAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
number = '0123456789'
def decrypt(message):
    key = 1
    newMessage = ''
    for character in message:
        if character in noCapAlphabet:
            position = noCapAlphabet.find(character)
            newPosition = (position - key) % 26
            newCharacter = noCapAlphabet[newPosition]
            newMessage += newCharacter
                        
        elif character in number:
            position = number.find(character)
            newPosition = (position - key) % 10
            newCharacter = number[newPosition]
            newMessage += newCharacter
                        
        elif character in capAlphabet:
            position = capAlphabet.find(character)
            newPosition = (position - key) % 26
            newCharacter = capAlphabet[newPosition]
            newMessage += newCharacter

        else:
            newMessage += character
            
    return newMessage

print(decrypt(message))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...