2016-12-26 34 views
-2

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

+0

你能显示你的代码吗? –

回答

1

按照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); 
+0

感谢您的回复。但我已经有点想通了。这对我有用。 – saravanan

+1

为什么不回答自己的问题,而不是让它在SO上开放?谢谢 –