I try to decrypt the encrypted data of a DataTable
with the following function.
for (int a = 0; a < dataTable.Columns.Count; a++)
{
if (dataTable.Columns[a].DataType != typeof(String)) continue;
for (int b = 0; b < dataTable.Rows.Count; b++)
{
dataTable.Rows[b][a] = SecurityProtocol.DecryptString(dataTable.Rows[b][a].ToString());
}
}
Departure:
| Valor Inicial | Encriptado | Desencriptado |
|------------------------|----------------------------------|------------------------|
|"ITSC" |"aREOeLQEHW0=" |"ITSC" |
|"Desarrollo de software"|"t28FXWcQswHA5FzQq2y8Uc3CMvdQpnQ2"|"Desarrollo de software"|
|"Técnico superior" |"Z9PFyFnDukGzYXs" | >>>> Error <<<< |
But I get an error when trying to decrypt column "Type"
:
Invalid length for a Base-64 array or string.
I tried the function with string
but no error occurred.
public void DecryptStringTest()
{
string encriptado1 = SecurityProtocol.EncriptString("ITSC");
string encriptado2 = SecurityProtocol.EncriptString("Desarrollo de software");
string encriptado3 = SecurityProtocol.EncriptString("Técnico superior");
Console.WriteLine(SecurityProtocol.DecryptString(encriptado1));
Console.WriteLine(SecurityProtocol.DecryptString(encriptado2));
Console.WriteLine(SecurityProtocol.DecryptString(encriptado3));
}
Departure:
| Valor Inicial | Encriptado | Desencriptado |
|------------------------|----------------------------------|------------------------|
|"ITSC" |"aREOeLQEHW0=" |"ITSC" |
|"Desarrollo de software"|"t28FXWcQswHA5FzQq2y8Uc3CMvdQpnQ2"|"Desarrollo de software"|
|"Técnico superior" |"Z9PFyFnDukGzYXs" |"Técnico superior" |
Function code DecryptString(string toDecrypt)
.
public static string DecryptString(string toDecrypt)
{
//Error con DataRow["Type"].ToString() // Value = "Z9PFyFnDukGzYXs"
//Longitud no válida para una matriz o cadena de caracteres Base-64
byte[] stringInBytes = Convert.FromBase64String(toDecrypt);
byte[] resoult = SecurityProtocol.DecryptTripleDES(stringInBytes);
return Encoding.UTF8.GetString(resoult);
}
I have no idea what is causing the problem. If it were for the string, it would give me the same error with the test I did, but it only happens with the DataTable. I hope someone can help me.
Base-64 has padding characters, as you can see in your first encrypted result, at the end it has the = character, since it is what you are missing in the last encrypted data.
Possible cause:
Your database could be truncating characters, check the size of the field.
To know if a fill character is missing or the string was cut, you must do the following.
If that condition is met, your encrypted string is wrong or just missing the padding character.
As you can see, you did a test with String variables, which means that the problem must be the string in the database changes the field limits.
It is recommended not to limit the fields in the database since we will not know what the length of our encrypted string will be, and a varchar(10) occupies the same as a varchar(max).
Example if they want to repair the chain:
String a="YW55IGNhcm5hbCBwbGVhc3U" ;
Here I leave this documentation so that you understand how Base-64 is handled : Base-64