CryptoStream makes encrypted data bigger than original string

G

Guest

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.
 
T

Tim Smelser

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
 
B

Burke ATILLA

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??

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".
 
R

Rob Teixeira [MVP]

m_csp.Padding = PaddingMode.None

That will fix your problem, but realize that you are using relatively weak
encryption here. You're Initialization Vector is all zero, so it's quite
easy to attain just by sheer trial and error. You are using ECB mode, which
supplies no feedback to the cipher chain, which will create repeating
patterns of cipher text if there are repeating patterns in the plain text -
this can expose weaknesses that allow cryptanalysis to decode your key.
Finally, the DES algorithm isn't that strong. I suggest using TripleDES or
AES (with a 256-bit key if possible).

-Rob Teixeira [MVP]

Burke ATILLA said:
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??

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
 
B

Burke ATILLA

Yes that it is. problem fixed. thanks for your comments below. but we are
developing Credit Card Management System and VISA and MASTERCARD uses ECB.
and no way out for this reason.
Thanks again.
Burke.

Rob Teixeira said:
m_csp.Padding = PaddingMode.None

That will fix your problem, but realize that you are using relatively weak
encryption here. You're Initialization Vector is all zero, so it's quite
easy to attain just by sheer trial and error. You are using ECB mode, which
supplies no feedback to the cipher chain, which will create repeating
patterns of cipher text if there are repeating patterns in the plain text -
this can expose weaknesses that allow cryptanalysis to decode your key.
Finally, the DES algorithm isn't that strong. I suggest using TripleDES or
AES (with a 256-bit key if possible).

-Rob Teixeira [MVP]

Burke ATILLA said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top