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.Get Bytes(txtToDb.T ext);
//Console.WriteLi ne(inputBytes.T oString());
TripleDESCrypto ServiceProvider tdesProvider = new
TripleDESCrypto ServiceProvider ();
ICryptoTransfor m cryptoTransform =
tdesProvider.Cr eateEncryptor(t ripleDes.Key,tr ipleDes.IV);
MemoryStream encryptedStream = new MemoryStream();
CryptoStream cryptStream = new
CryptoStream(en cryptedStream,c ryptoTransform, CryptoStreamMod e.Write);
cryptStream.Wri te(inputBytes,0 ,inputBytes.Len gth);
cryptStream.Flu shFinalBlock();
encryptedStream .Position = 0;
Byte[] bResult = new Byte[encryptedStream .Length-1];
encryptedStream .Read(bResult,0 ,int.Parse(encr yptedStream.Len gth.ToString())-1);
cryptStream.Clo se();
myDecrypt(bResu lt);
return bResult;
}
static string myDecrypt(Byte[] inputInBytes)
{
//UTF8Encoding utf8encoder = new UTF8Encoding();
TripleDESCrypto ServiceProvider tdesProvider = new
TripleDESCrypto ServiceProvider ();
ICryptoTransfor m cryptoTranform =
tdesProvider.Cr eateDecryptor(t ripleDes.Key,tr ipleDes.IV);
MemoryStream decryptedStream = new MemoryStream();
CryptoStream cryptStream = new
CryptoStream(de cryptedStream,c ryptoTranform,C ryptoStreamMode .Write);
cryptStream.Wri te(inputInBytes ,0,inputInBytes .Length);
cryptStream.Flu shFinalBlock();
decryptedStream .Position=0;
Byte[] result = new Byte[decryptedStream .Length-1];
decryptedStream .Read(result,0, int.Parse(decry ptedStream.Leng th.ToString())) ;
cryptStream.Clo se();
UTF8Encoding myutf = new UTF8Encoding();
return myutf.GetString (result).ToStri ng();
}
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.Get Bytes(txtToDb.T ext);
//Console.WriteLi ne(inputBytes.T oString());
TripleDESCrypto ServiceProvider tdesProvider = new
TripleDESCrypto ServiceProvider ();
ICryptoTransfor m cryptoTransform =
tdesProvider.Cr eateEncryptor(t ripleDes.Key,tr ipleDes.IV);
MemoryStream encryptedStream = new MemoryStream();
CryptoStream cryptStream = new
CryptoStream(en cryptedStream,c ryptoTransform, CryptoStreamMod e.Write);
cryptStream.Wri te(inputBytes,0 ,inputBytes.Len gth);
cryptStream.Flu shFinalBlock();
encryptedStream .Position = 0;
Byte[] bResult = new Byte[encryptedStream .Length-1];
encryptedStream .Read(bResult,0 ,int.Parse(encr yptedStream.Len gth.ToString())-1);
cryptStream.Clo se();
myDecrypt(bResu lt);
return bResult;
}
static string myDecrypt(Byte[] inputInBytes)
{
//UTF8Encoding utf8encoder = new UTF8Encoding();
TripleDESCrypto ServiceProvider tdesProvider = new
TripleDESCrypto ServiceProvider ();
ICryptoTransfor m cryptoTranform =
tdesProvider.Cr eateDecryptor(t ripleDes.Key,tr ipleDes.IV);
MemoryStream decryptedStream = new MemoryStream();
CryptoStream cryptStream = new
CryptoStream(de cryptedStream,c ryptoTranform,C ryptoStreamMode .Write);
cryptStream.Wri te(inputInBytes ,0,inputInBytes .Length);
cryptStream.Flu shFinalBlock();
decryptedStream .Position=0;
Byte[] result = new Byte[decryptedStream .Length-1];
decryptedStream .Read(result,0, int.Parse(decry ptedStream.Leng th.ToString())) ;
cryptStream.Clo se();
UTF8Encoding myutf = new UTF8Encoding();
return myutf.GetString (result).ToStri ng();
}
Comment