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