大马资讯论坛 - 马来西亚中文资讯平台

 找回密码
 注册
搜索
打印 上一主题 下一主题

[教学] Xamarin Studio PCLCrypto library AES Encrypt Decrypt

[复制链接]
跳转到指定楼层
1#
发表于 2017-2-7 20:47:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在xamarin你可以使用nuget来安装 PCLCrypto
https://github.com/AArnott/PCLCrypto

取代 PHP MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC,  System.Security.Cryptography, RijndaelManaged, sample_visual_c_sharp_aes256_imcore_net

  1.     public static string EncryptAES(string content, string password)
  2.     {
  3.         byte[] keyMaterial = Encoding.UTF8.GetBytes(password);
  4.         byte[] data = Encoding.UTF8.GetBytes(content);
  5.         byte[] iv = new byte[keyMaterial.Length];
  6.         var provider = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);
  7.         var key = provider.CreateSymmetricKey(keyMaterial);
  8.         byte[] cipherText = WinRTCrypto.CryptographicEngine.Encrypt(key, data, iv);
  9.         return Convert.ToBase64String(cipherText);
  10.     }

  11.     public static string DecryptAES(string content, string password)
  12.     {
  13.         byte[] keyMaterial = Encoding.UTF8.GetBytes(password);
  14.         byte[] data = Convert.FromBase64String(content);
  15.         byte[] iv = new byte[keyMaterial.Length];
  16.         var provider = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);
  17.         var key = provider.CreateSymmetricKey(keyMaterial);
  18.         byte[] cipherText = WinRTCrypto.CryptographicEngine.Decrypt(key, data, iv);
  19.         return Encoding.UTF8.GetString(cipherText, 0, cipherText.Length);
  20.     }
复制代码


source : http://stackoverflow.com/questio ... n-windows-phone-8-1

手机版|大马资讯论坛  

GMT+8, 2024-12-5 05:45 , Processed in 0.024543 second(s), 9 queries , File On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表