473,288 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

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(string textToEncrypt, string
encryptionHash)
{
Byte[] bytearrayinput =
StringAndByteManipulation.ConvertStringToByteArray (textToEncrypt);
//DES instance
System.Security.Cryptography.TripleDESCryptoServic eProvider des = new
TripleDESCryptoServiceProvider();
// use the default SHA-1 hash algorithm
string pws = encryptionHash;
System.Security.Cryptography.PasswordDeriveBytes db = new
System.Security.Cryptography.PasswordDeriveBytes(p ws,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.MemoryStream ms = new System.IO.MemoryStream();
//Create Crypto Stream that transforms text stream using Triple DES
encryption
CryptoStream cryptostream = new
CryptoStream(ms,des.CreateEncryptor(prndKey,IV),Cr yptoStreamMode.Write);
cryptostream.Write(bytearrayinput,0,bytearrayinput .Length);

System.IO.StreamWriter sw = new System.IO.StreamWriter(cryptostream);
sw.Write(bytearrayinput);
Byte[] mBytes = new Byte[ms.Length-1];
ms.Position = 0;
ms.Read(mBytes,0,mBytes.Length);
cryptostream.Close();
ms.Close();
return mBytes;
}

public static string DeCryptText(Byte[] textToDecrypt, string
encryptionHash)
{
Byte[] bytearrayinput = textToDecrypt;
//DES instance
System.Security.Cryptography.TripleDESCryptoServic eProvider des = new
TripleDESCryptoServiceProvider();
string pws = encryptionHash;
System.Security.Cryptography.PasswordDeriveBytes db = new
System.Security.Cryptography.PasswordDeriveBytes(p ws,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.MemoryStream ms = new System.IO.MemoryStream(bytearrayinput);
//Create Crypto Stream that transforms text stream using Triple DES
encryption
CryptoStream cryptostream = new
CryptoStream(ms,des.CreateDecryptor(prndKey,IV),Cr yptoStreamMode.Read);
System.IO.StreamReader SR = new
System.IO.StreamReader(cryptostream,System.Text.En coding.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 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.CryptographicExceptio n:
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
Nov 15 '05 #1
7 5455
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.ASCII.GetBytes(stringToEncrypt);
byte[] m_bDESKey = ASCIIEncoding.ASCII.GetBytes(hashString);
byte[] m_bDESIV = ASCIIEncoding.ASCII.GetBytes(hashString);
MemoryStream ms = new MemoryStream(4096);
DES des = new DESCryptoServiceProvider() ;
CryptoStream encStream = new CryptoStream(ms,
des.CreateEncryptor(m_bDESKey, m_bDESIV),
CryptoStreamMode.Write);
encStream.Write(data,0,data.Length);
encStream.FlushFinalBlock();
//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.ASCII.GetBytes(stringToDeCrypt);
byte[] m_bDESKey = ASCIIEncoding.ASCII.GetBytes(hashString);
byte[] m_bDESIV = ASCIIEncoding.ASCII.GetBytes(hashString);
MemoryStream ms = new MemoryStream(data.Length);
DES des = new DESCryptoServiceProvider() ;
CryptoStream encStream = new CryptoStream(ms,
des.CreateDecryptor(m_bDESKey, m_bDESIV),
CryptoStreamMode.Read);
ms.Write(data,0,data.Length);
ms.Position = 0;
string strResult = new StreamReader(encStream).ReadToEnd();
encStream.Close();
return ASCIIEncoding.ASCII.GetBytes(strResult);
}

Doing the following works:
byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes("Hello my friend");
byte[] encrypted =
NFS.Architecture.Security.TextEncryption.DesEncryp t(data,"password");
byte[] decrypted =
NFS.Architecture.Security.TextEncryption.DesDecryp t(encrypted,"password");

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

But the following Does not work:

byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes("Hello my friend");
byte[] encrypted =
NFS.Architecture.Security.TextEncryption.DesEncryp t(data,"password");
string encryptedStr = System.Text.ASCIIEncoding.ASCII.GetString(encrypte d);
byte[] reencrypted = System.Text.ASCIIEncoding.ASCII.GetBytes(encrypted Str);
byte[] decrypted =
NFS.Architecture.Security.TextEncryption.DesDecryp t(reencrypted,"password");
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
Response.Write(enc.GetString(encrypted));
Response.Write("<BR>");
Response.Write(enc.GetString(decrypted));

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**************@TK2MSFTNGP10.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(string textToEncrypt, string
encryptionHash)
{
Byte[] bytearrayinput =
StringAndByteManipulation.ConvertStringToByteArray (textToEncrypt);
//DES instance
System.Security.Cryptography.TripleDESCryptoServic eProvider des = new
TripleDESCryptoServiceProvider();
// use the default SHA-1 hash algorithm
string pws = encryptionHash;
System.Security.Cryptography.PasswordDeriveBytes db = new
System.Security.Cryptography.PasswordDeriveBytes(p ws,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.MemoryStream ms = new System.IO.MemoryStream();
//Create Crypto Stream that transforms text stream using Triple DES
encryption
CryptoStream cryptostream = new
CryptoStream(ms,des.CreateEncryptor(prndKey,IV),Cr yptoStreamMode.Write);
cryptostream.Write(bytearrayinput,0,bytearrayinput .Length);

System.IO.StreamWriter sw = new System.IO.StreamWriter(cryptostream);
sw.Write(bytearrayinput);
Byte[] mBytes = new Byte[ms.Length-1];
ms.Position = 0;
ms.Read(mBytes,0,mBytes.Length);
cryptostream.Close();
ms.Close();
return mBytes;
}

public static string DeCryptText(Byte[] textToDecrypt, string
encryptionHash)
{
Byte[] bytearrayinput = textToDecrypt;
//DES instance
System.Security.Cryptography.TripleDESCryptoServic eProvider des = new
TripleDESCryptoServiceProvider();
string pws = encryptionHash;
System.Security.Cryptography.PasswordDeriveBytes db = new
System.Security.Cryptography.PasswordDeriveBytes(p ws,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.MemoryStream ms = new System.IO.MemoryStream(bytearrayinput); //Create Crypto Stream that transforms text stream using Triple DES
encryption
CryptoStream cryptostream = new
CryptoStream(ms,des.CreateDecryptor(prndKey,IV),Cr yptoStreamMode.Read);
System.IO.StreamReader SR = new
System.IO.StreamReader(cryptostream,System.Text.En coding.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 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.CryptographicExceptio n:
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

Nov 15 '05 #2
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**************@TK2MSFTNGP10.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.ASCII.GetBytes(stringToEncrypt);
byte[] m_bDESKey = ASCIIEncoding.ASCII.GetBytes(hashString);
byte[] m_bDESIV = ASCIIEncoding.ASCII.GetBytes(hashString);
MemoryStream ms = new MemoryStream(4096);
DES des = new DESCryptoServiceProvider() ;
CryptoStream encStream = new CryptoStream(ms,
des.CreateEncryptor(m_bDESKey, m_bDESIV),
CryptoStreamMode.Write);
encStream.Write(data,0,data.Length);
encStream.FlushFinalBlock();
//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.ASCII.GetBytes(stringToDeCrypt);
byte[] m_bDESKey = ASCIIEncoding.ASCII.GetBytes(hashString);
byte[] m_bDESIV = ASCIIEncoding.ASCII.GetBytes(hashString);
MemoryStream ms = new MemoryStream(data.Length);
DES des = new DESCryptoServiceProvider() ;
CryptoStream encStream = new CryptoStream(ms,
des.CreateDecryptor(m_bDESKey, m_bDESIV),
CryptoStreamMode.Read);
ms.Write(data,0,data.Length);
ms.Position = 0;
string strResult = new StreamReader(encStream).ReadToEnd();
encStream.Close();
return ASCIIEncoding.ASCII.GetBytes(strResult);
}

Doing the following works:
byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes("Hello my friend");
byte[] encrypted =
NFS.Architecture.Security.TextEncryption.DesEncryp t(data,"password");
byte[] decrypted =
NFS.Architecture.Security.TextEncryption.DesDecryp t(encrypted,"password");

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

But the following Does not work:

byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes("Hello my friend");
byte[] encrypted =
NFS.Architecture.Security.TextEncryption.DesEncryp t(data,"password");
string encryptedStr = System.Text.ASCIIEncoding.ASCII.GetString(encrypte d); byte[] reencrypted = System.Text.ASCIIEncoding.ASCII.GetBytes(encrypted Str); byte[] decrypted =
NFS.Architecture.Security.TextEncryption.DesDecryp t(reencrypted,"password"); System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
Response.Write(enc.GetString(encrypted));
Response.Write("<BR>");
Response.Write(enc.GetString(decrypted));

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**************@TK2MSFTNGP10.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(string textToEncrypt, string
encryptionHash)
{
Byte[] bytearrayinput =
StringAndByteManipulation.ConvertStringToByteArray (textToEncrypt);
//DES instance
System.Security.Cryptography.TripleDESCryptoServic eProvider des = new TripleDESCryptoServiceProvider();
// use the default SHA-1 hash algorithm
string pws = encryptionHash;
System.Security.Cryptography.PasswordDeriveBytes db = new
System.Security.Cryptography.PasswordDeriveBytes(p ws,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.MemoryStream ms = new System.IO.MemoryStream();
//Create Crypto Stream that transforms text stream using Triple DES
encryption
CryptoStream cryptostream = new
CryptoStream(ms,des.CreateEncryptor(prndKey,IV),Cr yptoStreamMode.Write);
cryptostream.Write(bytearrayinput,0,bytearrayinput .Length);

System.IO.StreamWriter sw = new System.IO.StreamWriter(cryptostream); sw.Write(bytearrayinput);
Byte[] mBytes = new Byte[ms.Length-1];
ms.Position = 0;
ms.Read(mBytes,0,mBytes.Length);
cryptostream.Close();
ms.Close();
return mBytes;
}

public static string DeCryptText(Byte[] textToDecrypt, string
encryptionHash)
{
Byte[] bytearrayinput = textToDecrypt;
//DES instance
System.Security.Cryptography.TripleDESCryptoServic eProvider des = new TripleDESCryptoServiceProvider();
string pws = encryptionHash;
System.Security.Cryptography.PasswordDeriveBytes db = new
System.Security.Cryptography.PasswordDeriveBytes(p ws,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.MemoryStream ms = new

System.IO.MemoryStream(bytearrayinput);
//Create Crypto Stream that transforms text stream using Triple DES
encryption
CryptoStream cryptostream = new
CryptoStream(ms,des.CreateDecryptor(prndKey,IV),Cr yptoStreamMode.Read);
System.IO.StreamReader SR = new
System.IO.StreamReader(cryptostream,System.Text.En coding.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 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.CryptographicExceptio n:
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


Nov 15 '05 #3
gcl
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**************@TK2MSFTNGP10.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.ASCII.GetBytes (stringToEncrypt); byte[] m_bDESKey = ASCIIEncoding.ASCII.GetBytes (hashString); byte[] m_bDESIV = ASCIIEncoding.ASCII.GetBytes (hashString); MemoryStream ms = new MemoryStream(4096);
DES des = new DESCryptoServiceProvider() ;
CryptoStream encStream = new CryptoStream(ms,
des.CreateEncryptor(m_bDESKey, m_bDESIV),
CryptoStreamMode.Write);
encStream.Write(data,0,data.Length);
encStream.FlushFinalBlock();
//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.ASCII.GetBytes (stringToDeCrypt); byte[] m_bDESKey = ASCIIEncoding.ASCII.GetBytes (hashString); byte[] m_bDESIV = ASCIIEncoding.ASCII.GetBytes (hashString); MemoryStream ms = new MemoryStream(data.Length);
DES des = new DESCryptoServiceProvider() ;
CryptoStream encStream = new CryptoStream(ms,
des.CreateDecryptor(m_bDESKey, m_bDESIV),
CryptoStreamMode.Read);
ms.Write(data,0,data.Length);
ms.Position = 0;
string strResult = new StreamReader (encStream).ReadToEnd(); encStream.Close();
return ASCIIEncoding.ASCII.GetBytes(strResult);
}

Doing the following works:
byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes ("Hello my friend"); byte[] encrypted =
NFS.Architecture.Security.TextEncryption.DesEncryp t (data,"password"); byte[] decrypted =
NFS.Architecture.Security.TextEncryption.DesDecryp t (encrypted,"password");
Response.Write(encrypted);
Response.Write("<BR>");
Response.Write(decrypted);

But the following Does not work:

byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes ("Hello my friend"); byte[] encrypted =
NFS.Architecture.Security.TextEncryption.DesEncryp t (data,"password"); string encryptedStr =System.Text.ASCIIEncoding.ASCII.GetString(encrypt ed);
byte[] reencrypted =

System.Text.ASCIIEncoding.ASCII.GetBytes(encrypte dStr);
byte[] decrypted =

NFS.Architecture.Security.TextEncryption.DesDecry pt

(reencrypted,"password");
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); Response.Write(enc.GetString(encrypted));
Response.Write("<BR>");
Response.Write(enc.GetString(decrypted));

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**************@TK2MSFTNGP10.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(string
textToEncrypt, string > encryptionHash)
> {
> Byte[] bytearrayinput =
> StringAndByteManipulation.ConvertStringToByteArray (textToEncrypt); > //DES instance
> System.Security.Cryptography.TripleDESCryptoServic eProvide
r des =
new > TripleDESCryptoServiceProvider();
> // use the default SHA-1 hash algorithm
> string pws = encryptionHash;
> System.Security.Cryptography.PasswordDeriveBytes
db = new > System.Security.Cryptography.PasswordDeriveBytes (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.MemoryStream ms = new System.IO.MemoryStream(); > //Create Crypto Stream that transforms text stream using Triple DES > encryption
> CryptoStream cryptostream = new
> CryptoStream(ms,des.CreateEncryptor (prndKey,IV),CryptoStreamMode.Write); > cryptostream.Write (bytearrayinput,0,bytearrayinput.Length); >
> System.IO.StreamWriter sw = new
System.IO.StreamWriter(cryptostream); > sw.Write(bytearrayinput);
> Byte[] mBytes = new Byte[ms.Length-1];
> ms.Position = 0;
> ms.Read(mBytes,0,mBytes.Length);
> cryptostream.Close();
> ms.Close();
> return mBytes;
> }
>
> public static string DeCryptText(Byte[] textToDecrypt, string > encryptionHash)
> {
> Byte[] bytearrayinput = textToDecrypt;
> //DES instance
> System.Security.Cryptography.TripleDESCryptoServic eProvide
r des =new
> TripleDESCryptoServiceProvider();
> string pws = encryptionHash;
> System.Security.Cryptography.PasswordDeriveBytes
db = new > System.Security.Cryptography.PasswordDeriveBytes (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.MemoryStream ms = new System.IO.MemoryStream(bytearrayinput);
> //Create Crypto Stream that transforms text
stream using Triple DES > encryption
> CryptoStream cryptostream = new
> CryptoStream(ms,des.CreateDecryptor (prndKey,IV),CryptoStreamMode.Read); > System.IO.StreamReader SR = new
> System.IO.StreamReader (cryptostream,System.Text.Encoding.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.CryptographicExceptio n: > 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
>
>


.

Nov 15 '05 #4
Not sure how it was confusing but this is what I am now using and it works:

public static string DesEncrypt(string stringToEncrypt, string hashString)
{
byte[] data = ConvertStringToByteArray(stringToEncrypt);
string pws = hashString;
System.Security.Cryptography.PasswordDeriveBytes db = new
System.Security.Cryptography.PasswordDeriveBytes(p ws,new byte[0]);
byte[] m_bDESKey= db.GetBytes(16);
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Mode = CipherMode.CBC;
byte[] m_bDESIV = UnicodeEncoding.Unicode.GetBytes(hashString);
MemoryStream ms = new MemoryStream(4096);
CryptoStream encStream = new CryptoStream(ms,
des.CreateEncryptor(m_bDESKey, m_bDESIV),
CryptoStreamMode.Write);
encStream.Write(data,0,data.Length);
encStream.FlushFinalBlock();
//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.GetString(bResult);
}
public static string DesDecrypt ( string stringToDeCrypt, string
hashString )
{
byte[] data = ConvertStringToByteArray(stringToDeCrypt);
string pws = hashString;
System.Security.Cryptography.PasswordDeriveBytes db = new
System.Security.Cryptography.PasswordDeriveBytes(p ws,new byte[0]);
byte[] m_bDESKey= db.GetBytes(16);
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Mode = CipherMode.CBC;
byte[] m_bDESIV = UnicodeEncoding.Unicode.GetBytes(hashString);
MemoryStream ms = new MemoryStream(data.Length);
CryptoStream encStream = new CryptoStream(ms,
des.CreateDecryptor(m_bDESKey, m_bDESIV),
CryptoStreamMode.Read);
ms.Write(data,0,data.Length);
ms.Position = 0;
string strResult = new StreamReader(encStream).ReadToEnd();
encStream.Close();
return strResult;
}

private Byte[] ConvertStringToByteArray(String s)
{
Byte[] returnValue = UnicodeEncoding.Unicode.GetBytes(s);
return returnValue;
}

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

string encrypted =
NFS.Architecture.Security.TextEncryption.DesEncryp t("Hello
World","password");
string decrypted =
NFS.Architecture.Security.TextEncryption.DesDecryp t(encrypted,"password");
Console.WriteLine(encrypted);
Console.WriteLine(decrypted);
Console.ReadLine();

Now if you replace UnicodeEncoding.Unicode with ASCIIEncoding.ASCII
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*******@comcast.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 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**************@TK2MSFTNGP10.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.ASCII.GetBytes (stringToEncrypt); byte[] m_bDESKey = ASCIIEncoding.ASCII.GetBytes (hashString); byte[] m_bDESIV = ASCIIEncoding.ASCII.GetBytes (hashString); MemoryStream ms = new MemoryStream(4096);
DES des = new DESCryptoServiceProvider() ;
CryptoStream encStream = new CryptoStream(ms,
des.CreateEncryptor(m_bDESKey, m_bDESIV),
CryptoStreamMode.Write);
encStream.Write(data,0,data.Length);
encStream.FlushFinalBlock();
//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.ASCII.GetBytes (stringToDeCrypt); byte[] m_bDESKey = ASCIIEncoding.ASCII.GetBytes (hashString); byte[] m_bDESIV = ASCIIEncoding.ASCII.GetBytes (hashString); MemoryStream ms = new MemoryStream(data.Length);
DES des = new DESCryptoServiceProvider() ;
CryptoStream encStream = new CryptoStream(ms,
des.CreateDecryptor(m_bDESKey, m_bDESIV),
CryptoStreamMode.Read);
ms.Write(data,0,data.Length);
ms.Position = 0;
string strResult = new StreamReader (encStream).ReadToEnd(); encStream.Close();
return ASCIIEncoding.ASCII.GetBytes(strResult);
}

Doing the following works:
byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes ("Hello my friend"); byte[] encrypted =
NFS.Architecture.Security.TextEncryption.DesEncryp t (data,"password"); byte[] decrypted =
NFS.Architecture.Security.TextEncryption.DesDecryp t (encrypted,"password");
Response.Write(encrypted);
Response.Write("<BR>");
Response.Write(decrypted);

But the following Does not work:

byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes ("Hello my friend"); byte[] encrypted =
NFS.Architecture.Security.TextEncryption.DesEncryp t (data,"password"); string encryptedStr =

System.Text.ASCIIEncoding.ASCII.GetString(encrypt ed);
byte[] reencrypted =

System.Text.ASCIIEncoding.ASCII.GetBytes(encrypte dStr);
byte[] decrypted =

NFS.Architecture.Security.TextEncryption.DesDecry pt

(reencrypted,"password");
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); Response.Write(enc.GetString(encrypted));
Response.Write("<BR>");
Response.Write(enc.GetString(decrypted));

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**************@TK2MSFTNGP10.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(string

textToEncrypt, string > encryptionHash)
> {
> Byte[] bytearrayinput =
> StringAndByteManipulation.ConvertStringToByteArray (textToEncrypt); > //DES instance
> System.Security.Cryptography.TripleDESCryptoServic eProvide
r des =
new
> TripleDESCryptoServiceProvider();
> // use the default SHA-1 hash algorithm
> string pws = encryptionHash;
> System.Security.Cryptography.PasswordDeriveBytes

db = new > System.Security.Cryptography.PasswordDeriveBytes (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.MemoryStream ms = new System.IO.MemoryStream(); > //Create Crypto Stream that transforms text stream using Triple DES > encryption
> CryptoStream cryptostream = new
> CryptoStream(ms,des.CreateEncryptor (prndKey,IV),CryptoStreamMode.Write); > cryptostream.Write (bytearrayinput,0,bytearrayinput.Length); >
> System.IO.StreamWriter sw = new

System.IO.StreamWriter(cryptostream);
> sw.Write(bytearrayinput);
> Byte[] mBytes = new Byte[ms.Length-1];
> ms.Position = 0;
> ms.Read(mBytes,0,mBytes.Length);
> cryptostream.Close();
> ms.Close();
> return mBytes;
> }
>
> public static string DeCryptText(Byte[] textToDecrypt, string > encryptionHash)
> {
> Byte[] bytearrayinput = textToDecrypt;
> //DES instance
> System.Security.Cryptography.TripleDESCryptoServic eProvide
r des =
new
> TripleDESCryptoServiceProvider();
> string pws = encryptionHash;
> System.Security.Cryptography.PasswordDeriveBytes

db = new > System.Security.Cryptography.PasswordDeriveBytes (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.MemoryStream ms = new
System.IO.MemoryStream(bytearrayinput);
> //Create Crypto Stream that transforms text stream using Triple DES > encryption
> CryptoStream cryptostream = new
> CryptoStream(ms,des.CreateDecryptor (prndKey,IV),CryptoStreamMode.Read); > System.IO.StreamReader SR = new
> System.IO.StreamReader (cryptostream,System.Text.Encoding.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 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.CryptographicExceptio n: > 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
>
>

.

Nov 15 '05 #5
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*******@Atrevido.net> wrote in message
news:%2****************@tk2msftngp13.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**************@TK2MSFTNGP10.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**************@TK2MSFTNGP10.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.ASCII.GetBytes(stringToEncrypt);
byte[] m_bDESKey = ASCIIEncoding.ASCII.GetBytes(hashString);
byte[] m_bDESIV = ASCIIEncoding.ASCII.GetBytes(hashString);
MemoryStream ms = new MemoryStream(4096);
DES des = new DESCryptoServiceProvider() ;
CryptoStream encStream = new CryptoStream(ms,
des.CreateEncryptor(m_bDESKey, m_bDESIV),
CryptoStreamMode.Write);
encStream.Write(data,0,data.Length);
encStream.FlushFinalBlock();
//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.ASCII.GetBytes(stringToDeCrypt);
byte[] m_bDESKey = ASCIIEncoding.ASCII.GetBytes(hashString);
byte[] m_bDESIV = ASCIIEncoding.ASCII.GetBytes(hashString);
MemoryStream ms = new MemoryStream(data.Length);
DES des = new DESCryptoServiceProvider() ;
CryptoStream encStream = new CryptoStream(ms,
des.CreateDecryptor(m_bDESKey, m_bDESIV),
CryptoStreamMode.Read);
ms.Write(data,0,data.Length);
ms.Position = 0;
string strResult = new StreamReader(encStream).ReadToEnd();
encStream.Close();
return ASCIIEncoding.ASCII.GetBytes(strResult);
}

Doing the following works:
byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes("Hello my friend"); byte[] encrypted =
NFS.Architecture.Security.TextEncryption.DesEncryp t(data,"password"); byte[] decrypted =
NFS.Architecture.Security.TextEncryption.DesDecryp t(encrypted,"passwor
d");
Response.Write(encrypted);
Response.Write("<BR>");
Response.Write(decrypted);

But the following Does not work:

byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes("Hello my friend"); byte[] encrypted =
NFS.Architecture.Security.TextEncryption.DesEncryp t(data,"password"); string encryptedStr =

System.Text.ASCIIEncoding.ASCII.GetString(encrypte d);
byte[] reencrypted =

System.Text.ASCIIEncoding.ASCII.GetBytes(encrypted Str);
byte[] decrypted =

NFS.Architecture.Security.TextEncryption.DesDecryp t(reencrypted,"passw
ord");
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
Response.Write(enc.GetString(encrypted));
Response.Write("<BR>");
Response.Write(enc.GetString(decrypted));

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**************@TK2MSFTNGP10.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(string textToEncrypt, string
> encryptionHash)
> {
> Byte[] bytearrayinput =
>

StringAndByteManipulation.ConvertStringToByteArray (textToEncrypt); > //DES instance
> System.Security.Cryptography.TripleDESCryptoServic eProvider des =
new
> TripleDESCryptoServiceProvider();
> // use the default SHA-1 hash algorithm
> string pws = encryptionHash;
> System.Security.Cryptography.PasswordDeriveBytes db = new
> System.Security.Cryptography.PasswordDeriveBytes(p ws,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.MemoryStream ms = new System.IO.MemoryStream();
> //Create Crypto Stream that transforms text stream using Triple DES > encryption
> CryptoStream cryptostream = new
> CryptoStream(ms,des.CreateEncryptor(prndKey,IV),Cr yptoStreamMode.Write
); > cryptostream.Write(bytearrayinput,0,bytearrayinput .Length);
>
> System.IO.StreamWriter sw = new

System.IO.StreamWriter(cryptostream);
> sw.Write(bytearrayinput);
> Byte[] mBytes = new Byte[ms.Length-1];
> ms.Position = 0;
> ms.Read(mBytes,0,mBytes.Length);
> cryptostream.Close();
> ms.Close();
> return mBytes;
> }
>
> public static string DeCryptText(Byte[] textToDecrypt, string
> encryptionHash)
> {
> Byte[] bytearrayinput = textToDecrypt;
> //DES instance
> System.Security.Cryptography.TripleDESCryptoServic eProvider des =
new
> TripleDESCryptoServiceProvider();
> string pws = encryptionHash;
> System.Security.Cryptography.PasswordDeriveBytes db = new
> System.Security.Cryptography.PasswordDeriveBytes(p ws,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.MemoryStream ms = new
System.IO.MemoryStream(bytearrayinput);
> //Create Crypto Stream that transforms text stream using Triple DES > encryption
> CryptoStream cryptostream = new
> CryptoStream(ms,des.CreateDecryptor(prndKey,IV),Cr yptoStreamMode.Read)
; > System.IO.StreamReader SR = new
> System.IO.StreamReader(cryptostream,System.Text.En coding.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 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.CryptographicExceptio n: > 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
>
>



Nov 15 '05 #6
Store as a varchar or char, and use Base64 (Convert.ToBase64String) to
get your byte[] to an easily managed string.
-mike
MVP

"c duden" <cd****@hotmail.com> wrote in message
news:ei**************@TK2MSFTNGP12.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*******@Atrevido.net> wrote in message
news:%2****************@tk2msftngp13.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**************@TK2MSFTNGP10.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**************@TK2MSFTNGP10.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.ASCII.GetBytes(stringToEncrypt);
> byte[] m_bDESKey = ASCIIEncoding.ASCII.GetBytes(hashString);
> byte[] m_bDESIV = ASCIIEncoding.ASCII.GetBytes(hashString);
> MemoryStream ms = new MemoryStream(4096);
> DES des = new DESCryptoServiceProvider() ;
> CryptoStream encStream = new CryptoStream(ms,
> des.CreateEncryptor(m_bDESKey, m_bDESIV),
> CryptoStreamMode.Write);
> encStream.Write(data,0,data.Length);
> encStream.FlushFinalBlock();
> //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.ASCII.GetBytes(stringToDeCrypt);
> byte[] m_bDESKey = ASCIIEncoding.ASCII.GetBytes(hashString);
> byte[] m_bDESIV = ASCIIEncoding.ASCII.GetBytes(hashString);
> MemoryStream ms = new MemoryStream(data.Length);
> DES des = new DESCryptoServiceProvider() ;
> CryptoStream encStream = new CryptoStream(ms,
> des.CreateDecryptor(m_bDESKey, m_bDESIV),
> CryptoStreamMode.Read);
> ms.Write(data,0,data.Length);
> ms.Position = 0;
> string strResult = new StreamReader(encStream).ReadToEnd();
> encStream.Close();
> return ASCIIEncoding.ASCII.GetBytes(strResult);
> }
>
> Doing the following works:
> byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes("Hello my friend");
> byte[] encrypted =
>

NFS.Architecture.Security.TextEncryption.DesEncryp t(data,"password"); > byte[] decrypted =
>

NFS.Architecture.Security.TextEncryption.DesDecryp t(encrypted,"passwor d");
>
> Response.Write(encrypted);
> Response.Write("<BR>");
> Response.Write(decrypted);
>
> But the following Does not work:
>
> byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes("Hello my
friend");
> byte[] encrypted =
>

NFS.Architecture.Security.TextEncryption.DesEncryp t(data,"password"); > string encryptedStr =
System.Text.ASCIIEncoding.ASCII.GetString(encrypte d);
> byte[] reencrypted =
System.Text.ASCIIEncoding.ASCII.GetBytes(encrypted Str);
> byte[] decrypted =
>

NFS.Architecture.Security.TextEncryption.DesDecryp t(reencrypted,"passw ord");
> System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); > Response.Write(enc.GetString(encrypted));
> Response.Write("<BR>");
> Response.Write(enc.GetString(decrypted));
>
> 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**************@TK2MSFTNGP10.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(string textToEncrypt,
string > > encryptionHash)
> > {
> > Byte[] bytearrayinput =
> >

StringAndByteManipulation.ConvertStringToByteArray (textToEncrypt);
> > //DES instance
> > System.Security.Cryptography.TripleDESCryptoServic eProvider des =
new
> > TripleDESCryptoServiceProvider();
> > // use the default SHA-1 hash algorithm
> > string pws = encryptionHash;
> > System.Security.Cryptography.PasswordDeriveBytes db =
new > > System.Security.Cryptography.PasswordDeriveBytes(p ws,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.MemoryStream ms = new System.IO.MemoryStream(); > > //Create Crypto Stream that transforms text stream using

Triple DES
> > encryption
> > CryptoStream cryptostream = new
> >

CryptoStream(ms,des.CreateEncryptor(prndKey,IV),Cr yptoStreamMode.Write );
> > cryptostream.Write(bytearrayinput,0,bytearrayinput .Length); > >
> > System.IO.StreamWriter sw = new
System.IO.StreamWriter(cryptostream);
> > sw.Write(bytearrayinput);
> > Byte[] mBytes = new Byte[ms.Length-1];
> > ms.Position = 0;
> > ms.Read(mBytes,0,mBytes.Length);
> > cryptostream.Close();
> > ms.Close();
> > return mBytes;
> > }
> >
> > public static string DeCryptText(Byte[] textToDecrypt, string > > encryptionHash)
> > {
> > Byte[] bytearrayinput = textToDecrypt;
> > //DES instance
> > System.Security.Cryptography.TripleDESCryptoServic eProvider
des =
new
> > TripleDESCryptoServiceProvider();
> > string pws = encryptionHash;
> > System.Security.Cryptography.PasswordDeriveBytes db =
new > > System.Security.Cryptography.PasswordDeriveBytes(p ws,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.MemoryStream ms = new
> System.IO.MemoryStream(bytearrayinput);
> > //Create Crypto Stream that transforms text stream using

Triple DES
> > encryption
> > CryptoStream cryptostream = new
> >

CryptoStream(ms,des.CreateDecryptor(prndKey,IV),Cr yptoStreamMode.Read) ;
> > System.IO.StreamReader SR = new
> >
System.IO.StreamReader(cryptostream,System.Text.En coding.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

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.CryptographicExceptio n:
> > 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
> >
> >
>
>



Nov 15 '05 #7
Converting to and from Base64String was the ticket. Thanks much, appreciate
the help.

"Michael Giagnocavo [MVP]" <mg*******@Atrevido.net> wrote in message
news:ek**************@TK2MSFTNGP10.phx.gbl...
Store as a varchar or char, and use Base64 (Convert.ToBase64String) to
get your byte[] to an easily managed string.
-mike
MVP

"c duden" <cd****@hotmail.com> wrote in message
news:ei**************@TK2MSFTNGP12.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*******@Atrevido.net> wrote in message
news:%2****************@tk2msftngp13.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**************@TK2MSFTNGP10.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**************@TK2MSFTNGP10.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.ASCII.GetBytes(stringToEncrypt);
> > byte[] m_bDESKey = ASCIIEncoding.ASCII.GetBytes(hashString);
> > byte[] m_bDESIV = ASCIIEncoding.ASCII.GetBytes(hashString);
> > MemoryStream ms = new MemoryStream(4096);
> > DES des = new DESCryptoServiceProvider() ;
> > CryptoStream encStream = new CryptoStream(ms,
> > des.CreateEncryptor(m_bDESKey, m_bDESIV),
> > CryptoStreamMode.Write);
> > encStream.Write(data,0,data.Length);
> > encStream.FlushFinalBlock();
> > //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.ASCII.GetBytes(stringToDeCrypt);
> > byte[] m_bDESKey = ASCIIEncoding.ASCII.GetBytes(hashString);
> > byte[] m_bDESIV = ASCIIEncoding.ASCII.GetBytes(hashString);
> > MemoryStream ms = new MemoryStream(data.Length);
> > DES des = new DESCryptoServiceProvider() ;
> > CryptoStream encStream = new CryptoStream(ms,
> > des.CreateDecryptor(m_bDESKey, m_bDESIV),
> > CryptoStreamMode.Read);
> > ms.Write(data,0,data.Length);
> > ms.Position = 0;
> > string strResult = new StreamReader(encStream).ReadToEnd();
> > encStream.Close();
> > return ASCIIEncoding.ASCII.GetBytes(strResult);
> > }
> >
> > Doing the following works:
> > byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes("Hello my friend");
> > byte[] encrypted =
> >
NFS.Architecture.Security.TextEncryption.DesEncryp t(data,"password"); > > byte[] decrypted =
> >
NFS.Architecture.Security.TextEncryption.DesDecryp t(encrypted,"passwor d");
> >
> > Response.Write(encrypted);
> > Response.Write("<BR>");
> > Response.Write(decrypted);
> >
> > But the following Does not work:
> >
> > byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes("Hello my friend");
> > byte[] encrypted =
> >
NFS.Architecture.Security.TextEncryption.DesEncryp t(data,"password"); > > string encryptedStr =
> System.Text.ASCIIEncoding.ASCII.GetString(encrypte d);
> > byte[] reencrypted =
> System.Text.ASCIIEncoding.ASCII.GetBytes(encrypted Str);
> > byte[] decrypted =
> >
>
NFS.Architecture.Security.TextEncryption.DesDecryp t(reencrypted,"passw ord");
> > System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); > > Response.Write(enc.GetString(encrypted));
> > Response.Write("<BR>");
> > Response.Write(enc.GetString(decrypted));
> >
> > 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**************@TK2MSFTNGP10.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(string textToEncrypt, string > > > encryptionHash)
> > > {
> > > Byte[] bytearrayinput =
> > >
StringAndByteManipulation.ConvertStringToByteArray (textToEncrypt);
> > > //DES instance
> > > System.Security.Cryptography.TripleDESCryptoServic eProvider des =
> new
> > > TripleDESCryptoServiceProvider();
> > > // use the default SHA-1 hash algorithm
> > > string pws = encryptionHash;
> > > System.Security.Cryptography.PasswordDeriveBytes db = new > > > System.Security.Cryptography.PasswordDeriveBytes(p ws,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.MemoryStream ms = new System.IO.MemoryStream(); > > > //Create Crypto Stream that transforms text stream using
Triple DES
> > > encryption
> > > CryptoStream cryptostream = new
> > >
CryptoStream(ms,des.CreateEncryptor(prndKey,IV),Cr yptoStreamMode.Write );
> > > cryptostream.Write(bytearrayinput,0,bytearrayinput .Length); > > >
> > > System.IO.StreamWriter sw = new
> System.IO.StreamWriter(cryptostream);
> > > sw.Write(bytearrayinput);
> > > Byte[] mBytes = new Byte[ms.Length-1];
> > > ms.Position = 0;
> > > ms.Read(mBytes,0,mBytes.Length);
> > > cryptostream.Close();
> > > ms.Close();
> > > return mBytes;
> > > }
> > >
> > > public static string DeCryptText(Byte[] textToDecrypt, string > > > encryptionHash)
> > > {
> > > Byte[] bytearrayinput = textToDecrypt;
> > > //DES instance
> > > System.Security.Cryptography.TripleDESCryptoServic eProvider des =
> new
> > > TripleDESCryptoServiceProvider();
> > > string pws = encryptionHash;
> > > System.Security.Cryptography.PasswordDeriveBytes db = new > > > System.Security.Cryptography.PasswordDeriveBytes(p ws,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.MemoryStream ms = new
> > System.IO.MemoryStream(bytearrayinput);
> > > //Create Crypto Stream that transforms text stream using
Triple DES
> > > encryption
> > > CryptoStream cryptostream = new
> > >
CryptoStream(ms,des.CreateDecryptor(prndKey,IV),Cr yptoStreamMode.Read) ;
> > > System.IO.StreamReader SR = new
> > > System.IO.StreamReader(cryptostream,System.Text.En coding.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
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.CryptographicExceptio n:
> > > 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
> > >
> > >
> >
> >
>
>



Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
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) {...
5
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...
3
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...
5
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...
22
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...
4
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.