在 Mac OS X 上的 Xamarin Studio 6.1 中使用 System.Security.Cryptography 时出现程序集引用错误 (CS0234)

Assembly reference error (CS0234) when using System.Security.Cryptography in Xamarin Studio 6.1 on Mac OS X

运行 OS X El Capitan 10.11.5 上的 Xamarin Studio(社区)6.1(内部版本 4963),我正在尝试在我的解决方案中使用 RNGCryptoService(目标 iOS 和Android),我需要为其使用命名空间 System.Security.Cryptography。但是,似乎 using System.Security.Cryptography; 然后在我的代码中某处调用 RNGCryptoServiceProvider random = new RNGCryptoServiceProvider(); 给了我错误。

Error CS0234: The type or namespace name 'Cryptography' does not exist in the namespace 'System.Security'. Are you missing an assembly reference? (CS0234)

我试图在 NuGet 存储库(包括预发布版本)中查找 System.Security.Cryptography 包,但我没有找到任何东西。

我错过了什么?我在哪里可以找到程序集参考?

您需要 System.Security.Cryptography.Algorithms 包才能使用 RNGCryptoServiceProvider。使用 NuGet v 3.4 或更高版本安装它:

> PM Install-Package System.Security.Cryptography.Algorithms

您可能还需要修改 project.json 文件,使此包成为 dotnet5.4 的依赖项而不是全局依赖项,以阻止编译器抱怨重复的 class 声明。

跟进:System.Security.dll 似乎不支持 Xamarin.iOS 或 Android。作为替代方案,您可能会幸运地使用 Jeffrey Stedfast 的 Bouncy Castle 密码学库 bc-csharp 的分支,用于 Xamarin.Android 和 Xamarin.iOS.

另一种选择可能是使用 PCLCrypto library with this helper class 和 NuGet 的解决方法。