使用 PCLCrypto 加密字符串
Encrypt string using PCLCrypto
我有一个从服务提供商处获得的 RSA 密钥。我只想使用 PCLCrypto 库使用该 RSA 密钥加密字符串数据。我不想使用 PCLCrypto 创建 RSA 密钥。我只想加密数据。 (我正在 xamarin 中开发 PCL 组件。)
遵循 documentation for AES encryption 并针对 RSA 进行修改。使用 AsymmetricAlgorithm.RsaPkcs1
作为算法提供者。
以下示例适用于 AES。
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);
我有一个从服务提供商处获得的 RSA 密钥。我只想使用 PCLCrypto 库使用该 RSA 密钥加密字符串数据。我不想使用 PCLCrypto 创建 RSA 密钥。我只想加密数据。 (我正在 xamarin 中开发 PCL 组件。)
遵循 documentation for AES encryption 并针对 RSA 进行修改。使用 AsymmetricAlgorithm.RsaPkcs1
作为算法提供者。
以下示例适用于 AES。
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);