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 7 5490
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" <cd****@hotmail .com> wrote in message
news:uQ******** ******@TK2MSFTN GP10.phx.gbl... 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
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" <cd****@hotmail .com> wrote in message
news:uJ******** ******@TK2MSFTN GP10.phx.gbl... 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" <cd****@hotmail .com> wrote in message news:uQ******** ******@TK2MSFTN GP10.phx.gbl... 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
give me your code and I can check..
Your early message causes confusion -----Original Message----- Ok, I figured out a workaround -- changed everything to
use UnicodeEncodinginstead of ASCIIEncoding and it now works. BUT does
anyone have any insightinto why this will fail when you use ASCII encoding? I
know that in C# allstrings are Unicode Byte Arrays but why would the
conversion not workcorrectly using the Crypto providers?
Thanks
"c duden" <cd****@hotmail .com> wrote in message news:uJ******* *******@TK2MSFT NGP10.phx.gbl.. . Found something interesting that narrows the scope of
the problemsomewhat. 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
(stringToEncryp t); byte[] m_bDESKey = ASCIIEncoding.A SCII.GetBytes
(hashString); byte[] m_bDESIV = ASCIIEncoding.A SCII.GetBytes
(hashString); 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
(stringToDeCryp t); byte[] m_bDESKey = ASCIIEncoding.A SCII.GetBytes
(hashString); byte[] m_bDESIV = ASCIIEncoding.A SCII.GetBytes
(hashString); 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
(encStream).Rea dToEnd(); encStream.Close (); return ASCIIEncoding.A SCII.GetBytes(s trResult); }
Doing the following works: byte[] data = System.Text.ASC IIEncoding.ASCI I.GetBytes
("Hello my friend"); byte[] encrypted = NFS.Architectur e.Security.Text Encryption.DesE ncrypt
(data,"password "); byte[] decrypted = NFS.Architectur e.Security.Text Encryption.DesD ecrypt
(encrypted,"pas sword"); Response.Write( encrypted); Response.Write( "<BR>"); Response.Write( decrypted);
But the following Does not work:
byte[] data = System.Text.ASC IIEncoding.ASCI I.GetBytes
("Hello my friend"); byte[] encrypted = NFS.Architectur e.Security.Text Encryption.DesE ncrypt
(data,"password "); string encryptedStr =System.Text.AS CIIEncoding.ASC II.GetString(en crypted); byte[] reencrypted = System.Text.AS CIIEncoding.ASC II.GetBytes(enc ryptedStr); byte[] decrypted = NFS.Architectu re.Security.Tex tEncryption.Des Decrypt
(reencrypted,"p assword"); 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 thenconverting that string back to a byte array caused the decryption
method to bomb witha "Bad Data" error.
Anyone have any ideas?
"c duden" <cd****@hotmail .com> wrote in message news:uQ******** ******@TK2MSFTN GP10.phx.gbl... > I am attempting to encrypt some text and be able to
decrypt it at alater > time. I have two methods to do this: > > public static Byte[] EncryptText(str ing
textToEncrypt, string > encryptionHash) > { > Byte[] bytearrayinput = > StringAndByteMa nipulation.Conv ertStringToByte Array
(textToEncrypt) ; > //DES instance >
System.Security .Cryptography.T ripleDESCryptoS erviceProvide
r 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,IV),Cr yptoStreamMode. Write); > cryptostream.Wr ite
(bytearrayinput ,0,bytearrayinp ut.Length); > > System.IO.Strea mWriter sw = new System.IO.Stre amWriter(crypto stream); > 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 erviceProvide
r 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,IV),Cr yptoStreamMode. Read); > System.IO.Strea mReader SR = new > System.IO.Strea mReader
(cryptostream,S ystem.Text.Enco ding.ASCII); > 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 encryptedby > 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 > >
.
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" <ln*******@comc ast.net> wrote in message
news:02******** *************** *****@phx.gbl.. . give me your code and I can check.. Your early message causes confusion
-----Original Message----- Ok, I figured out a workaround -- changed everything to use UnicodeEncodinginstead of ASCIIEncoding and it now works. BUT does anyone have any insightinto why this will fail when you use ASCII encoding? I know that in C# allstrings are Unicode Byte Arrays but why would the conversion not workcorrectly using the Crypto providers?
Thanks
"c duden" <cd****@hotmail .com> wrote in message news:uJ******* *******@TK2MSFT NGP10.phx.gbl.. . Found something interesting that narrows the scope of the problemsomewhat. 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 (stringToEncryp t); byte[] m_bDESKey = ASCIIEncoding.A SCII.GetBytes (hashString); byte[] m_bDESIV = ASCIIEncoding.A SCII.GetBytes (hashString); 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 (stringToDeCryp t); byte[] m_bDESKey = ASCIIEncoding.A SCII.GetBytes (hashString); byte[] m_bDESIV = ASCIIEncoding.A SCII.GetBytes (hashString); 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 (encStream).Rea dToEnd(); encStream.Close (); return ASCIIEncoding.A SCII.GetBytes(s trResult); }
Doing the following works: byte[] data = System.Text.ASC IIEncoding.ASCI I.GetBytes ("Hello my friend"); byte[] encrypted = NFS.Architectur e.Security.Text Encryption.DesE ncrypt (data,"password "); byte[] decrypted = NFS.Architectur e.Security.Text Encryption.DesD ecrypt (encrypted,"pas sword"); Response.Write( encrypted); Response.Write( "<BR>"); Response.Write( decrypted);
But the following Does not work:
byte[] data = System.Text.ASC IIEncoding.ASCI I.GetBytes ("Hello my friend"); byte[] encrypted = NFS.Architectur e.Security.Text Encryption.DesE ncrypt (data,"password "); string encryptedStr = System.Text.AS CIIEncoding.ASC II.GetString(en crypted); byte[] reencrypted = System.Text.AS CIIEncoding.ASC II.GetBytes(enc ryptedStr); byte[] decrypted = NFS.Architectu re.Security.Tex tEncryption.Des Decrypt (reencrypted,"p assword"); 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 thenconverting that string back to a byte array caused the decryption method to bomb witha "Bad Data" error.
Anyone have any ideas?
"c duden" <cd****@hotmail .com> wrote in message news:uQ******** ******@TK2MSFTN GP10.phx.gbl... > I am attempting to encrypt some text and be able to decrypt it at alater > time. I have two methods to do this: > > public static Byte[] EncryptText(str ing textToEncrypt, string > encryptionHash) > { > Byte[] bytearrayinput = > StringAndByteMa nipulation.Conv ertStringToByte Array (textToEncrypt) ; > //DES instance > System.Security .Cryptography.T ripleDESCryptoS erviceProvide r 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,IV),Cr yptoStreamMode. Write); > cryptostream.Wr ite (bytearrayinput ,0,bytearrayinp ut.Length); > > System.IO.Strea mWriter sw = new System.IO.Stre amWriter(crypto stream); > 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 erviceProvide r 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,IV),Cr yptoStreamMode. Read); > System.IO.Strea mReader SR = new > System.IO.Strea mReader (cryptostream,S ystem.Text.Enco ding.ASCII); > 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 encryptedby > 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 > >
.
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]" <mg*******@Atre vido.net> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. .. 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" <cd****@hotmail .com> wrote in message news:u2******** ******@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 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" <cd****@hotmail .com> wrote in message news:uJ******** ******@TK2MSFTN GP10.phx.gbl... 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,"passwor 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 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,"passw ord"); 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" <cd****@hotmail .com> wrote in message news:uQ******** ******@TK2MSFTN GP10.phx.gbl... > 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 > >
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" <cd****@hotmail .com> wrote in message
news:ei******** ******@TK2MSFTN GP12.phx.gbl... 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]" <mg*******@Atre vido.net> wrote in message news:%2******** ********@tk2msf tngp13.phx.gbl. .. 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" <cd****@hotmail .com> wrote in message news:u2******** ******@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
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" <cd****@hotmail .com> wrote in message news:uJ******** ******@TK2MSFTN GP10.phx.gbl... > 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,"passwor 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
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,"passw ord"); > 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" <cd****@hotmail .com> wrote in message > news:uQ******** ******@TK2MSFTN GP10.phx.gbl... > > 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 > > > > > >
Converting to and from Base64String was the ticket. Thanks much, appreciate
the help.
"Michael Giagnocavo [MVP]" <mg*******@Atre vido.net> wrote in message
news:ek******** ******@TK2MSFTN GP10.phx.gbl... 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" <cd****@hotmail .com> wrote in message news:ei******** ******@TK2MSFTN GP12.phx.gbl... 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]" <mg*******@Atre vido.net> wrote in message news:%2******** ********@tk2msf tngp13.phx.gbl. .. 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" <cd****@hotmail .com> wrote in message news:u2******** ******@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 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" <cd****@hotmail .com> wrote in message > news:uJ******** ******@TK2MSFTN GP10.phx.gbl... > > 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,"passwor 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 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,"passw ord"); > > 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" <cd****@hotmail .com> wrote in message > > news:uQ******** ******@TK2MSFTN GP10.phx.gbl... > > > 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 > > > > > > > > > > > >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: c duden |
last post by:
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(string textToEncrypt, string
encryptionHash)
{
Byte bytearrayinput =
StringAndByteManipulation.ConvertStringToByteArray(textToEncrypt);
//DES instance
System.Security.Cryptography.TripleDESCryptoServiceProvider des = new
|
by: jdn |
last post by:
I'm new to using this part of the framework, so I'm hoping I've done
something obviously stupid, which someone will be able to point out in
an obvious manner.
Most of the samples I've seen involved encrypting and decrypting to and
from a file, but that's not what I want. I want to be able to insert a
string into an encryption function which outputs that encrypted string,
which could then be sent into a decryption function and spit out...
|
by: pachinco |
last post by:
Hello, I am having a problem encrypting a tiff image..it always loses
information after i decrypt and I know it has something to do with the
encoding but i can't figure it out. Any help would be appreciated..
Here is the encrypt function:
private byte Encrypt(byte bytes)
{
key = new byte;
iv = new byte;
|
by: Bob |
last post by:
Hi
Problem. Generate valuable 'plain' data on Mach A. Need to write encrypted
version to CD which gets inputed to database on mach B.
Want the data to be encrypted in the table so it cannot be eyeballed to see
data.
'Plain' data gets inputted by application to Mach B, if valid match Mach B
takes action.
So...
Problem is that RijndaelManaged is too smart for the job seeing that gives a
unique encryption for the same plain data each time...
|
by: Wilson |
last post by:
i am learning to program using c++ and was set a task of making a
simple encryption algorithim. I choose to start with one where simply
each letter is replaced with its equivilent in the alphabet when
written backmards, i am hoping to make this more complex soon. Below
is the program i wrote but it does not work, it simply returns the
exact same text you enter. Could you please advise on how to sort
this, and also suggest any ways of...
| |
by: Grant |
last post by:
I am trying to write a program in VB.NET that exchanges both strings and
files with a ColdFusion web server which is running on Debian. I
attempted to do so with the Chilkat (http://www.chilkat.com) encryption
library in VB.NET and mcrypt (run through the command line of
ColdFusion) on the web server. No matter what I try I can not get the
encrypted strings & files to decrypt on the other system.
I need to use VB.NET and would prefer...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |