2

First, I'm sorry, there's so many questions regarding this issue, and I drowned in so many answers, but I still don't get it, because my code works just if I write to the file(?)

So, why this works:

# encrypt and write to file    
    data_to_encrypt = "mytext" 
    data = data_to_encrypt.encode('utf-8')
    ciphered_bytes = cipher_encrypt.encrypt(data)
    ciphered_data = ciphered_bytes
    print(ciphered_data)

    with open(fileConfigBIN, "wb") as binary_file:
        binary_file.write(ciphered_data)

# read the file and decrypt
    with open(fileConfigBIN, "rb") as binary_file:
        data1 = binary_file.read()
        print(data1)
        deciphered_bytes = cipher_decrypt.decrypt(data1)
        decrypted_data = deciphered_bytes.decode('utf-8')
        print(decrypted_data)

Output:

b'\xa0U\xee\xda\xa8R'

b'\xa0U\xee\xda\xa8R'

mytext

But just by commenting the write to file (the file still there with the same information):

    #data_to_encrypt = "mytext" 
    #data = data_to_encrypt.encode('utf-8')
    #ciphered_bytes = cipher_encrypt.encrypt(data)
    #ciphered_data = ciphered_bytes
    #print(ciphered_data)

    #with open(fileConfigBIN, "wb") as binary_file:
    #    binary_file.write(ciphered_data)

    with open(fileConfigBIN, "rb") as binary_file:
        data1 = binary_file.read()
        print(data1)
        deciphered_bytes = cipher_decrypt.decrypt(data1)
        decrypted_data = deciphered_bytes.decode('utf-8')
        print(decrypted_data)

I get this:

b'\xa0U\xee\xda\xa8R'

Exception in Tkinter callback Traceback (most recent call last): File "C:\Users...................\Python37-32\lib\tkinter__init__.py", line 1705, in call return self.func(*args) File "C:\Users\Fabio\source.............", line 141, in AbrirConfig decrypted_data = deciphered_bytes.decode('utf-8') UnicodeDecodeError: 'utf-8' codec can't decode byte 0xdd in position 1: invalid continuation byte

The read code is using the same file as the commented part save it!

I tried to save and read in 'latin-1', 'ISO-8859-1' and others.. it give no errors, but returns just strange characters

Using Python 3.7

Thank's in advance!

EDIT:

Full working minimal code:

from Crypto.Cipher import AES

fileConfigBIN = "data.dat"

key = b'\x16\x18\xed\x1c^\xaaGN\rl\xc0]\xf0t=\xd0\xdc]t\xaf\xb2\x12,\xe6\xfc\xd6\x11-\x10\xb4\xb1\x0b'
cipher_encrypt = AES.new(key, AES.MODE_CFB)
iv = cipher_encrypt.iv
cipher_decrypt = AES.new(key, AES.MODE_CFB, iv=iv)

def Encrypt():
    data_to_encrypt = "MyTextToEncrypt" 
    data = data_to_encrypt.encode('utf-8')
    ciphered_bytes = cipher_encrypt.encrypt(data)
    ciphered_data = ciphered_bytes

    with open(fileConfigBIN, "wb") as binary_file:
        binary_file.write(ciphered_data)
        print("CRYPTED DATA SAVED TO FILE: ", str(ciphered_data))

def Decrypt():
    with open(fileConfigBIN, "rb") as binary_file:
        data1 = binary_file.read()
        print("DATA READ FROM FILE: ", data1)
        deciphered_bytes = cipher_decrypt.decrypt(data1)
        decrypted_data = deciphered_bytes.decode('utf-8')
        print("DECRYPTED DATA: ", decrypted_data)

Encrypt() #comment this function to show the problem
print('---------------------')
Decrypt()

I did it guys! Thanks to @Hoenie insight, for every encryption, I store the iv value and use it to decrypt when needed. If happens to need to encrypt again, it save another iv value and so on... Thanks thanks!

Ken Sowyer
  • 23
  • 1
  • 4

1 Answers1

0

Ok, it seems this does have nothing to do with encoding/decoding. You need to create a new cipher object, because it can only be used once.

def Decrypt():
    cipher_decrypt = AES.new(key, AES.MODE_CFB, iv=iv)   # <-- add this
    with open(fileConfigBIN, "rb") as binary_file:
        data1 = binary_file.read()
        print("DATA READ FROM FILE: ", data1)
        deciphered_bytes = cipher_decrypt.decrypt(data1)
        decrypted_data = deciphered_bytes.decode('utf-8')
        print("DECRYPTED DATA: ", decrypted_data)

In the source code: GitHub

A cipher object is stateful: once you have decrypted a message you cannot decrypt (or encrypt) another message with the same object.

Thanks to: https://stackoverflow.com/a/54082879/7547749

PythonSherpa
  • 2,560
  • 3
  • 19
  • 40
  • 1
    That kinda works! I put the first iv value that was generated in the encryptation in the "iv=" and now I can decrypt. BUT, I can encrypt again.. thanks dude! That helped! – Ken Sowyer Feb 14 '20 at 03:44