J
JC
Hi,
I have created 2 methods, one the encrypts a string using tripledes
and one that decrypts that string. THis works fine if i pass the
string directly to the decypt function. However, when I then save the
encrypted result in a varbinary field (128 length) in SQL, then pull
it back into the decrypt function the decypting is very very sketchy,
some values are mangled, others are unable to be unencrypted. The
error that occurs is 'bad data'.
The functions are included below, any help woudl be greatly
appreciated cos this starting to drive me balmy!
public Byte[] myEncrypt(string sInput)
{
UTF8Encoding utf8encoder = new UTF8Encoding();
Byte[] inputBytes = utf8encoder.GetBytes(sInput);
TripleDESCryptoServiceProvider tdesProvider = new
TripleDESCryptoServiceProvider();
ICryptoTransform cryptoTransform =
tdesProvider.CreateEncryptor(tripleDes.Key,tripleDes.IV);
MemoryStream encryptedStream = new MemoryStream();
CryptoStream cryptStream = new
CryptoStream(encryptedStream,cryptoTransform,CryptoStreamMode.Write);
cryptStream.Write(inputBytes,0,inputBytes.Length);
cryptStream.FlushFinalBlock();
encryptedStream.Position = 0;
Byte[] bResult = new Byte[encryptedStream.Length];
encryptedStream.Read(bResult,0,encryptedStream.ToArray().Length);
cryptStream.Close();
return bResult;
}
public string myDecrypt(Byte[] inputInBytes)
{
TripleDESCryptoServiceProvider tdesProvider = new
TripleDESCryptoServiceProvider();
ICryptoTransform cryptoTranform =
tdesProvider.CreateDecryptor(tripleDes.Key,tripleDes.IV);
MemoryStream decryptedStream = new MemoryStream();
CryptoStream cryptStream = new
CryptoStream(decryptedStream,cryptoTranform,CryptoStreamMode.Write);
cryptStream.Write(inputInBytes,0,inputInBytes.Length);
cryptStream.FlushFinalBlock();
decryptedStream.Position=0;
Byte[] result = new Byte[decryptedStream.Length];
decryptedStream.Read(result,0,decryptedStream.ToArray().Length);
cryptStream.Close();
UTF8Encoding myutf = new UTF8Encoding();
return myutf.GetString(result).ToString();
}
I have created 2 methods, one the encrypts a string using tripledes
and one that decrypts that string. THis works fine if i pass the
string directly to the decypt function. However, when I then save the
encrypted result in a varbinary field (128 length) in SQL, then pull
it back into the decrypt function the decypting is very very sketchy,
some values are mangled, others are unable to be unencrypted. The
error that occurs is 'bad data'.
The functions are included below, any help woudl be greatly
appreciated cos this starting to drive me balmy!
public Byte[] myEncrypt(string sInput)
{
UTF8Encoding utf8encoder = new UTF8Encoding();
Byte[] inputBytes = utf8encoder.GetBytes(sInput);
TripleDESCryptoServiceProvider tdesProvider = new
TripleDESCryptoServiceProvider();
ICryptoTransform cryptoTransform =
tdesProvider.CreateEncryptor(tripleDes.Key,tripleDes.IV);
MemoryStream encryptedStream = new MemoryStream();
CryptoStream cryptStream = new
CryptoStream(encryptedStream,cryptoTransform,CryptoStreamMode.Write);
cryptStream.Write(inputBytes,0,inputBytes.Length);
cryptStream.FlushFinalBlock();
encryptedStream.Position = 0;
Byte[] bResult = new Byte[encryptedStream.Length];
encryptedStream.Read(bResult,0,encryptedStream.ToArray().Length);
cryptStream.Close();
return bResult;
}
public string myDecrypt(Byte[] inputInBytes)
{
TripleDESCryptoServiceProvider tdesProvider = new
TripleDESCryptoServiceProvider();
ICryptoTransform cryptoTranform =
tdesProvider.CreateDecryptor(tripleDes.Key,tripleDes.IV);
MemoryStream decryptedStream = new MemoryStream();
CryptoStream cryptStream = new
CryptoStream(decryptedStream,cryptoTranform,CryptoStreamMode.Write);
cryptStream.Write(inputInBytes,0,inputInBytes.Length);
cryptStream.FlushFinalBlock();
decryptedStream.Position=0;
Byte[] result = new Byte[decryptedStream.Length];
decryptedStream.Read(result,0,decryptedStream.ToArray().Length);
cryptStream.Close();
UTF8Encoding myutf = new UTF8Encoding();
return myutf.GetString(result).ToString();
}