Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ICryptoTransform.TransformFinalBlock vs ICryptoTransform.TransformBlock

Tags:

.net

I am Learning cryptography in .net, why method 1 works while 2 fired argument exception. See Symmetric Algorithm exception for a complete code

1- ICryptoTransform.TransformFinalBlock

2- ICryptoTransform.TransformBlock

Thanks

like image 980
Costa Avatar asked Jan 05 '10 15:01

Costa


1 Answers

You should be using a CryptoStream, which will automatically call the correct ICryptoTransform methods.

For example:

var stream = new MemoryStream();
using (var transform = symAlgo.CreateEncryptor())
using (var cryptoStream = new CryptoStream(stream, transform, CryptoStreamMode.Write))
using (var writer = new StreamWriter(cryptoStream))
    writer.Write(someString);

byte[] cipherBytes = stream.ToArray();
like image 136
SLaks Avatar answered Oct 07 '22 00:10

SLaks