Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AesCryptoServiceProvider in MonoTouch

I'm working in the iPad version of my app and I have a function in what I use AesCryptoServiceProvider for example the following lines

 using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider())
 {
    setAesProviderSettings(aesProvider);
    result = aesProvider.CreateEncryptor().TransformFinalBlock(plainBytes, 0, plainBytes.Length);
 }

It works in my Android app(MonDroid) and in a .NET(WPF) application but in iOS I got this error;

Error  5      The type or namespace name 'AesCryptoServiceProvider' could not be found (are you missing a using directive or an assembly reference?)     

The function is in my core project and I use the project linker to reference it in the iPad project and also I use project linker in the Android version, of course in the three platforms is the same file with all the references that it need. I use Xamarin with Visual Studio 12.

like image 361
HDEV Avatar asked Sep 27 '13 14:09

HDEV


1 Answers

Use the factory method Aes.Create () and your code will be more portable across different .NET versions/platforms. E.g.

using (var aesProvider = Aes.Create ()) {
   setAesProviderSettings(aesProvider);
   result = aesProvider.CreateEncryptor().TransformFinalBlock(plainBytes, 0, plainBytes.Length);
}

In Xamarin.iOS it will return AesManaged (from System.Core.dll) but, in reality, it's calling Apple CommonCrypto (it's not a managed implementation) which can be hardware accelerated (depending on your devices).

like image 112
poupou Avatar answered Oct 03 '22 09:10

poupou