Is encoding needed in this decryption?
Posted
by
Lijo
on Programmers
See other posts from Programmers
or by Lijo
Published on 2012-09-06T05:55:25Z
Indexed on
2012/09/06
9:49 UTC
Read the original article
Hit count: 274
I have a Encryption – Decryption scenario as shown below.
//[Clear text ID string as input] -- [(ASCII GetByte) + Encoding] -- [Encrption as byte array] -- [Database column is in VarBinary] -- [Pass byte[] as VarBinary parameter to SP for comparison]
//[ID stored as VarBinary in Database] -- [Read as byte array] -- [(Decrypt as byte array) + Encoding + (ASCII Get String)] -- Show as string in the UI
My question is in the decryption scenario. After decryption I get a byte array. I am doing an encoding (IBM037) after that. Is it correct? Is there something wrong in the flow shown above?
private static byte[] GetEncryptedID(string id)
{
Interface_Request input = new Interface_Request();
input.RequestText = Encodeto64(id);
input.RequestType = Encryption;
ProgramInterface inputRequest = new ProgramInterface();
inputRequest.Test_Trial_Request = input;
using (KTestService operation = new KTestService())
{
return ((operation.KTrialOperation(inputRequest)).Test_Trial_Response.ResponseText);
}
}
private static string GetDecryptedID(byte[] id)
{
Interface_Request input = new Interface_Request();
input.RequestText = id;
input.RequestType = Decryption;
ProgramInterface request = new ProgramInterface();
request.Test_Trial_Request = input;
using (KTestService operationD = new KTestService())
{
ProgramInterface1 response = operationD.KI014Operation(request);
byte[] decryptedValue = response.ICSF_AES_Response.ResponseText;
Encoding sourceByteFormat = Encoding.GetEncoding("IBM037");
Encoding destinationByteFormat = Encoding.ASCII;
//Convert from one byte format to other (IBM to ASCII)
byte[] ibmEncodedBytes = Encoding.Convert(sourceByteFormat, destinationByteFormat,decryptedValue);
return System.Text.ASCIIEncoding.ASCII.GetString(ibmEncodedBytes);
}
}
private static byte[] EncodeTo64(string toEncode)
{
byte[] dataInBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
Encoding destinationByteFormat = Encoding.GetEncoding("IBM037");
Encoding sourceByteFormat = Encoding.ASCII;
//Convert from one byte format to other (ASCII to IBM)
byte[] asciiBytes = Encoding.Convert(sourceByteFormat, destinationByteFormat, dataInBytes);
return asciiBytes;
}
© Programmers or respective owner