public AsymmetricEncryptionResult Encrypt(AsymmetricEncryptionArguments arguments) { var encryptionResult = new AsymmetricEncryptionResult(); try { var myRSA = new RSACryptoServiceProvider(); var rsaKeyForEncryption = arguments.PublicKeyForEncryption.ToString(); myRSA.FromXmlString(rsaKeyForEncryption); byte[] data = Encoding.UTF8.GetBytes(arguments.PlainText); byte[] cipherText = myRSA.Encrypt(data, false); encryptionResult.CipherBytes = cipherText; encryptionResult.CipherText = Convert.ToBase64String(cipherText); encryptionResult.Success = true; } catch (Exception ex) { encryptionResult.ExceptionMessage = ex.Message; } return encryptionResult; }
public AsymmetricEncryptionResult Encrypt(AsymmetricEncryptionArguments arguments) { var encryptionResult = new AsymmetricEncryptionResult(); try { var myRSA = new RSACryptoServiceProvider(); var rsaKeyForEncryption = arguments.PublicKeyForEncryption.ToString(); myRSA.FromXmlString(rsaKeyForEncryption); byte[] data = Encoding.UTF8.GetBytes(arguments.PlainText); byte[] cipherText = myRSA.Encrypt(data, false); encryptionResult.CipherBytes = cipherText; encryptionResult.CipherText = Convert.ToBase64String(cipherText); encryptionResult.Success = true; } catch (Exception ex) { encryptionResult.ExceptionMessage = ex.Message; } return(encryptionResult); }
public void RsaAsymmetricCryptographyServiceEncryptDecryptTest() { var asymmetricSevice = new RsaAsymmetricCryptographyService(); var keyPairGenerationResult = asymmetricSevice.GenerateAsymmetricKeys(1024); Assert.AreEqual(true, keyPairGenerationResult.Success); var encryptionArgs = new AsymmetricEncryptionArguments { PlainText = "Text to be encrypted", PublicKeyForEncryption = keyPairGenerationResult.PublicKeyOnlyXml }; var encryptionResult = asymmetricSevice.Encrypt(encryptionArgs); Assert.AreEqual(true, encryptionResult.Success); var decryptionArguments = new AsymmetricDecryptionArguments() { CipherText = encryptionResult.CipherText, FullAsymmetricKey = keyPairGenerationResult.FullKeyPairXml }; var decryptionResult = asymmetricSevice.Decrypt(decryptionArguments); Assert.AreEqual(true, decryptionResult.Success); }