TripleDES encryption problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • c duden

    #1

    TripleDES encryption problem

    I am attempting to encrypt some text and be able to decrypt it at a later
    time. I have two methods to do this:

    public static Byte[] EncryptText(str ing textToEncrypt, string
    encryptionHash)
    {
    Byte[] bytearrayinput =
    StringAndByteMa nipulation.Conv ertStringToByte Array(textToEnc rypt);
    //DES instance
    System.Security .Cryptography.T ripleDESCryptoS erviceProvider des = new
    TripleDESCrypto ServiceProvider ();
    // use the default SHA-1 hash algorithm
    string pws = encryptionHash;
    System.Security .Cryptography.P asswordDeriveBy tes db = new
    System.Security .Cryptography.P asswordDeriveBy tes(pws,new byte[0]);
    byte[] prndKey= db.GetBytes(16) ;
    byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
    0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; // from the example in MS
    documentation.
    System.IO.Memor yStream ms = new System.IO.Memor yStream();
    //Create Crypto Stream that transforms text stream using Triple DES
    encryption
    CryptoStream cryptostream = new
    CryptoStream(ms ,des.CreateEncr yptor(prndKey,I V),CryptoStream Mode.Write);
    cryptostream.Wr ite(bytearrayin put,0,bytearray input.Length);

    System.IO.Strea mWriter sw = new System.IO.Strea mWriter(cryptos tream);
    sw.Write(bytear rayinput);
    Byte[] mBytes = new Byte[ms.Length-1];
    ms.Position = 0;
    ms.Read(mBytes, 0,mBytes.Length );
    cryptostream.Cl ose();
    ms.Close();
    return mBytes;
    }

    public static string DeCryptText(Byt e[] textToDecrypt, string
    encryptionHash)
    {
    Byte[] bytearrayinput = textToDecrypt;
    //DES instance
    System.Security .Cryptography.T ripleDESCryptoS erviceProvider des = new
    TripleDESCrypto ServiceProvider ();
    string pws = encryptionHash;
    System.Security .Cryptography.P asswordDeriveBy tes db = new
    System.Security .Cryptography.P asswordDeriveBy tes(pws,new byte[0]);
    byte[] prndKey= db.GetBytes(16) ;
    byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
    0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
    System.IO.Memor yStream ms = new System.IO.Memor yStream(bytearr ayinput);
    //Create Crypto Stream that transforms text stream using Triple DES
    encryption
    CryptoStream cryptostream = new
    CryptoStream(ms ,des.CreateDecr yptor(prndKey,I V),CryptoStream Mode.Read);
    System.IO.Strea mReader SR = new
    System.IO.Strea mReader(cryptos tream,System.Te xt.Encoding.ASC II);
    return SR.ReadToEnd();
    }

    I have tried this about half a hundred ways with the same results :
    It chokes on
    SR.ReadToEnd(); when atttepting to Decrypt the data that was encrypted by
    EncryptText(..)

    Length of the data to decrypt is invalid.
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information
    about the error and where it originated in the code.

    Exception Details: System.Security .Cryptography.C ryptographicExc eption:
    Length of the data to decrypt is invalid.


    Can someone explain what is going on and what I am doing wrong. In looking
    for insight into this I have seen allot of newsgroup posts where people had
    the same problem but no answers.

    Thanks,
    CMD


  • c duden

    #2
    Re: TripleDES encryption problem

    Found something interesting that narrows the scope of the problem somewhat.

    Here are a set of methods (one set of many I have tried):

    public static byte[] DesEncrypt(byte[] data, string hashString)
    {
    //byte[] data = ASCIIEncoding.A SCII.GetBytes(s tringToEncrypt) ;
    byte[] m_bDESKey = ASCIIEncoding.A SCII.GetBytes(h ashString);
    byte[] m_bDESIV = ASCIIEncoding.A SCII.GetBytes(h ashString);
    MemoryStream ms = new MemoryStream(40 96);
    DES des = new DESCryptoServic eProvider() ;
    CryptoStream encStream = new CryptoStream(ms ,
    des.CreateEncry ptor(m_bDESKey, m_bDESIV),
    CryptoStreamMod e.Write);
    encStream.Write (data,0,data.Le ngth);
    encStream.Flush FinalBlock();
    //calculate the length of the encrypted data
    byte[] bResult = new byte[ms.Position];
    ms.Position = 0;
    ms.Read(bResult , 0, bResult.Length) ;
    encStream.Close ();
    return bResult;
    }
    public static byte[] DesDecrypt ( byte[] data, string hashString )
    {
    //byte[] data = ASCIIEncoding.A SCII.GetBytes(s tringToDeCrypt) ;
    byte[] m_bDESKey = ASCIIEncoding.A SCII.GetBytes(h ashString);
    byte[] m_bDESIV = ASCIIEncoding.A SCII.GetBytes(h ashString);
    MemoryStream ms = new MemoryStream(da ta.Length);
    DES des = new DESCryptoServic eProvider() ;
    CryptoStream encStream = new CryptoStream(ms ,
    des.CreateDecry ptor(m_bDESKey, m_bDESIV),
    CryptoStreamMod e.Read);
    ms.Write(data,0 ,data.Length);
    ms.Position = 0;
    string strResult = new StreamReader(en cStream).ReadTo End();
    encStream.Close ();
    return ASCIIEncoding.A SCII.GetBytes(s trResult);
    }

    Doing the following works:
    byte[] data = System.Text.ASC IIEncoding.ASCI I.GetBytes("Hel lo my friend");
    byte[] encrypted =
    NFS.Architectur e.Security.Text Encryption.DesE ncrypt(data,"pa ssword");
    byte[] decrypted =
    NFS.Architectur e.Security.Text Encryption.DesD ecrypt(encrypte d,"password") ;

    Response.Write( encrypted);
    Response.Write( "<BR>");
    Response.Write( decrypted);

    But the following Does not work:

    byte[] data = System.Text.ASC IIEncoding.ASCI I.GetBytes("Hel lo my friend");
    byte[] encrypted =
    NFS.Architectur e.Security.Text Encryption.DesE ncrypt(data,"pa ssword");
    string encryptedStr = System.Text.ASC IIEncoding.ASCI I.GetString(enc rypted);
    byte[] reencrypted = System.Text.ASC IIEncoding.ASCI I.GetBytes(encr yptedStr);
    byte[] decrypted =
    NFS.Architectur e.Security.Text Encryption.DesD ecrypt(reencryp ted,"password") ;
    System.Text.ASC IIEncoding enc = new System.Text.ASC IIEncoding();
    Response.Write( enc.GetString(e ncrypted));
    Response.Write( "<BR>");
    Response.Write( enc.GetString(d ecrypted));

    Somehow converting the output of DesEncrypt to a string and then converting
    that string back to a byte array caused the decryption method to bomb with a
    "Bad Data" error.

    Anyone have any ideas?


    "c duden" <cduden@hotmail .com> wrote in message
    news:uQEHlrkTDH A.2188@TK2MSFTN GP10.phx.gbl...[color=blue]
    > I am attempting to encrypt some text and be able to decrypt it at a later
    > time. I have two methods to do this:
    >
    > public static Byte[] EncryptText(str ing textToEncrypt, string
    > encryptionHash)
    > {
    > Byte[] bytearrayinput =
    > StringAndByteMa nipulation.Conv ertStringToByte Array(textToEnc rypt);
    > //DES instance
    > System.Security .Cryptography.T ripleDESCryptoS erviceProvider des = new
    > TripleDESCrypto ServiceProvider ();
    > // use the default SHA-1 hash algorithm
    > string pws = encryptionHash;
    > System.Security .Cryptography.P asswordDeriveBy tes db = new
    > System.Security .Cryptography.P asswordDeriveBy tes(pws,new byte[0]);
    > byte[] prndKey= db.GetBytes(16) ;
    > byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,[/color]
    0x10,[color=blue]
    > 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; // from the example in MS
    > documentation.
    > System.IO.Memor yStream ms = new System.IO.Memor yStream();
    > //Create Crypto Stream that transforms text stream using Triple DES
    > encryption
    > CryptoStream cryptostream = new
    > CryptoStream(ms ,des.CreateEncr yptor(prndKey,I V),CryptoStream Mode.Write);
    > cryptostream.Wr ite(bytearrayin put,0,bytearray input.Length);
    >
    > System.IO.Strea mWriter sw = new System.IO.Strea mWriter(cryptos tream);
    > sw.Write(bytear rayinput);
    > Byte[] mBytes = new Byte[ms.Length-1];
    > ms.Position = 0;
    > ms.Read(mBytes, 0,mBytes.Length );
    > cryptostream.Cl ose();
    > ms.Close();
    > return mBytes;
    > }
    >
    > public static string DeCryptText(Byt e[] textToDecrypt, string
    > encryptionHash)
    > {
    > Byte[] bytearrayinput = textToDecrypt;
    > //DES instance
    > System.Security .Cryptography.T ripleDESCryptoS erviceProvider des = new
    > TripleDESCrypto ServiceProvider ();
    > string pws = encryptionHash;
    > System.Security .Cryptography.P asswordDeriveBy tes db = new
    > System.Security .Cryptography.P asswordDeriveBy tes(pws,new byte[0]);
    > byte[] prndKey= db.GetBytes(16) ;
    > byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,[/color]
    0x10,[color=blue]
    > 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
    > System.IO.Memor yStream ms = new[/color]
    System.IO.Memor yStream(bytearr ayinput);[color=blue]
    > //Create Crypto Stream that transforms text stream using Triple DES
    > encryption
    > CryptoStream cryptostream = new
    > CryptoStream(ms ,des.CreateDecr yptor(prndKey,I V),CryptoStream Mode.Read);
    > System.IO.Strea mReader SR = new
    > System.IO.Strea mReader(cryptos tream,System.Te xt.Encoding.ASC II);
    > return SR.ReadToEnd();
    > }
    >
    > I have tried this about half a hundred ways with the same results :
    > It chokes on
    > SR.ReadToEnd(); when atttepting to Decrypt the data that was encrypted by
    > EncryptText(..)
    >
    > Length of the data to decrypt is invalid.
    > Description: An unhandled exception occurred during the execution of the
    > current web request. Please review the stack trace for more information
    > about the error and where it originated in the code.
    >
    > Exception Details: System.Security .Cryptography.C ryptographicExc eption:
    > Length of the data to decrypt is invalid.
    >
    >
    > Can someone explain what is going on and what I am doing wrong. In[/color]
    looking[color=blue]
    > for insight into this I have seen allot of newsgroup posts where people[/color]
    had[color=blue]
    > the same problem but no answers.
    >
    > Thanks,
    > CMD
    >
    >[/color]


    Comment

    • c duden

      #3
      Re: TripleDES encryption problem

      Ok, I figured out a workaround -- changed everything to use UnicodeEncoding
      instead of ASCIIEncoding and it now works. BUT does anyone have any insight
      into why this will fail when you use ASCII encoding? I know that in C# all
      strings are Unicode Byte Arrays but why would the conversion not work
      correctly using the Crypto providers?

      Thanks


      "c duden" <cduden@hotmail .com> wrote in message
      news:uJQgkqlTDH A.3088@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Found something interesting that narrows the scope of the problem[/color]
      somewhat.[color=blue]
      >
      > Here are a set of methods (one set of many I have tried):
      >
      > public static byte[] DesEncrypt(byte[] data, string hashString)
      > {
      > //byte[] data = ASCIIEncoding.A SCII.GetBytes(s tringToEncrypt) ;
      > byte[] m_bDESKey = ASCIIEncoding.A SCII.GetBytes(h ashString);
      > byte[] m_bDESIV = ASCIIEncoding.A SCII.GetBytes(h ashString);
      > MemoryStream ms = new MemoryStream(40 96);
      > DES des = new DESCryptoServic eProvider() ;
      > CryptoStream encStream = new CryptoStream(ms ,
      > des.CreateEncry ptor(m_bDESKey, m_bDESIV),
      > CryptoStreamMod e.Write);
      > encStream.Write (data,0,data.Le ngth);
      > encStream.Flush FinalBlock();
      > //calculate the length of the encrypted data
      > byte[] bResult = new byte[ms.Position];
      > ms.Position = 0;
      > ms.Read(bResult , 0, bResult.Length) ;
      > encStream.Close ();
      > return bResult;
      > }
      > public static byte[] DesDecrypt ( byte[] data, string hashString )
      > {
      > //byte[] data = ASCIIEncoding.A SCII.GetBytes(s tringToDeCrypt) ;
      > byte[] m_bDESKey = ASCIIEncoding.A SCII.GetBytes(h ashString);
      > byte[] m_bDESIV = ASCIIEncoding.A SCII.GetBytes(h ashString);
      > MemoryStream ms = new MemoryStream(da ta.Length);
      > DES des = new DESCryptoServic eProvider() ;
      > CryptoStream encStream = new CryptoStream(ms ,
      > des.CreateDecry ptor(m_bDESKey, m_bDESIV),
      > CryptoStreamMod e.Read);
      > ms.Write(data,0 ,data.Length);
      > ms.Position = 0;
      > string strResult = new StreamReader(en cStream).ReadTo End();
      > encStream.Close ();
      > return ASCIIEncoding.A SCII.GetBytes(s trResult);
      > }
      >
      > Doing the following works:
      > byte[] data = System.Text.ASC IIEncoding.ASCI I.GetBytes("Hel lo my friend");
      > byte[] encrypted =
      > NFS.Architectur e.Security.Text Encryption.DesE ncrypt(data,"pa ssword");
      > byte[] decrypted =
      > NFS.Architectur e.Security.Text Encryption.DesD ecrypt(encrypte d,"password") ;
      >
      > Response.Write( encrypted);
      > Response.Write( "<BR>");
      > Response.Write( decrypted);
      >
      > But the following Does not work:
      >
      > byte[] data = System.Text.ASC IIEncoding.ASCI I.GetBytes("Hel lo my friend");
      > byte[] encrypted =
      > NFS.Architectur e.Security.Text Encryption.DesE ncrypt(data,"pa ssword");
      > string encryptedStr =[/color]
      System.Text.ASC IIEncoding.ASCI I.GetString(enc rypted);[color=blue]
      > byte[] reencrypted =[/color]
      System.Text.ASC IIEncoding.ASCI I.GetBytes(encr yptedStr);[color=blue]
      > byte[] decrypted =
      >[/color]
      NFS.Architectur e.Security.Text Encryption.DesD ecrypt(reencryp ted,"password") ;[color=blue]
      > System.Text.ASC IIEncoding enc = new System.Text.ASC IIEncoding();
      > Response.Write( enc.GetString(e ncrypted));
      > Response.Write( "<BR>");
      > Response.Write( enc.GetString(d ecrypted));
      >
      > Somehow converting the output of DesEncrypt to a string and then[/color]
      converting[color=blue]
      > that string back to a byte array caused the decryption method to bomb with[/color]
      a[color=blue]
      > "Bad Data" error.
      >
      > Anyone have any ideas?
      >
      >
      > "c duden" <cduden@hotmail .com> wrote in message
      > news:uQEHlrkTDH A.2188@TK2MSFTN GP10.phx.gbl...[color=green]
      > > I am attempting to encrypt some text and be able to decrypt it at a[/color][/color]
      later[color=blue][color=green]
      > > time. I have two methods to do this:
      > >
      > > public static Byte[] EncryptText(str ing textToEncrypt, string
      > > encryptionHash)
      > > {
      > > Byte[] bytearrayinput =
      > > StringAndByteMa nipulation.Conv ertStringToByte Array(textToEnc rypt);
      > > //DES instance
      > > System.Security .Cryptography.T ripleDESCryptoS erviceProvider des =[/color][/color]
      new[color=blue][color=green]
      > > TripleDESCrypto ServiceProvider ();
      > > // use the default SHA-1 hash algorithm
      > > string pws = encryptionHash;
      > > System.Security .Cryptography.P asswordDeriveBy tes db = new
      > > System.Security .Cryptography.P asswordDeriveBy tes(pws,new byte[0]);
      > > byte[] prndKey= db.GetBytes(16) ;
      > > byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,[/color]
      > 0x10,[color=green]
      > > 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; // from the example in MS
      > > documentation.
      > > System.IO.Memor yStream ms = new System.IO.Memor yStream();
      > > //Create Crypto Stream that transforms text stream using Triple DES
      > > encryption
      > > CryptoStream cryptostream = new
      > > CryptoStream(ms ,des.CreateEncr yptor(prndKey,I V),CryptoStream Mode.Write);
      > > cryptostream.Wr ite(bytearrayin put,0,bytearray input.Length);
      > >
      > > System.IO.Strea mWriter sw = new[/color][/color]
      System.IO.Strea mWriter(cryptos tream);[color=blue][color=green]
      > > sw.Write(bytear rayinput);
      > > Byte[] mBytes = new Byte[ms.Length-1];
      > > ms.Position = 0;
      > > ms.Read(mBytes, 0,mBytes.Length );
      > > cryptostream.Cl ose();
      > > ms.Close();
      > > return mBytes;
      > > }
      > >
      > > public static string DeCryptText(Byt e[] textToDecrypt, string
      > > encryptionHash)
      > > {
      > > Byte[] bytearrayinput = textToDecrypt;
      > > //DES instance
      > > System.Security .Cryptography.T ripleDESCryptoS erviceProvider des =[/color][/color]
      new[color=blue][color=green]
      > > TripleDESCrypto ServiceProvider ();
      > > string pws = encryptionHash;
      > > System.Security .Cryptography.P asswordDeriveBy tes db = new
      > > System.Security .Cryptography.P asswordDeriveBy tes(pws,new byte[0]);
      > > byte[] prndKey= db.GetBytes(16) ;
      > > byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,[/color]
      > 0x10,[color=green]
      > > 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
      > > System.IO.Memor yStream ms = new[/color]
      > System.IO.Memor yStream(bytearr ayinput);[color=green]
      > > //Create Crypto Stream that transforms text stream using Triple DES
      > > encryption
      > > CryptoStream cryptostream = new
      > > CryptoStream(ms ,des.CreateDecr yptor(prndKey,I V),CryptoStream Mode.Read);
      > > System.IO.Strea mReader SR = new
      > > System.IO.Strea mReader(cryptos tream,System.Te xt.Encoding.ASC II);
      > > return SR.ReadToEnd();
      > > }
      > >
      > > I have tried this about half a hundred ways with the same results :
      > > It chokes on
      > > SR.ReadToEnd(); when atttepting to Decrypt the data that was encrypted[/color][/color]
      by[color=blue][color=green]
      > > EncryptText(..)
      > >
      > > Length of the data to decrypt is invalid.
      > > Description: An unhandled exception occurred during the execution of the
      > > current web request. Please review the stack trace for more information
      > > about the error and where it originated in the code.
      > >
      > > Exception Details: System.Security .Cryptography.C ryptographicExc eption:
      > > Length of the data to decrypt is invalid.
      > >
      > >
      > > Can someone explain what is going on and what I am doing wrong. In[/color]
      > looking[color=green]
      > > for insight into this I have seen allot of newsgroup posts where people[/color]
      > had[color=green]
      > > the same problem but no answers.
      > >
      > > Thanks,
      > > CMD
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • gcl

        #4
        Re: TripleDES encryption problem

        give me your code and I can check..
        Your early message causes confusion

        [color=blue]
        >-----Original Message-----
        >Ok, I figured out a workaround -- changed everything to[/color]
        use UnicodeEncoding[color=blue]
        >instead of ASCIIEncoding and it now works. BUT does[/color]
        anyone have any insight[color=blue]
        >into why this will fail when you use ASCII encoding? I[/color]
        know that in C# all[color=blue]
        >strings are Unicode Byte Arrays but why would the[/color]
        conversion not work[color=blue]
        >correctly using the Crypto providers?
        >
        >Thanks
        >
        >
        >"c duden" <cduden@hotmail .com> wrote in message
        >news:uJQgkqlTD HA.3088@TK2MSFT NGP10.phx.gbl.. .[color=green]
        >> Found something interesting that narrows the scope of[/color][/color]
        the problem[color=blue]
        >somewhat.[color=green]
        >>
        >> Here are a set of methods (one set of many I have[/color][/color]
        tried):[color=blue][color=green]
        >>
        >> public static byte[] DesEncrypt(byte[] data, string[/color][/color]
        hashString)[color=blue][color=green]
        >> {
        >> //byte[] data = ASCIIEncoding.A SCII.GetBytes[/color][/color]
        (stringToEncryp t);[color=blue][color=green]
        >> byte[] m_bDESKey = ASCIIEncoding.A SCII.GetBytes[/color][/color]
        (hashString);[color=blue][color=green]
        >> byte[] m_bDESIV = ASCIIEncoding.A SCII.GetBytes[/color][/color]
        (hashString);[color=blue][color=green]
        >> MemoryStream ms = new MemoryStream(40 96);
        >> DES des = new DESCryptoServic eProvider() ;
        >> CryptoStream encStream = new CryptoStream(ms ,
        >> des.CreateEncry ptor(m_bDESKey, m_bDESIV),
        >> CryptoStreamMod e.Write);
        >> encStream.Write (data,0,data.Le ngth);
        >> encStream.Flush FinalBlock();
        >> //calculate the length of the encrypted data
        >> byte[] bResult = new byte[ms.Position];
        >> ms.Position = 0;
        >> ms.Read(bResult , 0, bResult.Length) ;
        >> encStream.Close ();
        >> return bResult;
        >> }
        >> public static byte[] DesDecrypt ( byte[] data, string[/color][/color]
        hashString )[color=blue][color=green]
        >> {
        >> //byte[] data = ASCIIEncoding.A SCII.GetBytes[/color][/color]
        (stringToDeCryp t);[color=blue][color=green]
        >> byte[] m_bDESKey = ASCIIEncoding.A SCII.GetBytes[/color][/color]
        (hashString);[color=blue][color=green]
        >> byte[] m_bDESIV = ASCIIEncoding.A SCII.GetBytes[/color][/color]
        (hashString);[color=blue][color=green]
        >> MemoryStream ms = new MemoryStream(da ta.Length);
        >> DES des = new DESCryptoServic eProvider() ;
        >> CryptoStream encStream = new CryptoStream(ms ,
        >> des.CreateDecry ptor(m_bDESKey, m_bDESIV),
        >> CryptoStreamMod e.Read);
        >> ms.Write(data,0 ,data.Length);
        >> ms.Position = 0;
        >> string strResult = new StreamReader[/color][/color]
        (encStream).Rea dToEnd();[color=blue][color=green]
        >> encStream.Close ();
        >> return ASCIIEncoding.A SCII.GetBytes(s trResult);
        >> }
        >>
        >> Doing the following works:
        >> byte[] data = System.Text.ASC IIEncoding.ASCI I.GetBytes[/color][/color]
        ("Hello my friend");[color=blue][color=green]
        >> byte[] encrypted =
        >> NFS.Architectur e.Security.Text Encryption.DesE ncrypt[/color][/color]
        (data,"password ");[color=blue][color=green]
        >> byte[] decrypted =
        >> NFS.Architectur e.Security.Text Encryption.DesD ecrypt[/color][/color]
        (encrypted,"pas sword");[color=blue][color=green]
        >>
        >> Response.Write( encrypted);
        >> Response.Write( "<BR>");
        >> Response.Write( decrypted);
        >>
        >> But the following Does not work:
        >>
        >> byte[] data = System.Text.ASC IIEncoding.ASCI I.GetBytes[/color][/color]
        ("Hello my friend");[color=blue][color=green]
        >> byte[] encrypted =
        >> NFS.Architectur e.Security.Text Encryption.DesE ncrypt[/color][/color]
        (data,"password ");[color=blue][color=green]
        >> string encryptedStr =[/color]
        >System.Text.AS CIIEncoding.ASC II.GetString(en crypted);[color=green]
        >> byte[] reencrypted =[/color]
        >System.Text.AS CIIEncoding.ASC II.GetBytes(enc ryptedStr);[color=green]
        >> byte[] decrypted =
        >>[/color]
        >NFS.Architectu re.Security.Tex tEncryption.Des Decrypt[/color]
        (reencrypted,"p assword");[color=blue][color=green]
        >> System.Text.ASC IIEncoding enc = new[/color][/color]
        System.Text.ASC IIEncoding();[color=blue][color=green]
        >> Response.Write( enc.GetString(e ncrypted));
        >> Response.Write( "<BR>");
        >> Response.Write( enc.GetString(d ecrypted));
        >>
        >> Somehow converting the output of DesEncrypt to a[/color][/color]
        string and then[color=blue]
        >converting[color=green]
        >> that string back to a byte array caused the decryption[/color][/color]
        method to bomb with[color=blue]
        >a[color=green]
        >> "Bad Data" error.
        >>
        >> Anyone have any ideas?
        >>
        >>
        >> "c duden" <cduden@hotmail .com> wrote in message
        >> news:uQEHlrkTDH A.2188@TK2MSFTN GP10.phx.gbl...[color=darkred]
        >> > I am attempting to encrypt some text and be able to[/color][/color][/color]
        decrypt it at a[color=blue]
        >later[color=green][color=darkred]
        >> > time. I have two methods to do this:
        >> >
        >> > public static Byte[] EncryptText(str ing[/color][/color][/color]
        textToEncrypt, string[color=blue][color=green][color=darkred]
        >> > encryptionHash)
        >> > {
        >> > Byte[] bytearrayinput =
        >> > StringAndByteMa nipulation.Conv ertStringToByte Array[/color][/color][/color]
        (textToEncrypt) ;[color=blue][color=green][color=darkred]
        >> > //DES instance
        >> >[/color][/color][/color]
        System.Security .Cryptography.T ripleDESCryptoS erviceProvide
        r des =[color=blue]
        >new[color=green][color=darkred]
        >> > TripleDESCrypto ServiceProvider ();
        >> > // use the default SHA-1 hash algorithm
        >> > string pws = encryptionHash;
        >> > System.Security .Cryptography.P asswordDeriveBy tes[/color][/color][/color]
        db = new[color=blue][color=green][color=darkred]
        >> > System.Security .Cryptography.P asswordDeriveBy tes[/color][/color][/color]
        (pws,new byte[0]);[color=blue][color=green][color=darkred]
        >> > byte[] prndKey= db.GetBytes(16) ;
        >> > byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06,[/color][/color][/color]
        0x07, 0x08, 0x09,[color=blue][color=green]
        >> 0x10,[color=darkred]
        >> > 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; // from the[/color][/color][/color]
        example in MS[color=blue][color=green][color=darkred]
        >> > documentation.
        >> > System.IO.Memor yStream ms = new[/color][/color][/color]
        System.IO.Memor yStream();[color=blue][color=green][color=darkred]
        >> > //Create Crypto Stream that transforms text[/color][/color][/color]
        stream using Triple DES[color=blue][color=green][color=darkred]
        >> > encryption
        >> > CryptoStream cryptostream = new
        >> > CryptoStream(ms ,des.CreateEncr yptor[/color][/color][/color]
        (prndKey,IV),Cr yptoStreamMode. Write);[color=blue][color=green][color=darkred]
        >> > cryptostream.Wr ite[/color][/color][/color]
        (bytearrayinput ,0,bytearrayinp ut.Length);[color=blue][color=green][color=darkred]
        >> >
        >> > System.IO.Strea mWriter sw = new[/color][/color]
        >System.IO.Stre amWriter(crypto stream);[color=green][color=darkred]
        >> > sw.Write(bytear rayinput);
        >> > Byte[] mBytes = new Byte[ms.Length-1];
        >> > ms.Position = 0;
        >> > ms.Read(mBytes, 0,mBytes.Length );
        >> > cryptostream.Cl ose();
        >> > ms.Close();
        >> > return mBytes;
        >> > }
        >> >
        >> > public static string DeCryptText(Byt e[][/color][/color][/color]
        textToDecrypt, string[color=blue][color=green][color=darkred]
        >> > encryptionHash)
        >> > {
        >> > Byte[] bytearrayinput = textToDecrypt;
        >> > //DES instance
        >> >[/color][/color][/color]
        System.Security .Cryptography.T ripleDESCryptoS erviceProvide
        r des =[color=blue]
        >new[color=green][color=darkred]
        >> > TripleDESCrypto ServiceProvider ();
        >> > string pws = encryptionHash;
        >> > System.Security .Cryptography.P asswordDeriveBy tes[/color][/color][/color]
        db = new[color=blue][color=green][color=darkred]
        >> > System.Security .Cryptography.P asswordDeriveBy tes[/color][/color][/color]
        (pws,new byte[0]);[color=blue][color=green][color=darkred]
        >> > byte[] prndKey= db.GetBytes(16) ;
        >> > byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06,[/color][/color][/color]
        0x07, 0x08, 0x09,[color=blue][color=green]
        >> 0x10,[color=darkred]
        >> > 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
        >> > System.IO.Memor yStream ms = new[/color]
        >> System.IO.Memor yStream(bytearr ayinput);[color=darkred]
        >> > //Create Crypto Stream that transforms text[/color][/color][/color]
        stream using Triple DES[color=blue][color=green][color=darkred]
        >> > encryption
        >> > CryptoStream cryptostream = new
        >> > CryptoStream(ms ,des.CreateDecr yptor[/color][/color][/color]
        (prndKey,IV),Cr yptoStreamMode. Read);[color=blue][color=green][color=darkred]
        >> > System.IO.Strea mReader SR = new
        >> > System.IO.Strea mReader[/color][/color][/color]
        (cryptostream,S ystem.Text.Enco ding.ASCII);[color=blue][color=green][color=darkred]
        >> > return SR.ReadToEnd();
        >> > }
        >> >
        >> > I have tried this about half a hundred ways with the[/color][/color][/color]
        same results :[color=blue][color=green][color=darkred]
        >> > It chokes on
        >> > SR.ReadToEnd(); when atttepting to Decrypt the data[/color][/color][/color]
        that was encrypted[color=blue]
        >by[color=green][color=darkred]
        >> > EncryptText(..)
        >> >
        >> > Length of the data to decrypt is invalid.
        >> > Description: An unhandled exception occurred during[/color][/color][/color]
        the execution of the[color=blue][color=green][color=darkred]
        >> > current web request. Please review the stack trace[/color][/color][/color]
        for more information[color=blue][color=green][color=darkred]
        >> > about the error and where it originated in the code.
        >> >
        >> > Exception Details:[/color][/color][/color]
        System.Security .Cryptography.C ryptographicExc eption:[color=blue][color=green][color=darkred]
        >> > Length of the data to decrypt is invalid.
        >> >
        >> >
        >> > Can someone explain what is going on and what I am[/color][/color][/color]
        doing wrong. In[color=blue][color=green]
        >> looking[color=darkred]
        >> > for insight into this I have seen allot of newsgroup[/color][/color][/color]
        posts where people[color=blue][color=green]
        >> had[color=darkred]
        >> > the same problem but no answers.
        >> >
        >> > Thanks,
        >> > CMD
        >> >
        >> >[/color]
        >>
        >>[/color]
        >
        >
        >.
        >[/color]

        Comment

        • c duden

          #5
          Re: TripleDES encryption problem

          Not sure how it was confusing but this is what I am now using and it works:

          public static string DesEncrypt(stri ng stringToEncrypt , string hashString)
          {
          byte[] data = ConvertStringTo ByteArray(strin gToEncrypt);
          string pws = hashString;
          System.Security .Cryptography.P asswordDeriveBy tes db = new
          System.Security .Cryptography.P asswordDeriveBy tes(pws,new byte[0]);
          byte[] m_bDESKey= db.GetBytes(16) ;
          TripleDESCrypto ServiceProvider des = new TripleDESCrypto ServiceProvider ();
          des.Mode = CipherMode.CBC;
          byte[] m_bDESIV = UnicodeEncoding .Unicode.GetByt es(hashString);
          MemoryStream ms = new MemoryStream(40 96);
          CryptoStream encStream = new CryptoStream(ms ,
          des.CreateEncry ptor(m_bDESKey, m_bDESIV),
          CryptoStreamMod e.Write);
          encStream.Write (data,0,data.Le ngth);
          encStream.Flush FinalBlock();
          //calculate the length of the encrypted data
          byte[] bResult = new byte[ms.Position];
          ms.Position = 0;
          ms.Read(bResult , 0, bResult.Length) ;
          encStream.Close ();
          return UnicodeEncoding .Unicode.GetStr ing(bResult);
          }
          public static string DesDecrypt ( string stringToDeCrypt , string
          hashString )
          {
          byte[] data = ConvertStringTo ByteArray(strin gToDeCrypt);
          string pws = hashString;
          System.Security .Cryptography.P asswordDeriveBy tes db = new
          System.Security .Cryptography.P asswordDeriveBy tes(pws,new byte[0]);
          byte[] m_bDESKey= db.GetBytes(16) ;
          TripleDESCrypto ServiceProvider des = new TripleDESCrypto ServiceProvider ();
          des.Mode = CipherMode.CBC;
          byte[] m_bDESIV = UnicodeEncoding .Unicode.GetByt es(hashString);
          MemoryStream ms = new MemoryStream(da ta.Length);
          CryptoStream encStream = new CryptoStream(ms ,
          des.CreateDecry ptor(m_bDESKey, m_bDESIV),
          CryptoStreamMod e.Read);
          ms.Write(data,0 ,data.Length);
          ms.Position = 0;
          string strResult = new StreamReader(en cStream).ReadTo End();
          encStream.Close ();
          return strResult;
          }

          private Byte[] ConvertStringTo ByteArray(Strin g s)
          {
          Byte[] returnValue = UnicodeEncoding .Unicode.GetByt es(s);
          return returnValue;
          }

          In my test harness this was how I tested the methods above:

          string encrypted =
          NFS.Architectur e.Security.Text Encryption.DesE ncrypt("Hello
          World","passwor d");
          string decrypted =
          NFS.Architectur e.Security.Text Encryption.DesD ecrypt(encrypte d,"password") ;
          Console.WriteLi ne(encrypted);
          Console.WriteLi ne(decrypted);
          Console.ReadLin e();

          Now if you replace UnicodeEncoding .Unicode with ASCIIEncoding.A SCII
          everywhere it is used in the code above it will fail when you attempt to
          decrypt what is encrypted.

          Essentially I would just like a better understanding of why that is. What
          is going on internally that makes it fail.

          Thanks.


          "gcl" <lnguyen59@comc ast.net> wrote in message
          news:02b001c34e 79$688f3190$a40 1280a@phx.gbl.. .[color=blue]
          > give me your code and I can check..
          > Your early message causes confusion
          >
          >[color=green]
          > >-----Original Message-----
          > >Ok, I figured out a workaround -- changed everything to[/color]
          > use UnicodeEncoding[color=green]
          > >instead of ASCIIEncoding and it now works. BUT does[/color]
          > anyone have any insight[color=green]
          > >into why this will fail when you use ASCII encoding? I[/color]
          > know that in C# all[color=green]
          > >strings are Unicode Byte Arrays but why would the[/color]
          > conversion not work[color=green]
          > >correctly using the Crypto providers?
          > >
          > >Thanks
          > >
          > >
          > >"c duden" <cduden@hotmail .com> wrote in message
          > >news:uJQgkqlTD HA.3088@TK2MSFT NGP10.phx.gbl.. .[color=darkred]
          > >> Found something interesting that narrows the scope of[/color][/color]
          > the problem[color=green]
          > >somewhat.[color=darkred]
          > >>
          > >> Here are a set of methods (one set of many I have[/color][/color]
          > tried):[color=green][color=darkred]
          > >>
          > >> public static byte[] DesEncrypt(byte[] data, string[/color][/color]
          > hashString)[color=green][color=darkred]
          > >> {
          > >> //byte[] data = ASCIIEncoding.A SCII.GetBytes[/color][/color]
          > (stringToEncryp t);[color=green][color=darkred]
          > >> byte[] m_bDESKey = ASCIIEncoding.A SCII.GetBytes[/color][/color]
          > (hashString);[color=green][color=darkred]
          > >> byte[] m_bDESIV = ASCIIEncoding.A SCII.GetBytes[/color][/color]
          > (hashString);[color=green][color=darkred]
          > >> MemoryStream ms = new MemoryStream(40 96);
          > >> DES des = new DESCryptoServic eProvider() ;
          > >> CryptoStream encStream = new CryptoStream(ms ,
          > >> des.CreateEncry ptor(m_bDESKey, m_bDESIV),
          > >> CryptoStreamMod e.Write);
          > >> encStream.Write (data,0,data.Le ngth);
          > >> encStream.Flush FinalBlock();
          > >> //calculate the length of the encrypted data
          > >> byte[] bResult = new byte[ms.Position];
          > >> ms.Position = 0;
          > >> ms.Read(bResult , 0, bResult.Length) ;
          > >> encStream.Close ();
          > >> return bResult;
          > >> }
          > >> public static byte[] DesDecrypt ( byte[] data, string[/color][/color]
          > hashString )[color=green][color=darkred]
          > >> {
          > >> //byte[] data = ASCIIEncoding.A SCII.GetBytes[/color][/color]
          > (stringToDeCryp t);[color=green][color=darkred]
          > >> byte[] m_bDESKey = ASCIIEncoding.A SCII.GetBytes[/color][/color]
          > (hashString);[color=green][color=darkred]
          > >> byte[] m_bDESIV = ASCIIEncoding.A SCII.GetBytes[/color][/color]
          > (hashString);[color=green][color=darkred]
          > >> MemoryStream ms = new MemoryStream(da ta.Length);
          > >> DES des = new DESCryptoServic eProvider() ;
          > >> CryptoStream encStream = new CryptoStream(ms ,
          > >> des.CreateDecry ptor(m_bDESKey, m_bDESIV),
          > >> CryptoStreamMod e.Read);
          > >> ms.Write(data,0 ,data.Length);
          > >> ms.Position = 0;
          > >> string strResult = new StreamReader[/color][/color]
          > (encStream).Rea dToEnd();[color=green][color=darkred]
          > >> encStream.Close ();
          > >> return ASCIIEncoding.A SCII.GetBytes(s trResult);
          > >> }
          > >>
          > >> Doing the following works:
          > >> byte[] data = System.Text.ASC IIEncoding.ASCI I.GetBytes[/color][/color]
          > ("Hello my friend");[color=green][color=darkred]
          > >> byte[] encrypted =
          > >> NFS.Architectur e.Security.Text Encryption.DesE ncrypt[/color][/color]
          > (data,"password ");[color=green][color=darkred]
          > >> byte[] decrypted =
          > >> NFS.Architectur e.Security.Text Encryption.DesD ecrypt[/color][/color]
          > (encrypted,"pas sword");[color=green][color=darkred]
          > >>
          > >> Response.Write( encrypted);
          > >> Response.Write( "<BR>");
          > >> Response.Write( decrypted);
          > >>
          > >> But the following Does not work:
          > >>
          > >> byte[] data = System.Text.ASC IIEncoding.ASCI I.GetBytes[/color][/color]
          > ("Hello my friend");[color=green][color=darkred]
          > >> byte[] encrypted =
          > >> NFS.Architectur e.Security.Text Encryption.DesE ncrypt[/color][/color]
          > (data,"password ");[color=green][color=darkred]
          > >> string encryptedStr =[/color]
          > >System.Text.AS CIIEncoding.ASC II.GetString(en crypted);[color=darkred]
          > >> byte[] reencrypted =[/color]
          > >System.Text.AS CIIEncoding.ASC II.GetBytes(enc ryptedStr);[color=darkred]
          > >> byte[] decrypted =
          > >>[/color]
          > >NFS.Architectu re.Security.Tex tEncryption.Des Decrypt[/color]
          > (reencrypted,"p assword");[color=green][color=darkred]
          > >> System.Text.ASC IIEncoding enc = new[/color][/color]
          > System.Text.ASC IIEncoding();[color=green][color=darkred]
          > >> Response.Write( enc.GetString(e ncrypted));
          > >> Response.Write( "<BR>");
          > >> Response.Write( enc.GetString(d ecrypted));
          > >>
          > >> Somehow converting the output of DesEncrypt to a[/color][/color]
          > string and then[color=green]
          > >converting[color=darkred]
          > >> that string back to a byte array caused the decryption[/color][/color]
          > method to bomb with[color=green]
          > >a[color=darkred]
          > >> "Bad Data" error.
          > >>
          > >> Anyone have any ideas?
          > >>
          > >>
          > >> "c duden" <cduden@hotmail .com> wrote in message
          > >> news:uQEHlrkTDH A.2188@TK2MSFTN GP10.phx.gbl...
          > >> > I am attempting to encrypt some text and be able to[/color][/color]
          > decrypt it at a[color=green]
          > >later[color=darkred]
          > >> > time. I have two methods to do this:
          > >> >
          > >> > public static Byte[] EncryptText(str ing[/color][/color]
          > textToEncrypt, string[color=green][color=darkred]
          > >> > encryptionHash)
          > >> > {
          > >> > Byte[] bytearrayinput =
          > >> > StringAndByteMa nipulation.Conv ertStringToByte Array[/color][/color]
          > (textToEncrypt) ;[color=green][color=darkred]
          > >> > //DES instance
          > >> >[/color][/color]
          > System.Security .Cryptography.T ripleDESCryptoS erviceProvide
          > r des =[color=green]
          > >new[color=darkred]
          > >> > TripleDESCrypto ServiceProvider ();
          > >> > // use the default SHA-1 hash algorithm
          > >> > string pws = encryptionHash;
          > >> > System.Security .Cryptography.P asswordDeriveBy tes[/color][/color]
          > db = new[color=green][color=darkred]
          > >> > System.Security .Cryptography.P asswordDeriveBy tes[/color][/color]
          > (pws,new byte[0]);[color=green][color=darkred]
          > >> > byte[] prndKey= db.GetBytes(16) ;
          > >> > byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06,[/color][/color]
          > 0x07, 0x08, 0x09,[color=green][color=darkred]
          > >> 0x10,
          > >> > 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; // from the[/color][/color]
          > example in MS[color=green][color=darkred]
          > >> > documentation.
          > >> > System.IO.Memor yStream ms = new[/color][/color]
          > System.IO.Memor yStream();[color=green][color=darkred]
          > >> > //Create Crypto Stream that transforms text[/color][/color]
          > stream using Triple DES[color=green][color=darkred]
          > >> > encryption
          > >> > CryptoStream cryptostream = new
          > >> > CryptoStream(ms ,des.CreateEncr yptor[/color][/color]
          > (prndKey,IV),Cr yptoStreamMode. Write);[color=green][color=darkred]
          > >> > cryptostream.Wr ite[/color][/color]
          > (bytearrayinput ,0,bytearrayinp ut.Length);[color=green][color=darkred]
          > >> >
          > >> > System.IO.Strea mWriter sw = new[/color]
          > >System.IO.Stre amWriter(crypto stream);[color=darkred]
          > >> > sw.Write(bytear rayinput);
          > >> > Byte[] mBytes = new Byte[ms.Length-1];
          > >> > ms.Position = 0;
          > >> > ms.Read(mBytes, 0,mBytes.Length );
          > >> > cryptostream.Cl ose();
          > >> > ms.Close();
          > >> > return mBytes;
          > >> > }
          > >> >
          > >> > public static string DeCryptText(Byt e[][/color][/color]
          > textToDecrypt, string[color=green][color=darkred]
          > >> > encryptionHash)
          > >> > {
          > >> > Byte[] bytearrayinput = textToDecrypt;
          > >> > //DES instance
          > >> >[/color][/color]
          > System.Security .Cryptography.T ripleDESCryptoS erviceProvide
          > r des =[color=green]
          > >new[color=darkred]
          > >> > TripleDESCrypto ServiceProvider ();
          > >> > string pws = encryptionHash;
          > >> > System.Security .Cryptography.P asswordDeriveBy tes[/color][/color]
          > db = new[color=green][color=darkred]
          > >> > System.Security .Cryptography.P asswordDeriveBy tes[/color][/color]
          > (pws,new byte[0]);[color=green][color=darkred]
          > >> > byte[] prndKey= db.GetBytes(16) ;
          > >> > byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06,[/color][/color]
          > 0x07, 0x08, 0x09,[color=green][color=darkred]
          > >> 0x10,
          > >> > 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
          > >> > System.IO.Memor yStream ms = new
          > >> System.IO.Memor yStream(bytearr ayinput);
          > >> > //Create Crypto Stream that transforms text[/color][/color]
          > stream using Triple DES[color=green][color=darkred]
          > >> > encryption
          > >> > CryptoStream cryptostream = new
          > >> > CryptoStream(ms ,des.CreateDecr yptor[/color][/color]
          > (prndKey,IV),Cr yptoStreamMode. Read);[color=green][color=darkred]
          > >> > System.IO.Strea mReader SR = new
          > >> > System.IO.Strea mReader[/color][/color]
          > (cryptostream,S ystem.Text.Enco ding.ASCII);[color=green][color=darkred]
          > >> > return SR.ReadToEnd();
          > >> > }
          > >> >
          > >> > I have tried this about half a hundred ways with the[/color][/color]
          > same results :[color=green][color=darkred]
          > >> > It chokes on
          > >> > SR.ReadToEnd(); when atttepting to Decrypt the data[/color][/color]
          > that was encrypted[color=green]
          > >by[color=darkred]
          > >> > EncryptText(..)
          > >> >
          > >> > Length of the data to decrypt is invalid.
          > >> > Description: An unhandled exception occurred during[/color][/color]
          > the execution of the[color=green][color=darkred]
          > >> > current web request. Please review the stack trace[/color][/color]
          > for more information[color=green][color=darkred]
          > >> > about the error and where it originated in the code.
          > >> >
          > >> > Exception Details:[/color][/color]
          > System.Security .Cryptography.C ryptographicExc eption:[color=green][color=darkred]
          > >> > Length of the data to decrypt is invalid.
          > >> >
          > >> >
          > >> > Can someone explain what is going on and what I am[/color][/color]
          > doing wrong. In[color=green][color=darkred]
          > >> looking
          > >> > for insight into this I have seen allot of newsgroup[/color][/color]
          > posts where people[color=green][color=darkred]
          > >> had
          > >> > the same problem but no answers.
          > >> >
          > >> > Thanks,
          > >> > CMD
          > >> >
          > >> >
          > >>
          > >>[/color]
          > >
          > >
          > >.
          > >[/color][/color]


          Comment

          • c duden

            #6
            Re: TripleDES encryption problem

            Ok, Michael that makes perfect sense when you put it that way. Follow up
            question then -- with the Unicode working verison I insert that encrypted
            data into a field in SQL2k -- (nvarchar -- supposed to be able to handle
            Unicode values) but when I pull it out it can't unencode it -- fails with
            "bad data" error. Is SQL doing something in the translation?


            "Michael Giagnocavo [MVP]" <mggUNSPAM@Atre vido.net> wrote in message
            news:%23sBwxo5T DHA.3132@tk2msf tngp13.phx.gbl. ..[color=blue]
            > ASCII is only a 7-bit encoding, and your key uses the full 8-bit space
            > of each byte. Thus, writing your key to ASCII results in some data
            > being messed up.
            >
            > -mike
            > MVP
            >
            > "c duden" <cduden@hotmail .com> wrote in message
            > news:u2oVr2lTDH A.2460@TK2MSFTN GP10.phx.gbl...[color=green]
            > > Ok, I figured out a workaround -- changed everything to use[/color]
            > UnicodeEncoding[color=green]
            > > instead of ASCIIEncoding and it now works. BUT does anyone have any[/color]
            > insight[color=green]
            > > into why this will fail when you use ASCII encoding? I know that in[/color]
            > C# all[color=green]
            > > strings are Unicode Byte Arrays but why would the conversion not[/color]
            > work[color=green]
            > > correctly using the Crypto providers?
            > >
            > > Thanks
            > >
            > >
            > > "c duden" <cduden@hotmail .com> wrote in message
            > > news:uJQgkqlTDH A.3088@TK2MSFTN GP10.phx.gbl...[color=darkred]
            > > > Found something interesting that narrows the scope of the problem[/color]
            > > somewhat.[color=darkred]
            > > >
            > > > Here are a set of methods (one set of many I have tried):
            > > >
            > > > public static byte[] DesEncrypt(byte[] data, string hashString)
            > > > {
            > > > //byte[] data = ASCIIEncoding.A SCII.GetBytes(s tringToEncrypt) ;
            > > > byte[] m_bDESKey = ASCIIEncoding.A SCII.GetBytes(h ashString);
            > > > byte[] m_bDESIV = ASCIIEncoding.A SCII.GetBytes(h ashString);
            > > > MemoryStream ms = new MemoryStream(40 96);
            > > > DES des = new DESCryptoServic eProvider() ;
            > > > CryptoStream encStream = new CryptoStream(ms ,
            > > > des.CreateEncry ptor(m_bDESKey, m_bDESIV),
            > > > CryptoStreamMod e.Write);
            > > > encStream.Write (data,0,data.Le ngth);
            > > > encStream.Flush FinalBlock();
            > > > //calculate the length of the encrypted data
            > > > byte[] bResult = new byte[ms.Position];
            > > > ms.Position = 0;
            > > > ms.Read(bResult , 0, bResult.Length) ;
            > > > encStream.Close ();
            > > > return bResult;
            > > > }
            > > > public static byte[] DesDecrypt ( byte[] data, string hashString )
            > > > {
            > > > //byte[] data = ASCIIEncoding.A SCII.GetBytes(s tringToDeCrypt) ;
            > > > byte[] m_bDESKey = ASCIIEncoding.A SCII.GetBytes(h ashString);
            > > > byte[] m_bDESIV = ASCIIEncoding.A SCII.GetBytes(h ashString);
            > > > MemoryStream ms = new MemoryStream(da ta.Length);
            > > > DES des = new DESCryptoServic eProvider() ;
            > > > CryptoStream encStream = new CryptoStream(ms ,
            > > > des.CreateDecry ptor(m_bDESKey, m_bDESIV),
            > > > CryptoStreamMod e.Read);
            > > > ms.Write(data,0 ,data.Length);
            > > > ms.Position = 0;
            > > > string strResult = new StreamReader(en cStream).ReadTo End();
            > > > encStream.Close ();
            > > > return ASCIIEncoding.A SCII.GetBytes(s trResult);
            > > > }
            > > >
            > > > Doing the following works:
            > > > byte[] data = System.Text.ASC IIEncoding.ASCI I.GetBytes("Hel lo my[/color][/color]
            > friend");[color=green][color=darkred]
            > > > byte[] encrypted =
            > > >[/color][/color]
            > NFS.Architectur e.Security.Text Encryption.DesE ncrypt(data,"pa ssword");[color=green][color=darkred]
            > > > byte[] decrypted =
            > > >[/color][/color]
            > NFS.Architectur e.Security.Text Encryption.DesD ecrypt(encrypte d,"passwor
            > d");[color=green][color=darkred]
            > > >
            > > > Response.Write( encrypted);
            > > > Response.Write( "<BR>");
            > > > Response.Write( decrypted);
            > > >
            > > > But the following Does not work:
            > > >
            > > > byte[] data = System.Text.ASC IIEncoding.ASCI I.GetBytes("Hel lo my[/color][/color]
            > friend");[color=green][color=darkred]
            > > > byte[] encrypted =
            > > >[/color][/color]
            > NFS.Architectur e.Security.Text Encryption.DesE ncrypt(data,"pa ssword");[color=green][color=darkred]
            > > > string encryptedStr =[/color]
            > > System.Text.ASC IIEncoding.ASCI I.GetString(enc rypted);[color=darkred]
            > > > byte[] reencrypted =[/color]
            > > System.Text.ASC IIEncoding.ASCI I.GetBytes(encr yptedStr);[color=darkred]
            > > > byte[] decrypted =
            > > >[/color]
            > >[/color]
            > NFS.Architectur e.Security.Text Encryption.DesD ecrypt(reencryp ted,"passw
            > ord");[color=green][color=darkred]
            > > > System.Text.ASC IIEncoding enc = new System.Text.ASC IIEncoding();
            > > > Response.Write( enc.GetString(e ncrypted));
            > > > Response.Write( "<BR>");
            > > > Response.Write( enc.GetString(d ecrypted));
            > > >
            > > > Somehow converting the output of DesEncrypt to a string and then[/color]
            > > converting[color=darkred]
            > > > that string back to a byte array caused the decryption method to[/color][/color]
            > bomb with[color=green]
            > > a[color=darkred]
            > > > "Bad Data" error.
            > > >
            > > > Anyone have any ideas?
            > > >
            > > >
            > > > "c duden" <cduden@hotmail .com> wrote in message
            > > > news:uQEHlrkTDH A.2188@TK2MSFTN GP10.phx.gbl...
            > > > > I am attempting to encrypt some text and be able to decrypt it[/color][/color]
            > at a[color=green]
            > > later[color=darkred]
            > > > > time. I have two methods to do this:
            > > > >
            > > > > public static Byte[] EncryptText(str ing textToEncrypt, string
            > > > > encryptionHash)
            > > > > {
            > > > > Byte[] bytearrayinput =
            > > > >[/color][/color]
            > StringAndByteMa nipulation.Conv ertStringToByte Array(textToEnc rypt);[color=green][color=darkred]
            > > > > //DES instance
            > > > > System.Security .Cryptography.T ripleDESCryptoS erviceProvider[/color][/color]
            > des =[color=green]
            > > new[color=darkred]
            > > > > TripleDESCrypto ServiceProvider ();
            > > > > // use the default SHA-1 hash algorithm
            > > > > string pws = encryptionHash;
            > > > > System.Security .Cryptography.P asswordDeriveBy tes db = new
            > > > > System.Security .Cryptography.P asswordDeriveBy tes(pws,new[/color][/color]
            > byte[0]);[color=green][color=darkred]
            > > > > byte[] prndKey= db.GetBytes(16) ;
            > > > > byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,[/color][/color]
            > 0x09,[color=green][color=darkred]
            > > > 0x10,
            > > > > 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; // from the example in MS
            > > > > documentation.
            > > > > System.IO.Memor yStream ms = new System.IO.Memor yStream();
            > > > > //Create Crypto Stream that transforms text stream using[/color][/color]
            > Triple DES[color=green][color=darkred]
            > > > > encryption
            > > > > CryptoStream cryptostream = new
            > > > >[/color][/color]
            > CryptoStream(ms ,des.CreateEncr yptor(prndKey,I V),CryptoStream Mode.Write
            > );[color=green][color=darkred]
            > > > > cryptostream.Wr ite(bytearrayin put,0,bytearray input.Length);
            > > > >
            > > > > System.IO.Strea mWriter sw = new[/color]
            > > System.IO.Strea mWriter(cryptos tream);[color=darkred]
            > > > > sw.Write(bytear rayinput);
            > > > > Byte[] mBytes = new Byte[ms.Length-1];
            > > > > ms.Position = 0;
            > > > > ms.Read(mBytes, 0,mBytes.Length );
            > > > > cryptostream.Cl ose();
            > > > > ms.Close();
            > > > > return mBytes;
            > > > > }
            > > > >
            > > > > public static string DeCryptText(Byt e[] textToDecrypt, string
            > > > > encryptionHash)
            > > > > {
            > > > > Byte[] bytearrayinput = textToDecrypt;
            > > > > //DES instance
            > > > > System.Security .Cryptography.T ripleDESCryptoS erviceProvider[/color][/color]
            > des =[color=green]
            > > new[color=darkred]
            > > > > TripleDESCrypto ServiceProvider ();
            > > > > string pws = encryptionHash;
            > > > > System.Security .Cryptography.P asswordDeriveBy tes db = new
            > > > > System.Security .Cryptography.P asswordDeriveBy tes(pws,new[/color][/color]
            > byte[0]);[color=green][color=darkred]
            > > > > byte[] prndKey= db.GetBytes(16) ;
            > > > > byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,[/color][/color]
            > 0x09,[color=green][color=darkred]
            > > > 0x10,
            > > > > 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
            > > > > System.IO.Memor yStream ms = new
            > > > System.IO.Memor yStream(bytearr ayinput);
            > > > > //Create Crypto Stream that transforms text stream using[/color][/color]
            > Triple DES[color=green][color=darkred]
            > > > > encryption
            > > > > CryptoStream cryptostream = new
            > > > >[/color][/color]
            > CryptoStream(ms ,des.CreateDecr yptor(prndKey,I V),CryptoStream Mode.Read)
            > ;[color=green][color=darkred]
            > > > > System.IO.Strea mReader SR = new
            > > > > System.IO.Strea mReader(cryptos tream,System.Te xt.Encoding.ASC II);
            > > > > return SR.ReadToEnd();
            > > > > }
            > > > >
            > > > > I have tried this about half a hundred ways with the same[/color][/color]
            > results :[color=green][color=darkred]
            > > > > It chokes on
            > > > > SR.ReadToEnd(); when atttepting to Decrypt the data that was[/color][/color]
            > encrypted[color=green]
            > > by[color=darkred]
            > > > > EncryptText(..)
            > > > >
            > > > > Length of the data to decrypt is invalid.
            > > > > Description: An unhandled exception occurred during the[/color][/color]
            > execution of the[color=green][color=darkred]
            > > > > current web request. Please review the stack trace for more[/color][/color]
            > information[color=green][color=darkred]
            > > > > about the error and where it originated in the code.
            > > > >
            > > > > Exception Details:[/color][/color]
            > System.Security .Cryptography.C ryptographicExc eption:[color=green][color=darkred]
            > > > > Length of the data to decrypt is invalid.
            > > > >
            > > > >
            > > > > Can someone explain what is going on and what I am doing wrong.[/color][/color]
            > In[color=green][color=darkred]
            > > > looking
            > > > > for insight into this I have seen allot of newsgroup posts where[/color][/color]
            > people[color=green][color=darkred]
            > > > had
            > > > > the same problem but no answers.
            > > > >
            > > > > Thanks,
            > > > > CMD
            > > > >
            > > > >
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Michael Giagnocavo [MVP]

              #7
              Re: TripleDES encryption problem

              Store as a varchar or char, and use Base64 (Convert.ToBase 64String) to
              get your byte[] to an easily managed string.
              -mike
              MVP

              "c duden" <cduden@hotmail .com> wrote in message
              news:eiP1yjKUDH A.2128@TK2MSFTN GP12.phx.gbl...[color=blue]
              > Ok, Michael that makes perfect sense when you put it that way.[/color]
              Follow up[color=blue]
              > question then -- with the Unicode working verison I insert that[/color]
              encrypted[color=blue]
              > data into a field in SQL2k -- (nvarchar -- supposed to be able to[/color]
              handle[color=blue]
              > Unicode values) but when I pull it out it can't unencode it -- fails[/color]
              with[color=blue]
              > "bad data" error. Is SQL doing something in the translation?
              >
              >
              > "Michael Giagnocavo [MVP]" <mggUNSPAM@Atre vido.net> wrote in message
              > news:%23sBwxo5T DHA.3132@tk2msf tngp13.phx.gbl. ..[color=green]
              > > ASCII is only a 7-bit encoding, and your key uses the full 8-bit[/color][/color]
              space[color=blue][color=green]
              > > of each byte. Thus, writing your key to ASCII results in some[/color][/color]
              data[color=blue][color=green]
              > > being messed up.
              > >
              > > -mike
              > > MVP
              > >
              > > "c duden" <cduden@hotmail .com> wrote in message
              > > news:u2oVr2lTDH A.2460@TK2MSFTN GP10.phx.gbl...[color=darkred]
              > > > Ok, I figured out a workaround -- changed everything to use[/color]
              > > UnicodeEncoding[color=darkred]
              > > > instead of ASCIIEncoding and it now works. BUT does anyone have[/color][/color][/color]
              any[color=blue][color=green]
              > > insight[color=darkred]
              > > > into why this will fail when you use ASCII encoding? I know[/color][/color][/color]
              that in[color=blue][color=green]
              > > C# all[color=darkred]
              > > > strings are Unicode Byte Arrays but why would the conversion not[/color]
              > > work[color=darkred]
              > > > correctly using the Crypto providers?
              > > >
              > > > Thanks
              > > >
              > > >
              > > > "c duden" <cduden@hotmail .com> wrote in message
              > > > news:uJQgkqlTDH A.3088@TK2MSFTN GP10.phx.gbl...
              > > > > Found something interesting that narrows the scope of the[/color][/color][/color]
              problem[color=blue][color=green][color=darkred]
              > > > somewhat.
              > > > >
              > > > > Here are a set of methods (one set of many I have tried):
              > > > >
              > > > > public static byte[] DesEncrypt(byte[] data, string[/color][/color][/color]
              hashString)[color=blue][color=green][color=darkred]
              > > > > {
              > > > > //byte[] data = ASCIIEncoding.A SCII.GetBytes(s tringToEncrypt) ;
              > > > > byte[] m_bDESKey = ASCIIEncoding.A SCII.GetBytes(h ashString);
              > > > > byte[] m_bDESIV = ASCIIEncoding.A SCII.GetBytes(h ashString);
              > > > > MemoryStream ms = new MemoryStream(40 96);
              > > > > DES des = new DESCryptoServic eProvider() ;
              > > > > CryptoStream encStream = new CryptoStream(ms ,
              > > > > des.CreateEncry ptor(m_bDESKey, m_bDESIV),
              > > > > CryptoStreamMod e.Write);
              > > > > encStream.Write (data,0,data.Le ngth);
              > > > > encStream.Flush FinalBlock();
              > > > > //calculate the length of the encrypted data
              > > > > byte[] bResult = new byte[ms.Position];
              > > > > ms.Position = 0;
              > > > > ms.Read(bResult , 0, bResult.Length) ;
              > > > > encStream.Close ();
              > > > > return bResult;
              > > > > }
              > > > > public static byte[] DesDecrypt ( byte[] data, string[/color][/color][/color]
              hashString )[color=blue][color=green][color=darkred]
              > > > > {
              > > > > //byte[] data = ASCIIEncoding.A SCII.GetBytes(s tringToDeCrypt) ;
              > > > > byte[] m_bDESKey = ASCIIEncoding.A SCII.GetBytes(h ashString);
              > > > > byte[] m_bDESIV = ASCIIEncoding.A SCII.GetBytes(h ashString);
              > > > > MemoryStream ms = new MemoryStream(da ta.Length);
              > > > > DES des = new DESCryptoServic eProvider() ;
              > > > > CryptoStream encStream = new CryptoStream(ms ,
              > > > > des.CreateDecry ptor(m_bDESKey, m_bDESIV),
              > > > > CryptoStreamMod e.Read);
              > > > > ms.Write(data,0 ,data.Length);
              > > > > ms.Position = 0;
              > > > > string strResult = new StreamReader(en cStream).ReadTo End();
              > > > > encStream.Close ();
              > > > > return ASCIIEncoding.A SCII.GetBytes(s trResult);
              > > > > }
              > > > >
              > > > > Doing the following works:
              > > > > byte[] data = System.Text.ASC IIEncoding.ASCI I.GetBytes("Hel lo[/color][/color][/color]
              my[color=blue][color=green]
              > > friend");[color=darkred]
              > > > > byte[] encrypted =
              > > > >[/color]
              > >[/color][/color]
              NFS.Architectur e.Security.Text Encryption.DesE ncrypt(data,"pa ssword");[color=blue][color=green][color=darkred]
              > > > > byte[] decrypted =
              > > > >[/color]
              > >[/color][/color]
              NFS.Architectur e.Security.Text Encryption.DesD ecrypt(encrypte d,"passwor[color=blue][color=green]
              > > d");[color=darkred]
              > > > >
              > > > > Response.Write( encrypted);
              > > > > Response.Write( "<BR>");
              > > > > Response.Write( decrypted);
              > > > >
              > > > > But the following Does not work:
              > > > >
              > > > > byte[] data = System.Text.ASC IIEncoding.ASCI I.GetBytes("Hel lo[/color][/color][/color]
              my[color=blue][color=green]
              > > friend");[color=darkred]
              > > > > byte[] encrypted =
              > > > >[/color]
              > >[/color][/color]
              NFS.Architectur e.Security.Text Encryption.DesE ncrypt(data,"pa ssword");[color=blue][color=green][color=darkred]
              > > > > string encryptedStr =
              > > > System.Text.ASC IIEncoding.ASCI I.GetString(enc rypted);
              > > > > byte[] reencrypted =
              > > > System.Text.ASC IIEncoding.ASCI I.GetBytes(encr yptedStr);
              > > > > byte[] decrypted =
              > > > >
              > > >[/color]
              > >[/color][/color]
              NFS.Architectur e.Security.Text Encryption.DesD ecrypt(reencryp ted,"passw[color=blue][color=green]
              > > ord");[color=darkred]
              > > > > System.Text.ASC IIEncoding enc = new[/color][/color][/color]
              System.Text.ASC IIEncoding();[color=blue][color=green][color=darkred]
              > > > > Response.Write( enc.GetString(e ncrypted));
              > > > > Response.Write( "<BR>");
              > > > > Response.Write( enc.GetString(d ecrypted));
              > > > >
              > > > > Somehow converting the output of DesEncrypt to a string and[/color][/color][/color]
              then[color=blue][color=green][color=darkred]
              > > > converting
              > > > > that string back to a byte array caused the decryption method[/color][/color][/color]
              to[color=blue][color=green]
              > > bomb with[color=darkred]
              > > > a
              > > > > "Bad Data" error.
              > > > >
              > > > > Anyone have any ideas?
              > > > >
              > > > >
              > > > > "c duden" <cduden@hotmail .com> wrote in message
              > > > > news:uQEHlrkTDH A.2188@TK2MSFTN GP10.phx.gbl...
              > > > > > I am attempting to encrypt some text and be able to decrypt[/color][/color][/color]
              it[color=blue][color=green]
              > > at a[color=darkred]
              > > > later
              > > > > > time. I have two methods to do this:
              > > > > >
              > > > > > public static Byte[] EncryptText(str ing textToEncrypt,[/color][/color][/color]
              string[color=blue][color=green][color=darkred]
              > > > > > encryptionHash)
              > > > > > {
              > > > > > Byte[] bytearrayinput =
              > > > > >[/color]
              > > StringAndByteMa nipulation.Conv ertStringToByte Array(textToEnc rypt);[color=darkred]
              > > > > > //DES instance
              > > > > >[/color][/color][/color]
              System.Security .Cryptography.T ripleDESCryptoS erviceProvider[color=blue][color=green]
              > > des =[color=darkred]
              > > > new
              > > > > > TripleDESCrypto ServiceProvider ();
              > > > > > // use the default SHA-1 hash algorithm
              > > > > > string pws = encryptionHash;
              > > > > > System.Security .Cryptography.P asswordDeriveBy tes db =[/color][/color][/color]
              new[color=blue][color=green][color=darkred]
              > > > > > System.Security .Cryptography.P asswordDeriveBy tes(pws,new[/color]
              > > byte[0]);[color=darkred]
              > > > > > byte[] prndKey= db.GetBytes(16) ;
              > > > > > byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,[/color][/color][/color]
              0x08,[color=blue][color=green]
              > > 0x09,[color=darkred]
              > > > > 0x10,
              > > > > > 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; // from the example in[/color][/color][/color]
              MS[color=blue][color=green][color=darkred]
              > > > > > documentation.
              > > > > > System.IO.Memor yStream ms = new[/color][/color][/color]
              System.IO.Memor yStream();[color=blue][color=green][color=darkred]
              > > > > > //Create Crypto Stream that transforms text stream using[/color]
              > > Triple DES[color=darkred]
              > > > > > encryption
              > > > > > CryptoStream cryptostream = new
              > > > > >[/color]
              > >[/color][/color]
              CryptoStream(ms ,des.CreateEncr yptor(prndKey,I V),CryptoStream Mode.Write[color=blue][color=green]
              > > );[color=darkred]
              > > > > >[/color][/color][/color]
              cryptostream.Wr ite(bytearrayin put,0,bytearray input.Length);[color=blue][color=green][color=darkred]
              > > > > >
              > > > > > System.IO.Strea mWriter sw = new
              > > > System.IO.Strea mWriter(cryptos tream);
              > > > > > sw.Write(bytear rayinput);
              > > > > > Byte[] mBytes = new Byte[ms.Length-1];
              > > > > > ms.Position = 0;
              > > > > > ms.Read(mBytes, 0,mBytes.Length );
              > > > > > cryptostream.Cl ose();
              > > > > > ms.Close();
              > > > > > return mBytes;
              > > > > > }
              > > > > >
              > > > > > public static string DeCryptText(Byt e[] textToDecrypt,[/color][/color][/color]
              string[color=blue][color=green][color=darkred]
              > > > > > encryptionHash)
              > > > > > {
              > > > > > Byte[] bytearrayinput = textToDecrypt;
              > > > > > //DES instance
              > > > > >[/color][/color][/color]
              System.Security .Cryptography.T ripleDESCryptoS erviceProvider[color=blue][color=green]
              > > des =[color=darkred]
              > > > new
              > > > > > TripleDESCrypto ServiceProvider ();
              > > > > > string pws = encryptionHash;
              > > > > > System.Security .Cryptography.P asswordDeriveBy tes db =[/color][/color][/color]
              new[color=blue][color=green][color=darkred]
              > > > > > System.Security .Cryptography.P asswordDeriveBy tes(pws,new[/color]
              > > byte[0]);[color=darkred]
              > > > > > byte[] prndKey= db.GetBytes(16) ;
              > > > > > byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,[/color][/color][/color]
              0x08,[color=blue][color=green]
              > > 0x09,[color=darkred]
              > > > > 0x10,
              > > > > > 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
              > > > > > System.IO.Memor yStream ms = new
              > > > > System.IO.Memor yStream(bytearr ayinput);
              > > > > > //Create Crypto Stream that transforms text stream using[/color]
              > > Triple DES[color=darkred]
              > > > > > encryption
              > > > > > CryptoStream cryptostream = new
              > > > > >[/color]
              > >[/color][/color]
              CryptoStream(ms ,des.CreateDecr yptor(prndKey,I V),CryptoStream Mode.Read)[color=blue][color=green]
              > > ;[color=darkred]
              > > > > > System.IO.Strea mReader SR = new
              > > > > >[/color][/color][/color]
              System.IO.Strea mReader(cryptos tream,System.Te xt.Encoding.ASC II);[color=blue][color=green][color=darkred]
              > > > > > return SR.ReadToEnd();
              > > > > > }
              > > > > >
              > > > > > I have tried this about half a hundred ways with the same[/color]
              > > results :[color=darkred]
              > > > > > It chokes on
              > > > > > SR.ReadToEnd(); when atttepting to Decrypt the data that was[/color]
              > > encrypted[color=darkred]
              > > > by
              > > > > > EncryptText(..)
              > > > > >
              > > > > > Length of the data to decrypt is invalid.
              > > > > > Description: An unhandled exception occurred during the[/color]
              > > execution of the[color=darkred]
              > > > > > current web request. Please review the stack trace for more[/color]
              > > information[color=darkred]
              > > > > > about the error and where it originated in the code.
              > > > > >
              > > > > > Exception Details:[/color]
              > > System.Security .Cryptography.C ryptographicExc eption:[color=darkred]
              > > > > > Length of the data to decrypt is invalid.
              > > > > >
              > > > > >
              > > > > > Can someone explain what is going on and what I am doing[/color][/color][/color]
              wrong.[color=blue][color=green]
              > > In[color=darkred]
              > > > > looking
              > > > > > for insight into this I have seen allot of newsgroup posts[/color][/color][/color]
              where[color=blue][color=green]
              > > people[color=darkred]
              > > > > had
              > > > > > the same problem but no answers.
              > > > > >
              > > > > > Thanks,
              > > > > > CMD
              > > > > >
              > > > > >
              > > > >
              > > > >
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • c duden

                #8
                Re: TripleDES encryption problem

                Converting to and from Base64String was the ticket. Thanks much, appreciate
                the help.



                "Michael Giagnocavo [MVP]" <mggUNSPAM@Atre vido.net> wrote in message
                news:ekqc$8MUDH A.2088@TK2MSFTN GP10.phx.gbl...[color=blue]
                > Store as a varchar or char, and use Base64 (Convert.ToBase 64String) to
                > get your byte[] to an easily managed string.
                > -mike
                > MVP
                >
                > "c duden" <cduden@hotmail .com> wrote in message
                > news:eiP1yjKUDH A.2128@TK2MSFTN GP12.phx.gbl...[color=green]
                > > Ok, Michael that makes perfect sense when you put it that way.[/color]
                > Follow up[color=green]
                > > question then -- with the Unicode working verison I insert that[/color]
                > encrypted[color=green]
                > > data into a field in SQL2k -- (nvarchar -- supposed to be able to[/color]
                > handle[color=green]
                > > Unicode values) but when I pull it out it can't unencode it -- fails[/color]
                > with[color=green]
                > > "bad data" error. Is SQL doing something in the translation?
                > >
                > >
                > > "Michael Giagnocavo [MVP]" <mggUNSPAM@Atre vido.net> wrote in message
                > > news:%23sBwxo5T DHA.3132@tk2msf tngp13.phx.gbl. ..[color=darkred]
                > > > ASCII is only a 7-bit encoding, and your key uses the full 8-bit[/color][/color]
                > space[color=green][color=darkred]
                > > > of each byte. Thus, writing your key to ASCII results in some[/color][/color]
                > data[color=green][color=darkred]
                > > > being messed up.
                > > >
                > > > -mike
                > > > MVP
                > > >
                > > > "c duden" <cduden@hotmail .com> wrote in message
                > > > news:u2oVr2lTDH A.2460@TK2MSFTN GP10.phx.gbl...
                > > > > Ok, I figured out a workaround -- changed everything to use
                > > > UnicodeEncoding
                > > > > instead of ASCIIEncoding and it now works. BUT does anyone have[/color][/color]
                > any[color=green][color=darkred]
                > > > insight
                > > > > into why this will fail when you use ASCII encoding? I know[/color][/color]
                > that in[color=green][color=darkred]
                > > > C# all
                > > > > strings are Unicode Byte Arrays but why would the conversion not
                > > > work
                > > > > correctly using the Crypto providers?
                > > > >
                > > > > Thanks
                > > > >
                > > > >
                > > > > "c duden" <cduden@hotmail .com> wrote in message
                > > > > news:uJQgkqlTDH A.3088@TK2MSFTN GP10.phx.gbl...
                > > > > > Found something interesting that narrows the scope of the[/color][/color]
                > problem[color=green][color=darkred]
                > > > > somewhat.
                > > > > >
                > > > > > Here are a set of methods (one set of many I have tried):
                > > > > >
                > > > > > public static byte[] DesEncrypt(byte[] data, string[/color][/color]
                > hashString)[color=green][color=darkred]
                > > > > > {
                > > > > > //byte[] data = ASCIIEncoding.A SCII.GetBytes(s tringToEncrypt) ;
                > > > > > byte[] m_bDESKey = ASCIIEncoding.A SCII.GetBytes(h ashString);
                > > > > > byte[] m_bDESIV = ASCIIEncoding.A SCII.GetBytes(h ashString);
                > > > > > MemoryStream ms = new MemoryStream(40 96);
                > > > > > DES des = new DESCryptoServic eProvider() ;
                > > > > > CryptoStream encStream = new CryptoStream(ms ,
                > > > > > des.CreateEncry ptor(m_bDESKey, m_bDESIV),
                > > > > > CryptoStreamMod e.Write);
                > > > > > encStream.Write (data,0,data.Le ngth);
                > > > > > encStream.Flush FinalBlock();
                > > > > > //calculate the length of the encrypted data
                > > > > > byte[] bResult = new byte[ms.Position];
                > > > > > ms.Position = 0;
                > > > > > ms.Read(bResult , 0, bResult.Length) ;
                > > > > > encStream.Close ();
                > > > > > return bResult;
                > > > > > }
                > > > > > public static byte[] DesDecrypt ( byte[] data, string[/color][/color]
                > hashString )[color=green][color=darkred]
                > > > > > {
                > > > > > //byte[] data = ASCIIEncoding.A SCII.GetBytes(s tringToDeCrypt) ;
                > > > > > byte[] m_bDESKey = ASCIIEncoding.A SCII.GetBytes(h ashString);
                > > > > > byte[] m_bDESIV = ASCIIEncoding.A SCII.GetBytes(h ashString);
                > > > > > MemoryStream ms = new MemoryStream(da ta.Length);
                > > > > > DES des = new DESCryptoServic eProvider() ;
                > > > > > CryptoStream encStream = new CryptoStream(ms ,
                > > > > > des.CreateDecry ptor(m_bDESKey, m_bDESIV),
                > > > > > CryptoStreamMod e.Read);
                > > > > > ms.Write(data,0 ,data.Length);
                > > > > > ms.Position = 0;
                > > > > > string strResult = new StreamReader(en cStream).ReadTo End();
                > > > > > encStream.Close ();
                > > > > > return ASCIIEncoding.A SCII.GetBytes(s trResult);
                > > > > > }
                > > > > >
                > > > > > Doing the following works:
                > > > > > byte[] data = System.Text.ASC IIEncoding.ASCI I.GetBytes("Hel lo[/color][/color]
                > my[color=green][color=darkred]
                > > > friend");
                > > > > > byte[] encrypted =
                > > > > >
                > > >[/color][/color]
                > NFS.Architectur e.Security.Text Encryption.DesE ncrypt(data,"pa ssword");[color=green][color=darkred]
                > > > > > byte[] decrypted =
                > > > > >
                > > >[/color][/color]
                > NFS.Architectur e.Security.Text Encryption.DesD ecrypt(encrypte d,"passwor[color=green][color=darkred]
                > > > d");
                > > > > >
                > > > > > Response.Write( encrypted);
                > > > > > Response.Write( "<BR>");
                > > > > > Response.Write( decrypted);
                > > > > >
                > > > > > But the following Does not work:
                > > > > >
                > > > > > byte[] data = System.Text.ASC IIEncoding.ASCI I.GetBytes("Hel lo[/color][/color]
                > my[color=green][color=darkred]
                > > > friend");
                > > > > > byte[] encrypted =
                > > > > >
                > > >[/color][/color]
                > NFS.Architectur e.Security.Text Encryption.DesE ncrypt(data,"pa ssword");[color=green][color=darkred]
                > > > > > string encryptedStr =
                > > > > System.Text.ASC IIEncoding.ASCI I.GetString(enc rypted);
                > > > > > byte[] reencrypted =
                > > > > System.Text.ASC IIEncoding.ASCI I.GetBytes(encr yptedStr);
                > > > > > byte[] decrypted =
                > > > > >
                > > > >
                > > >[/color][/color]
                > NFS.Architectur e.Security.Text Encryption.DesD ecrypt(reencryp ted,"passw[color=green][color=darkred]
                > > > ord");
                > > > > > System.Text.ASC IIEncoding enc = new[/color][/color]
                > System.Text.ASC IIEncoding();[color=green][color=darkred]
                > > > > > Response.Write( enc.GetString(e ncrypted));
                > > > > > Response.Write( "<BR>");
                > > > > > Response.Write( enc.GetString(d ecrypted));
                > > > > >
                > > > > > Somehow converting the output of DesEncrypt to a string and[/color][/color]
                > then[color=green][color=darkred]
                > > > > converting
                > > > > > that string back to a byte array caused the decryption method[/color][/color]
                > to[color=green][color=darkred]
                > > > bomb with
                > > > > a
                > > > > > "Bad Data" error.
                > > > > >
                > > > > > Anyone have any ideas?
                > > > > >
                > > > > >
                > > > > > "c duden" <cduden@hotmail .com> wrote in message
                > > > > > news:uQEHlrkTDH A.2188@TK2MSFTN GP10.phx.gbl...
                > > > > > > I am attempting to encrypt some text and be able to decrypt[/color][/color]
                > it[color=green][color=darkred]
                > > > at a
                > > > > later
                > > > > > > time. I have two methods to do this:
                > > > > > >
                > > > > > > public static Byte[] EncryptText(str ing textToEncrypt,[/color][/color]
                > string[color=green][color=darkred]
                > > > > > > encryptionHash)
                > > > > > > {
                > > > > > > Byte[] bytearrayinput =
                > > > > > >
                > > > StringAndByteMa nipulation.Conv ertStringToByte Array(textToEnc rypt);
                > > > > > > //DES instance
                > > > > > >[/color][/color]
                > System.Security .Cryptography.T ripleDESCryptoS erviceProvider[color=green][color=darkred]
                > > > des =
                > > > > new
                > > > > > > TripleDESCrypto ServiceProvider ();
                > > > > > > // use the default SHA-1 hash algorithm
                > > > > > > string pws = encryptionHash;
                > > > > > > System.Security .Cryptography.P asswordDeriveBy tes db =[/color][/color]
                > new[color=green][color=darkred]
                > > > > > > System.Security .Cryptography.P asswordDeriveBy tes(pws,new
                > > > byte[0]);
                > > > > > > byte[] prndKey= db.GetBytes(16) ;
                > > > > > > byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,[/color][/color]
                > 0x08,[color=green][color=darkred]
                > > > 0x09,
                > > > > > 0x10,
                > > > > > > 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; // from the example in[/color][/color]
                > MS[color=green][color=darkred]
                > > > > > > documentation.
                > > > > > > System.IO.Memor yStream ms = new[/color][/color]
                > System.IO.Memor yStream();[color=green][color=darkred]
                > > > > > > //Create Crypto Stream that transforms text stream using
                > > > Triple DES
                > > > > > > encryption
                > > > > > > CryptoStream cryptostream = new
                > > > > > >
                > > >[/color][/color]
                > CryptoStream(ms ,des.CreateEncr yptor(prndKey,I V),CryptoStream Mode.Write[color=green][color=darkred]
                > > > );
                > > > > > >[/color][/color]
                > cryptostream.Wr ite(bytearrayin put,0,bytearray input.Length);[color=green][color=darkred]
                > > > > > >
                > > > > > > System.IO.Strea mWriter sw = new
                > > > > System.IO.Strea mWriter(cryptos tream);
                > > > > > > sw.Write(bytear rayinput);
                > > > > > > Byte[] mBytes = new Byte[ms.Length-1];
                > > > > > > ms.Position = 0;
                > > > > > > ms.Read(mBytes, 0,mBytes.Length );
                > > > > > > cryptostream.Cl ose();
                > > > > > > ms.Close();
                > > > > > > return mBytes;
                > > > > > > }
                > > > > > >
                > > > > > > public static string DeCryptText(Byt e[] textToDecrypt,[/color][/color]
                > string[color=green][color=darkred]
                > > > > > > encryptionHash)
                > > > > > > {
                > > > > > > Byte[] bytearrayinput = textToDecrypt;
                > > > > > > //DES instance
                > > > > > >[/color][/color]
                > System.Security .Cryptography.T ripleDESCryptoS erviceProvider[color=green][color=darkred]
                > > > des =
                > > > > new
                > > > > > > TripleDESCrypto ServiceProvider ();
                > > > > > > string pws = encryptionHash;
                > > > > > > System.Security .Cryptography.P asswordDeriveBy tes db =[/color][/color]
                > new[color=green][color=darkred]
                > > > > > > System.Security .Cryptography.P asswordDeriveBy tes(pws,new
                > > > byte[0]);
                > > > > > > byte[] prndKey= db.GetBytes(16) ;
                > > > > > > byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,[/color][/color]
                > 0x08,[color=green][color=darkred]
                > > > 0x09,
                > > > > > 0x10,
                > > > > > > 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
                > > > > > > System.IO.Memor yStream ms = new
                > > > > > System.IO.Memor yStream(bytearr ayinput);
                > > > > > > //Create Crypto Stream that transforms text stream using
                > > > Triple DES
                > > > > > > encryption
                > > > > > > CryptoStream cryptostream = new
                > > > > > >
                > > >[/color][/color]
                > CryptoStream(ms ,des.CreateDecr yptor(prndKey,I V),CryptoStream Mode.Read)[color=green][color=darkred]
                > > > ;
                > > > > > > System.IO.Strea mReader SR = new
                > > > > > >[/color][/color]
                > System.IO.Strea mReader(cryptos tream,System.Te xt.Encoding.ASC II);[color=green][color=darkred]
                > > > > > > return SR.ReadToEnd();
                > > > > > > }
                > > > > > >
                > > > > > > I have tried this about half a hundred ways with the same
                > > > results :
                > > > > > > It chokes on
                > > > > > > SR.ReadToEnd(); when atttepting to Decrypt the data that was
                > > > encrypted
                > > > > by
                > > > > > > EncryptText(..)
                > > > > > >
                > > > > > > Length of the data to decrypt is invalid.
                > > > > > > Description: An unhandled exception occurred during the
                > > > execution of the
                > > > > > > current web request. Please review the stack trace for more
                > > > information
                > > > > > > about the error and where it originated in the code.
                > > > > > >
                > > > > > > Exception Details:
                > > > System.Security .Cryptography.C ryptographicExc eption:
                > > > > > > Length of the data to decrypt is invalid.
                > > > > > >
                > > > > > >
                > > > > > > Can someone explain what is going on and what I am doing[/color][/color]
                > wrong.[color=green][color=darkred]
                > > > In
                > > > > > looking
                > > > > > > for insight into this I have seen allot of newsgroup posts[/color][/color]
                > where[color=green][color=darkred]
                > > > people
                > > > > > had
                > > > > > > the same problem but no answers.
                > > > > > >
                > > > > > > Thanks,
                > > > > > > CMD
                > > > > > >
                > > > > > >
                > > > > >
                > > > > >
                > > > >
                > > > >
                > > >
                > > >[/color]
                > >
                > >[/color]
                >
                >[/color]


                Comment

                Working...