代码之家  ›  专栏  ›  技术社区  ›  Prashant.J

PCLCrypto RSA算法:如何在Xamarin中使用给定的公钥和私钥对文本进行加密和解密

  •  0
  • Prashant.J  · 技术社区  · 7 年前

    我有一个RSA密钥,是从服务提供商那里得到的。我只想使用PCLCrypto库用RSA密钥加密字符串数据。我不想使用PCLCrypto创建RSA密钥。我只想加密数据。(我正在开发xamarin中的PCL组件。)

    0 回复  |  直到 9 年前
        1
  •  1
  •   der_michael    9 年前

    跟随 documentation for AES encryption AsymmetricAlgorithm.RsaPkcs1 作为算法提供者。

    byte[] keyMaterial;
    byte[] data;
    var provider = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);
    var key = provider.CreateSymmetricKey(keyMaterial);
    
    // The IV may be null, but supplying a random IV increases security.
    // The IV is not a secret like the key is.
    // You can transmit the IV (w/o encryption) alongside the ciphertext.
    var iv = WinRTCrypto.CryptographicBuffer.GenerateRandom(provider.BlockLength);
    
    byte[] cipherText = WinRTCrypto.CryptographicEngine.Encrypt(key, data, iv);
    
    // When decrypting, use the same IV that was passed to encrypt.
    byte[] plainText = WinRTCrypto.CryptographicEngine.Decrypt(key, cipherText, iv);
    
    推荐文章