简体   繁体   中英

Python Crypted error "UnicodeEncodeError"

I Tried to creat a code for simulate a register.

The user choose for exemple to "create a account" and stock the usename and password in a txt named username.txt

This part work but now I trie to crypted the password but I obtened:

"File "C:\Program\Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0\lib\encodings\cp1252.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\x95' in position 2: character maps to "undefined".

But if I use my programm to crypted lonnely that work

my code:

# coding: utf-8

import colorama
from colorama import Fore, Back, Style
import os
import time
import platform
import shutil
import sys
import _socket
import socket
from Cryptage import *

#-------------------------------------------------------------------------------------------------

cle= open("cle.txt","r", encoding="utf-8")
cle=cle.read()
colorama.init(autoreset = True)
Boucle = 1
Pseudo = ""
encoded_string = ''

#-------------------------------------------------------------------------------------------------


def Clean():
    if platform.system() == "Windows":
        os.system("cls")
    elif platform.system() == "Linux":
        os.system("clear")

#-------------------------------------------------------------------------------------------------

def Acceuil():

    Clean()
    print(Fore.BLUE+"<===ACCEUIL===>")
    print(Fore.BLUE+"---------------------------------------------------------------------")
    print(Fore.BLUE+"1. Se connecter")
    print(Fore.BLUE+"2. S'inscrire")
    print(Fore.BLUE+"3. Quitter")
    print(Fore.BLUE+"---------------------------------------------------------------------")
    choix = input()

    if choix =='1':
        Connexion()
    
    elif choix =='2':
        Register()
    
    elif choix =='3':
        Clean()
        print("Fin du programme...")
        time.sleep(2.4)
        sys.exit()

    else:
        Clean()
        print(Fore.RED + "invalide")
        colorama.init(autoreset = True)

    #-------------------------------------------------------------------------------------------------

def Register():

    Clean()

    print(Fore.BLUE+"<===INSCRIPTION===>")
    print(Fore.BLUE+"---------------------------------------------------------------------")
    Pseudo, MDP = input(Fore.BLUE+"Pseudo, MDP: ").split()


    TxtUser = "{Pseudo2}.txt".format(Pseudo2=Pseudo)
    CheminBase = "C:\\Users\\yuris\\Desktop\\Python\\GestionDeCompte\\{TxtUser2}".format(TxtUser2=TxtUser)
    CheminArriver = "C:\\Users\\yuris\\Desktop\\Python\\GestionDeCompte\\Users\\{TxtUser2}".format(TxtUser2=TxtUser)

    MDPcrypted = encode(cle, MDP)

if os.path.exists(CheminArriver):

    Clean()
    print(Fore.RED+"Le Pseudo",Fore.RED+Pseudo,Fore.RED+"Existe deja")
    input()

 
else:

    NewUser = open(TxtUser,"x")
    NewUser.close()

    NewUser = open(TxtUser,"a")
    NewUser.write("Pseudo: ")
    NewUser.write(Pseudo)
    NewUser.write("! ")

    NewUser.write("MDP: ")
    NewUser.write( MDPcrypted)
    NewUser.write("!")
    NewUser.close()

    shutil.move(CheminBase,CheminArriver)
    print(Fore.BLUE+"Déplacement vers connexion...")
    time.sleep(2.4)
    Connexion()

        #-------------------------------------------------------------------------------------------------

def Connexion():

    Clean()
    print(Fore.BLUE+"<===CONNEXION===>")
    print(Fore.BLUE+"---------------------------------------------------------------------")
    Pseudo, MDP = input(Fore.BLUE+"Pseudo, MDP: ").split()
    print(Fore.BLUE+"---------------------------------------------------------------------")

    TxtUser = "{Pseudo2}.txt".format(Pseudo2=Pseudo)
    CheminArriver = "C:\\Users\\yuris\\Desktop\\Python\\GestionDeCompte\\Users\\{TxtUser2}".format(TxtUser2=TxtUser)

    if os.path.exists(CheminArriver) :

        MDPregister = "{MDP2}!".format(MDP2=MDP)
        UserConnect = open(CheminArriver,"r")
    
        for ligne in  UserConnect:
            if MDPregister in ligne:
                MenuPrincipal()
            else:
                print(Fore.RED+"Le Pseudo ou le MDP est invalide")
                input()
 
    else:
        print(Fore.RED+"Le Pseudo ou le MDP est invalide")
        input()

#-------------------------------------------------------------------------------------------------

def MenuPrincipal():
    while Boucle != 0:

        Clean()
   
        print(Fore.GREEN+"Bienvenue",Fore.GREEN+Pseudo)
        input()
 
#-------------------------------------------------------------------------------------------------

while Boucle != 0:

    Acceuil()
    print("")

And this is my programm for crypted the password:

# coding: utf-8
# vigenere cipher

#--------------------------------------------------------------------------------

cle= open("cle.txt","r", encoding="utf-8")
cle=cle.read()

#--------------------------------------------------------------------------------
        
def encode(cle, string):
    encoded_chars = []
    for i in range(len(string)):
        key_c =cle[i % len(cle)]
        encoded_c = chr(ord(string[i]) + ord(key_c) % 256)
        encoded_chars.append(encoded_c)
    encoded_string = ''.join(encoded_chars)
    return encoded_string

def decode(cle, string):
    encoded_chars = []
    for i in range(len(string)):
        key_c = cle[i % len(cle)]
        encoded_c = chr((ord(string[i]) - ord(key_c) + 256) % 256)
        encoded_chars.append(encoded_c)
    encoded_string = ''.join(encoded_chars)
    return encoded_string

(Re:Programme Modified)

Your first line should read # coding: utf-8 or coding=utf-8 (take out the en ), or specify when you read in cle that cle = open("cle.txt", "r", encoding="utf-8") . You are using a different codec than utf-8.

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