Re: CryptoStream makes encrypted data bigger than original string

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Burke ATILLA (burke_at_verisoft.com.tr)
Date: 02/06/04


Date: Fri, 6 Feb 2004 20:05:37 +0200

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_smelser@snotmail.com> wrote in message
news:1j1m3g8sh70l6.1kya7wx2gz3j2$.dlg@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.



Relevant Pages

  • Re: Pycrypto RSA ciphertext to string back to ciphertext issue
    ... I'm trying to convert the encrypted data from RSA to a string for ... sending over xmlrpc and then back to usable data. ... from Crypto.PublicKey import RSA ...
    (comp.lang.python)
  • Re: Obtaining length of binary string
    ... >> I read encrypted data from a server and save it in a variable. ... > It is right since you say it's a binary string. ... OP to selectively decide whether he wants characters or bytes to be ...
    (comp.lang.perl.misc)
  • Re: Copying string to byte array
    ... >>byte array and I at the moment I am confident I can figure out how to do ... > Dim sSomeString As String ... > Dim btSomeByteArray() As Byte ... I need to convert that (encrypted data such as that) to a byte array to ...
    (microsoft.public.vb.general.discussion)
  • Re: Encrypted Data Storage Size
    ... CAPICOM can convert the binary encrypted data to/from a string. ... a 50 character database column. ...
    (microsoft.public.platformsdk.security)