2016-12-26 35 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上開放?謝謝 –