Ok, I'm a programmer on C#, I used to have a program that transform a byte array from the AES key and IV to a base64 string and write this on a file. But I'm with a big problem, I need to made te same thing on Python, and the Python works very bad with byte arrays. I'm with this problem, I tried to generate a key with 'Crypto.Random.get_random_bytes(16)' and convert he to a string, on the C#, its simple, I convert to base64, and the base64 is almost the same that strings, we can concatanate with normal strings, write in files and all the other things. But in the Python when I convert a byte array to a string, give me this error:
key = get_random_bytes(16).decode(encoding="utf-8")
'utf-8' codec can't decode byte 0xaf in position 2: invalid start byte
This doesn't is the same problem all the time, the position and byte change occasionally. And the problems is on the str() too. So, I tried to convert to a base64 string, I haved this problem with the linebreak maker:
breaked = '\n'.join(key[i:i+every] for i in range(0, len(key), every))
TypeError: sequence item 0: expected str instance, bytes found
So, like a distressed person I remove the linebraker, and try put all on one line.
text = "------ Crypton Key Begin ------\n"+key+"\n------ Crypton Key End ------"
TypeError: can only concatenate str (not "bytes") to str
So oook, its very problematic, so I tried write on the file only the base64.
fob.write(key)
TypeError: write() argument must be str, not bytes
I don't know more what can I do, because in the C# a simple code works fine. If help, this is the C# Code:
Aes aesAlgorithm = Aes.Create();
aesAlgorithm.KeySize = 256;
aesAlgorithm.GenerateKey();
aesAlgorithm.GenerateIV();
_key = aesAlgorithm.Key;
_vector = aesAlgorithm.IV;
FileText = string.Format("----- Start Key File -----\n<0>{0}</0>\n<1>{1}</1>\n----- End Key File -----",
Convert.ToBase64String(_key),
Convert.ToBase64String(_vector)
)
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "SEA file (*.sea) | *.sea";
if (saveFileDialog.ShowDialog() == true)
File.WriteAllText(saveFileDialog.FileName, FileText);
else return;
My Python class:
from tkinter.filedialog import asksaveasfilename, askopenfilename
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from base64 import b64encode
class Crypton:
@staticmethod
def GetKey():
return get_random_bytes(16).decode(encoding="utf-8")
@staticmethod
def Encrypt(data, key):
if type(key) != type(str):
if type(key) == type(get_random_bytes(16)):
key = key.decode(encoding='utf-8')
else:
raise Exception("The type of the key is not supported.")
cipher = AES.new(key, AES.MODE_EAX)
ciphertext, tag = cipher.encrypt_and_digest(data)
nonce = cipher.nonce
str_decoded = Crypton.ToString([key, ciphertext, tag, nonce])
return str_decoded
@staticmethod
def Decrypt(str_decoded):
binary_decoded = Crypton.ToBinary(str_decoded)
cipher = AES.new(binary_decoded[0], AES.MODE_EAX, binary_decoded[3])
data = cipher.decrypt_and_verify(binary_decoded[1], binary_decoded[2])
return data
@staticmethod
def Decrypt(key, ciphertext, tag, nonce):
return Crypton.Decrypt([key, ciphertext, tag, nonce])
@staticmethod
def ToBinary(str_decoded):
utf = 'utf-8'
key = str_decoded[0].encode(encoding=utf)
ciphertext = str_decoded[1].encode(encoding=utf)
tag = str_decoded[2].encode(encoding=utf)
nonce = str_decoded[3].encode(encoding=utf)
return [key, ciphertext, tag, nonce]
@staticmethod
def ToString(binary_decoded):
utf = 'utf-8'
key = binary_decoded[0].decode(encoding=utf)
ciphertext = binary_decoded[1].decode(encoding=utf)
tag = binary_decoded[2].decode(encoding=utf)
nonce = binary_decoded[3].decode(encoding=utf)
return [key, ciphertext, tag, nonce]
@staticmethod
def CreateCryptonKeyFile(key):
bricked_key = Crypton.BreakKey(key)
text = "------ Crypton Key Begin ------\n"+bricked_key+"\n------ Crypton Key End ------"
file = asksaveasfilename(
filetypes=[("Crypton Key File", ".cey")],
defaultextension=".cey")
with open(file, 'w') as fob:
fob.write(text)
@staticmethod
def BreakKey(key):
every = 31
breaked = '\n'.join(key[i:i+every] for i in range(0, len(key), every))
return breaked
@staticmethod
def ReadCryptonKeyFile():
file = askopenfilename(
filetypes=[("Crypton Key File", ".cey")],
defaultextension=".cey")
with open(file, 'r').read() as infile:
return Crypton.ReadCryptonKeyString(infile)
@staticmethod
def ReadCryptonKeyString(string):
key = ''.join(
string.replace("------ Crypton Key Begin ------\n", "")\
.replace("\n------ Crypton Key End ------", "")\
.split('\n')
)
return key