0

I have next code on .net 6:

using var algorithm = TripleDES.Create();
var bytes1 = Encoding.UTF8.GetBytes(Keys.CryptKey);
var bytes2 = Encoding.UTF8.GetBytes(Keys.Crypt4);

using var transform = algorithm.CreateEncryptor(bytes1, bytes2);
algorithm.Mode = CipherMode.ECB;
var buffer = Encoding.UTF8.GetBytes(text);
var outBuffer = transform.TransformFinalBlock(buffer, 0, buffer.Length);
return Convert.ToBase64String(outBuffer);

It works on .net framework 4.8.

bytes1 and bytes2 are 24 length

When I create an encryptor, I get this error: Specified initialization vector (IV) does not match the block size for this algorithm. (Parameter 'rgbIV')'

I tried to set keys to 8 and 16 length

vatbub
  • 2,713
  • 18
  • 41
  • What happens if you write to a `CryptoStream` as shown in [this answer](https://stackoverflow.com/a/53353418/3744182)? Do you get the expected result? – dbc Dec 20 '22 at 01:46
  • @dbc in that answer encryptor creating before CryptoStream anyway – Matvey Radchenko Dec 20 '22 at 01:58
  • show us the code where you're setting the keys to 8 - you need to ensure that the initialization vector (IV) specified for the TripleDES algorithm is the correct length for the block size of the algorithm. The block size for TripleDES is 8 bytes, so you need to ensure that the IV you are using is 8 bytes long. – jazb Dec 20 '22 at 01:59
  • @jazb Thanks! When i set iv to 8 bytes, i set key to 8 bytes at the same. But why it works on .net framework 4.8? – Matvey Radchenko Dec 20 '22 at 02:06
  • 1
    .NET Framework 4.8 accepts a longer IV, but only considers the first 8 bytes. With .NET 6 a too long IV is not accepted. The latter is the preferable way to deal with an invalid IV. – Topaco Dec 21 '22 at 17:31

0 Answers0