J
JC
Hi all,
I have seem few messages posted regaring this but as yet have been
able to get this code to work. The plan was to encrypt some string
then pass the result to another function that woudl decrypt it -
please see below. Anyway i keep getting a 'bad data' exception. I'm
totally at a loss, so any help woudl be greatly appreciated.
public Byte[] myEncrypt()
{
UTF8Encoding utf8encoder = new UTF8Encoding();
Byte[] inputBytes = utf8encoder.GetBytes(txtToDb.Text);
//Console.WriteLine(inputBytes.ToString());
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-1];
encryptedStream.Read(bResult,0,int.Parse(encryptedStream.Length.ToString())-1);
cryptStream.Close();
myDecrypt(bResult);
return bResult;
}
static string myDecrypt(Byte[] inputInBytes)
{
//UTF8Encoding utf8encoder = new UTF8Encoding();
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-1];
decryptedStream.Read(result,0,int.Parse(decryptedStream.Length.ToString()));
cryptStream.Close();
UTF8Encoding myutf = new UTF8Encoding();
return myutf.GetString(result).ToString();
}
I have seem few messages posted regaring this but as yet have been
able to get this code to work. The plan was to encrypt some string
then pass the result to another function that woudl decrypt it -
please see below. Anyway i keep getting a 'bad data' exception. I'm
totally at a loss, so any help woudl be greatly appreciated.
public Byte[] myEncrypt()
{
UTF8Encoding utf8encoder = new UTF8Encoding();
Byte[] inputBytes = utf8encoder.GetBytes(txtToDb.Text);
//Console.WriteLine(inputBytes.ToString());
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-1];
encryptedStream.Read(bResult,0,int.Parse(encryptedStream.Length.ToString())-1);
cryptStream.Close();
myDecrypt(bResult);
return bResult;
}
static string myDecrypt(Byte[] inputInBytes)
{
//UTF8Encoding utf8encoder = new UTF8Encoding();
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-1];
decryptedStream.Read(result,0,int.Parse(decryptedStream.Length.ToString()));
cryptStream.Close();
UTF8Encoding myutf = new UTF8Encoding();
return myutf.GetString(result).ToString();
}