Why my encrypt method doesn't work, encoding without string argument?
Question:
I made a bin file without extension, and that file contain a text that is encrypted, the problem is that when I want to retrieve the text I get this error message
Exception in Tkinter callback
Traceback (most recent call last):
File "C:Usersconitc11AppDataLocalProgramsPythonPython39libtkinter__init__.py",
line 1884, in __call__
return self.func(*args)
File "c:Usersconitc11DesktopPYREPORTEBIOSTAR.py", line 338, in
LecturaBinario
encrypted_text = pad(bytes(content, 'utf-8'), 16)
TypeError: encoding without a string argument
Here is the code
def EncrypConfig(message):
entryMessage = pad(bytes(message, 'utf-8'), 16)
obj = AES.new(secret_key, AES.MODE_CBC, iv)
print('Original message is: ', entryMessage)
#PAD = lambda s: s + (32 - len(s) % 32)*''
encrypted_text = obj.encrypt(entryMessage)
print('The encrypted text', encrypted_text)
return encrypted_text
def CreacionBinario():
f = open('cfg', 'wb')
#byte_arr = [120, 3, 255, 0, 100]
#byte_arr = [columnasConfig[0], columnasConfig[1], columnasConfig[2]]
columnasConfig[0] = cbDepartamento.get()
columnasConfig[1] = '2345G564D5645'
columnasConfig[2] = lector.get()
#counterBoy = 0
#for x in range(3):
#binary_format = columnasConfig[x]
EncrypConfig(columnasConfig[0])
print('Texto a encryptar: ' + columnasConfig[0])
f.write((bytes(str(EncrypConfig(columnasConfig[0])), 'utf-8')))
print('Texto encryptado: ' + str(EncrypConfig(columnasConfig[0])))
#counterBoy += 1
#print(str(counterBoy - 1) + ' CONTADO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
f.close()
def LecturaBinario():
'''
f = open('cfg', 'w+b')
rev_obj = AES.new(secret_key, AES.MODE_CBC, iv)
decrypted_text = rev_obj.decrypt(encrypted_text)
#for x in range(2):
columnasConfig[0] = f.readline(decrypted_text)
f.close()
print('columna 0 ' + columnasConfig[0] + ' columna 1 ' + columnasConfig[1]+ ' columna 2 ' + columnasConfig[2])
'''
#CreacionBinario()
with open('cfg', 'rb') as fh:
content = fh.read()
print('Content:' + str(content))
#encrypted_text = pad(bytes(str(content), 'utf-8'), 16)
encrypted_text = pad(bytes(content, 'utf-8'), 16)
rev_obj = AES.new(secret_key, AES.MODE_CBC, iv)
decrypted_text = rev_obj.decrypt(encrypted_text)
print("Print the full content of the binary file:")
print(content)
print("DecriptedText: ", decrypted_text.decode('utf-8'))
This is the sample code i’m using, i tried not using bytes in content just pad and it display the next error "DecriptedText: <built-in method decode of bytes object at 0x0000019C7EFDFC00>" instead of the decoded output, without pad and bytes the result is "ValueError: Data must be padded to 16 byte boundary in CBC mode"
from email.message import Message
from os import urandom
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
# For Generating cipher text
secret_key = urandom(16)
iv = urandom(16)
obj = AES.new(secret_key, AES.MODE_CBC, iv)
message = pad(bytes('All Users', 'utf-8'), 16)
print('Original message is: ', message)
encrypted_text = obj.encrypt(message)
print('The encrypted text', encrypted_text)
# Decrypt the message
rev_obj = AES.new(secret_key, AES.MODE_CBC, iv)
decrypted_text = rev_obj.decrypt(encrypted_text)
print('The decrypted text', decrypted_text.decode('utf-8'))
SOLUTION.
Well at first place i saw the comments but i didn’t understand what they was saying, they gave me the idea of replacing or changing several code lines the next code line
message = pad(bytes(str(text), 'utf-8'), 16)
replaced this one
entryMessage = pad(bytes(message, 'utf-8'), 16)
This change has no effect in the code but i replace it to have a better order also i moved the line down of the next line
obj = AES.new(secret_key, AES.MODE_CBC, iv)
The next change was replacing this line
f.write(EncrypConfig(listOfConfig))
instead of this line
f.write((bytes(str(EncrypConfig(columnasConfig[0])), 'utf-8')))
This change was the most important, next to this i delete the next line of code
encrypted_text = pad(bytes(content, 'utf-8'), 16)
and some other changes the final result and THE SOLUTION is this:
secret_key = b'somebinarykey12'
iv = b'somebinaryvalue1'
def EncrypConfig(text):
obj = AES.new(secret_key, AES.MODE_CBC, iv)
message = pad(bytes(str(text), 'utf-8'), 16)
encrypted_text = obj.encrypt(message)
return encrypted_text
def CreacionBinario():
f = open('cfg', 'wb')
columnasConfig[0] = cbDepartamento.get()
columnasConfig[1] = txtContraseñaSave.get()
columnasConfig[2] = lector.get()
columnasConfig[3] = checkedLilWindow2.get()
listOfConfig = [columnasConfig[0], columnasConfig[1], columnasConfig[2], columnasConfig[3]]
f.write(EncrypConfig(listOfConfig))
print('Texto a encryptar: ', listOfConfig)
print('Texto encryptado: ', EncrypConfig(listOfConfig))
f.close()
def LecturaBinario():
with open('cfg', 'rb') as fh:
content = fh.read()
print('Content:', content)
rev_obj = AES.new(secret_key, AES.MODE_CBC, iv)
decrypted_text = rev_obj.decrypt(content)
print("Print the full content of the binary file:")
print(content)
print('The decrypted text: ', decrypted_text.decode('utf-8'))
listFromFile = list[decrypted_text.decode('utf-8')]
return listFromFile[1]
Answers:
SOLUTION.
Well at first place i saw the comments but i didn’t understand what they was saying, they gave me the idea of replacing or changing several code lines the next code line
message = pad(bytes(str(text), 'utf-8'), 16)
replaced this one
entryMessage = pad(bytes(message, 'utf-8'), 16)
This change has no effect in the code but i replace it to have a better order also i moved the line down of the next line
obj = AES.new(secret_key, AES.MODE_CBC, iv)
The next change was replacing this line
f.write(EncrypConfig(listOfConfig))
instead of this line
f.write((bytes(str(EncrypConfig(columnasConfig[0])), 'utf-8')))
This change was the most important, next to this i delete the next line of code
encrypted_text = pad(bytes(content, 'utf-8'), 16)
and some other changes the final result and THE SOLUTION is this:
secret_key = b'somebinarykey12'
iv = b'somebinaryvalue1'
def EncrypConfig(text):
obj = AES.new(secret_key, AES.MODE_CBC, iv)
message = pad(bytes(str(text), 'utf-8'), 16)
encrypted_text = obj.encrypt(message)
return encrypted_text
def CreacionBinario():
f = open('cfg', 'wb')
columnasConfig[0] = cbDepartamento.get()
columnasConfig[1] = txtContraseñaSave.get()
columnasConfig[2] = lector.get()
columnasConfig[3] = checkedLilWindow2.get()
listOfConfig = [columnasConfig[0], columnasConfig[1], columnasConfig[2], columnasConfig[3]]
f.write(EncrypConfig(listOfConfig))
print('Texto a encryptar: ', listOfConfig)
print('Texto encryptado: ', EncrypConfig(listOfConfig))
f.close()
def LecturaBinario():
with open('cfg', 'rb') as fh:
content = fh.read()
print('Content:', content)
rev_obj = AES.new(secret_key, AES.MODE_CBC, iv)
decrypted_text = rev_obj.decrypt(content)
print("Print the full content of the binary file:")
print(content)
print('The decrypted text: ', decrypted_text.decode('utf-8'))
listFromFile = list[decrypted_text.decode('utf-8')]
return listFromFile[1]
I made a bin file without extension, and that file contain a text that is encrypted, the problem is that when I want to retrieve the text I get this error message
Exception in Tkinter callback
Traceback (most recent call last):
File "C:Usersconitc11AppDataLocalProgramsPythonPython39libtkinter__init__.py",
line 1884, in __call__
return self.func(*args)
File "c:Usersconitc11DesktopPYREPORTEBIOSTAR.py", line 338, in
LecturaBinario
encrypted_text = pad(bytes(content, 'utf-8'), 16)
TypeError: encoding without a string argument
Here is the code
def EncrypConfig(message):
entryMessage = pad(bytes(message, 'utf-8'), 16)
obj = AES.new(secret_key, AES.MODE_CBC, iv)
print('Original message is: ', entryMessage)
#PAD = lambda s: s + (32 - len(s) % 32)*''
encrypted_text = obj.encrypt(entryMessage)
print('The encrypted text', encrypted_text)
return encrypted_text
def CreacionBinario():
f = open('cfg', 'wb')
#byte_arr = [120, 3, 255, 0, 100]
#byte_arr = [columnasConfig[0], columnasConfig[1], columnasConfig[2]]
columnasConfig[0] = cbDepartamento.get()
columnasConfig[1] = '2345G564D5645'
columnasConfig[2] = lector.get()
#counterBoy = 0
#for x in range(3):
#binary_format = columnasConfig[x]
EncrypConfig(columnasConfig[0])
print('Texto a encryptar: ' + columnasConfig[0])
f.write((bytes(str(EncrypConfig(columnasConfig[0])), 'utf-8')))
print('Texto encryptado: ' + str(EncrypConfig(columnasConfig[0])))
#counterBoy += 1
#print(str(counterBoy - 1) + ' CONTADO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
f.close()
def LecturaBinario():
'''
f = open('cfg', 'w+b')
rev_obj = AES.new(secret_key, AES.MODE_CBC, iv)
decrypted_text = rev_obj.decrypt(encrypted_text)
#for x in range(2):
columnasConfig[0] = f.readline(decrypted_text)
f.close()
print('columna 0 ' + columnasConfig[0] + ' columna 1 ' + columnasConfig[1]+ ' columna 2 ' + columnasConfig[2])
'''
#CreacionBinario()
with open('cfg', 'rb') as fh:
content = fh.read()
print('Content:' + str(content))
#encrypted_text = pad(bytes(str(content), 'utf-8'), 16)
encrypted_text = pad(bytes(content, 'utf-8'), 16)
rev_obj = AES.new(secret_key, AES.MODE_CBC, iv)
decrypted_text = rev_obj.decrypt(encrypted_text)
print("Print the full content of the binary file:")
print(content)
print("DecriptedText: ", decrypted_text.decode('utf-8'))
This is the sample code i’m using, i tried not using bytes in content just pad and it display the next error "DecriptedText: <built-in method decode of bytes object at 0x0000019C7EFDFC00>" instead of the decoded output, without pad and bytes the result is "ValueError: Data must be padded to 16 byte boundary in CBC mode"
from email.message import Message
from os import urandom
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
# For Generating cipher text
secret_key = urandom(16)
iv = urandom(16)
obj = AES.new(secret_key, AES.MODE_CBC, iv)
message = pad(bytes('All Users', 'utf-8'), 16)
print('Original message is: ', message)
encrypted_text = obj.encrypt(message)
print('The encrypted text', encrypted_text)
# Decrypt the message
rev_obj = AES.new(secret_key, AES.MODE_CBC, iv)
decrypted_text = rev_obj.decrypt(encrypted_text)
print('The decrypted text', decrypted_text.decode('utf-8'))
SOLUTION.
Well at first place i saw the comments but i didn’t understand what they was saying, they gave me the idea of replacing or changing several code lines the next code line
message = pad(bytes(str(text), 'utf-8'), 16)
replaced this one
entryMessage = pad(bytes(message, 'utf-8'), 16)
This change has no effect in the code but i replace it to have a better order also i moved the line down of the next line
obj = AES.new(secret_key, AES.MODE_CBC, iv)
The next change was replacing this line
f.write(EncrypConfig(listOfConfig))
instead of this line
f.write((bytes(str(EncrypConfig(columnasConfig[0])), 'utf-8')))
This change was the most important, next to this i delete the next line of code
encrypted_text = pad(bytes(content, 'utf-8'), 16)
and some other changes the final result and THE SOLUTION is this:
secret_key = b'somebinarykey12'
iv = b'somebinaryvalue1'
def EncrypConfig(text):
obj = AES.new(secret_key, AES.MODE_CBC, iv)
message = pad(bytes(str(text), 'utf-8'), 16)
encrypted_text = obj.encrypt(message)
return encrypted_text
def CreacionBinario():
f = open('cfg', 'wb')
columnasConfig[0] = cbDepartamento.get()
columnasConfig[1] = txtContraseñaSave.get()
columnasConfig[2] = lector.get()
columnasConfig[3] = checkedLilWindow2.get()
listOfConfig = [columnasConfig[0], columnasConfig[1], columnasConfig[2], columnasConfig[3]]
f.write(EncrypConfig(listOfConfig))
print('Texto a encryptar: ', listOfConfig)
print('Texto encryptado: ', EncrypConfig(listOfConfig))
f.close()
def LecturaBinario():
with open('cfg', 'rb') as fh:
content = fh.read()
print('Content:', content)
rev_obj = AES.new(secret_key, AES.MODE_CBC, iv)
decrypted_text = rev_obj.decrypt(content)
print("Print the full content of the binary file:")
print(content)
print('The decrypted text: ', decrypted_text.decode('utf-8'))
listFromFile = list[decrypted_text.decode('utf-8')]
return listFromFile[1]
SOLUTION.
Well at first place i saw the comments but i didn’t understand what they was saying, they gave me the idea of replacing or changing several code lines the next code line
message = pad(bytes(str(text), 'utf-8'), 16)
replaced this one
entryMessage = pad(bytes(message, 'utf-8'), 16)
This change has no effect in the code but i replace it to have a better order also i moved the line down of the next line
obj = AES.new(secret_key, AES.MODE_CBC, iv)
The next change was replacing this line
f.write(EncrypConfig(listOfConfig))
instead of this line
f.write((bytes(str(EncrypConfig(columnasConfig[0])), 'utf-8')))
This change was the most important, next to this i delete the next line of code
encrypted_text = pad(bytes(content, 'utf-8'), 16)
and some other changes the final result and THE SOLUTION is this:
secret_key = b'somebinarykey12'
iv = b'somebinaryvalue1'
def EncrypConfig(text):
obj = AES.new(secret_key, AES.MODE_CBC, iv)
message = pad(bytes(str(text), 'utf-8'), 16)
encrypted_text = obj.encrypt(message)
return encrypted_text
def CreacionBinario():
f = open('cfg', 'wb')
columnasConfig[0] = cbDepartamento.get()
columnasConfig[1] = txtContraseñaSave.get()
columnasConfig[2] = lector.get()
columnasConfig[3] = checkedLilWindow2.get()
listOfConfig = [columnasConfig[0], columnasConfig[1], columnasConfig[2], columnasConfig[3]]
f.write(EncrypConfig(listOfConfig))
print('Texto a encryptar: ', listOfConfig)
print('Texto encryptado: ', EncrypConfig(listOfConfig))
f.close()
def LecturaBinario():
with open('cfg', 'rb') as fh:
content = fh.read()
print('Content:', content)
rev_obj = AES.new(secret_key, AES.MODE_CBC, iv)
decrypted_text = rev_obj.decrypt(content)
print("Print the full content of the binary file:")
print(content)
print('The decrypted text: ', decrypted_text.decode('utf-8'))
listFromFile = list[decrypted_text.decode('utf-8')]
return listFromFile[1]