byte[]
and exits another encrypted byte[]
, and at the end of the output a checksum is placed; the checksum is a single byte generated by the application made by the asymmetric key, so you can check if the decryption hits the keys, both input and output.
The problem is that if I do byte[] -> byte[]
it works perfectly, encrypting and decrypting. But if I convert these byte[]
to Strings, they only work if I use an Encoding, and give Invalid checksum error if I use another encoding.
string TextoParaEncriptar = "Olá, mundo!";
string encriptado = cipher.EncryptString(TextoBytes); // ok, encripta normalmente
string decriptado = cipher.DecryptString(encriptado); // beleza também
Above the code, the decriptado
field has a value of "Hello world!", but both methods used the Encoding.Default
encoder, which is variable according to the running machine. Now if I specify the encoder, it gives error:
string TextoParaEncriptar = "Olá, mundo!";
string encriptado = cipher.EncryptString(TextoBytes, Encoding.ASCII); // ok, encripta normalmente
string decriptado = cipher.DecryptString(encriptado, Encoding.ASCII); // checksum inválido
These are the code of the methods to encrypt / describe strings:
public string EncryptString(string inputString) => EncryptString(inputString, Encoding.Default);
public string EncryptString(string inputString, Encoding byteEncoder)
{
byte[] strBytes = byteEncoder.GetBytes(inputString);
EncryptByteArray(ref strBytes);
return byteEncoder.GetString(strBytes);
}
public string DecryptString(string inputString) => DecryptString(inputString, Encoding.Default);
public string DecryptString(string inputString, Encoding byteEncoder)
{
byte[] strBytes = byteEncoder.GetBytes(inputString);
DecryptByteArray(ref strBytes);
return byteEncoder.GetString(strBytes);
}
Encrypt and decrypt codes:
public void EncryptByteArray(ref byte[] inputData)
{
if (k == null || k.Length == 0) throw new NullReferenceException("Key cannot be emtpy.");
if (inputData == null || inputData.Length == 0) return;
CryrazCore processor = new CryrazCore() { Positions = k };
{
processor.ComputeByteArray(ref inputData, false);
Array.Resize(ref inputData, inputData.Length + 1);
byte checksum = processor.PushChecksun();
{
inputData[inputData.Length - 1] = checksum;
}
}
}
public void DecryptByteArray(ref byte[] inputData)
{
if (k == null || k.Length == 0) throw new NullReferenceException("Key cannot be emtpy.");
if (inputData == null || inputData.Length == 0) return;
CryrazCore processor = new CryrazCore() { Positions = k };
byte dataChecksum = inputData[inputData.Length - 1];
byte processorChecksum = processor.PushChecksun();
if(dataChecksum != processorChecksum) throw new NullReferenceException("Invalid key for this data. Checksum check failed.");
{
inputData[inputData.Length - 1] = 0;
Array.Resize(ref inputData, inputData.Length - 1);
processor.ComputeByteArray(ref inputData, true);
}
}
processor.ComputeByteArray(ref byte[], boolean)
: This is the method that processes byte-byte frombyte[]
received.EncryptByteArray
inserts thebyte
checksum at the end of the string, theDecryptByteArray
method removes it before processing the decryption.
Why is it making a mistake, even using the same Encoding
to encrypt and decrypt only when Encoding byteEncoder
is not Encoding.Default
? How do I resolve this?
Update
If I use the Western European (ISO) Encoding ISO-8859 , which is SBCS (Single Byte Character Set) , which is one byte for each character, algorithm works normally. But I still do not understand.
The algorithm traverses all bytes received byGetBytes()
and places a checksum at the end of that string of bytes, and then converts them to a String using a GetString(byte[])
so that it has been encrypted, after describing that same encrypted string, says the last byte has been changed.