lolelo
lolelo

Reputation: 740

System.Security.CryptpgraphicUnexpectedOperationException when creating ICryptoTransform

I get an error: System.Security.Cryptography.CryptographicUnexpectedOperationException

When I run the following:

DESCryptoServiceProvider dESCrypto = new DESCryptoServiceProvider();
byte[] key = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
byte[] iv = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
var v = dESCrypto.CreateEncryptor(key, iv);

Stacktrace: at Crimson.CommonCrypto.Cryptor.Create (Crimson.CommonCrypto.CCOperation operation, Crimson.CommonCrypto.CCAlgorithm algorithm, Crimson.CommonCrypto.CCOptions options, System.Byte[] key, System.Byte[] iv) [0x0005d] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.2.1.12/src/Xamarin.iOS/mcs/class/corlib/CommonCrypto/CommonCrypto.cs:99

It is in a Xamarin project where I was hoping to use it like in the second answer in this thread C# Encrypt serialized file before writing to disk

How can I solve this problem?

Edit: Using AES and 8 values instead of 9 - unfortunately creates the same exception

Upvotes: 2

Views: 142

Answers (1)

Zergatul
Zergatul

Reputation: 2010

You are trying to set key/IV size 9 bytes, but DES can only work with 8 bytes. Change it to something like below:

DESCryptoServiceProvider dESCrypto = new DESCryptoServiceProvider();
byte[] key = { 0, 1, 2, 3, 4, 5, 6, 7 };
byte[] iv = { 0, 1, 2, 3, 4, 5, 6, 7 };
var v = dESCrypto.CreateEncryptor(key, iv);

Upvotes: 3

Related Questions