By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,566 Members | 1,762 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,566 IT Pros & Developers. It's quick & easy.

CryptoStream makes encrypted data bigger than original string

P: n/a
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.
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
--
Tim Smelser - MVP Visual C#
To email me, make the snot hot.
Nov 15 '05 #2

P: n/a
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[i].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[i].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" <t_*******@snotmail.com> wrote in message
news:1j******************************@40tude.net.. .
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
--
Tim Smelser - MVP Visual C#
To email me, make the snot hot.

Nov 15 '05 #3

P: n/a
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" <bu***@verisoft.com.tr> wrote in message
news:OY**************@TK2MSFTNGP12.phx.gbl...
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[i].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[i].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" <t_*******@snotmail.com> wrote in message
news:1j******************************@40tude.net.. .
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
--
Tim Smelser - MVP Visual C#
To email me, make the snot hot.


Nov 15 '05 #4

P: n/a
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 [MVP]" <RobTeixeira@@msn.com> wrote in message
news:OC**************@tk2msftngp13.phx.gbl...
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" <bu***@verisoft.com.tr> wrote in message
news:OY**************@TK2MSFTNGP12.phx.gbl...
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[i].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[i].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" <t_*******@snotmail.com> wrote in message
news:1j******************************@40tude.net.. .
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
--
Tim Smelser - MVP Visual C#
To email me, make the snot hot.



Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.