I use the code snippet below.
DESCryptoServiceProvider m_csp = new DESCryptoServiceProvider();
m_csp.Mode=CipherMode.ECB;
byte[] pIV = new byte[8];
int outref;
byte[] pData = new byte[8]{0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};;
byte[] pKey = new byte[8]{0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};;
//pData = Verisoft.General.Encoder.Pack(Data,out outref);
byte[] outData = new byte[8];
ICryptoTransform ict=null;
ict = m_csp.CreateEncryptor(pKey,pIV);
outData = ict.TransformFinalBlock(pData,0,8);
string hexString="";
for (int i=0; i<outData.Length; i++)
{
hexString += outData
.ToString("X2");
}
Console.WriteLine("Enc - {0}", hexString);
ict=null;
ict = m_csp.CreateDecryptor(pKey,pIV);
outData = ict.TransformFinalBlock(outData,0,16);
hexString="";
for (int i=0; i<outData.Length; i++)
{
hexString += outData.ToString("X2");
}
Console.WriteLine("Dec - {0}", hexString);
As you see if you use Encrypton result "outData" for decryption it seems
everything going fine. but if you use first 8 bytes you will get an
exception as i mention before "bad data".
so i asked what is last 8 bytes??
Tim Smelser said:
On Fri, 6 Feb 2004 08:31:09 -0800, Burke Atilla wrote:
While encrypting data with DES through CryptoStream makes encrypted
data
bigger than original string. if we have 8 byte key and 8 byte of data then
the mode is ECB. output encrypted data is 16 bytes long. first 8 bytes is
out encrypted key but last 8 byte unknown. and while decrypting if we
couldn't supply this 8 bytes we couldnt decrypt data. and get exception "Bad
Data".
What is this 8 bytes?
and how can i supply this data if i have only the encrypted 8 bytes.
If all that you are encrypting/descrypting is an 8-byte block, rather
than creating a CryptoStream why not just use the TransformFinalBlock
method of the DESCryptoServiceProvider's ICryptoTransform? That way
everything stays in nice 8-byte arrays.
HTH,
Tim