/// <summary> /// Get the RSA crypto service provider for encryption with the public key. /// </summary> /// <param name="publicKey">The stream containing the public key data.</param> /// <param name="password">The password used to decrypt the key within the file.</param> /// <returns>The RSA cryto service provider with the public key.</returns> public RSACryptoServiceProvider PublicKeyEncryptionProvider(StreamReader publicKey, string password = null) { Key.OpenSsl.PemReader publicKeyReader = null; if (String.IsNullOrEmpty(password)) { // Read the public key file. publicKeyReader = new Key.OpenSsl.PemReader(publicKey); } else { // Read the public key file. publicKeyReader = new Key.OpenSsl.PemReader(publicKey, new PasswordFinder(password)); } // Get the ras key parameters Key.Crypto.Parameters.RsaKeyParameters rsaPublicKey = (Key.Crypto.Parameters.RsaKeyParameters)publicKeyReader.ReadObject(); // Assign the rsa parameters. RSAParameters rsaPublicParam = new RSAParameters(); rsaPublicParam.Exponent = rsaPublicKey.Exponent.ToByteArrayUnsigned(); rsaPublicParam.Modulus = rsaPublicKey.Modulus.ToByteArrayUnsigned(); // Create the encyption provider. RSACryptoServiceProvider rsaEncryptProvider = new RSACryptoServiceProvider(); rsaEncryptProvider.ImportParameters(rsaPublicParam); // Return the rsa provider. return(rsaEncryptProvider); }
/// <summary> /// Get the RSA crypto service provider. /// </summary> /// <param name="publicPrivateKey">The stream containing the public and private key data.</param> /// <param name="password">The password used to decrypt the key within the file.</param> /// <returns>The RSA cryto service provider.</returns> public RSACryptoServiceProvider RSAProvider(StreamReader publicPrivateKey, string password = null) { Key.OpenSsl.PemReader keyReader = null; if (String.IsNullOrEmpty(password)) { // Read the key file. keyReader = new Key.OpenSsl.PemReader(publicPrivateKey); } else { // Read the key file. keyReader = new Key.OpenSsl.PemReader(publicPrivateKey, new PasswordFinder(password)); } // Get the ras key parameters Key.Crypto.AsymmetricCipherKeyPair rsaPrivateKey = (Key.Crypto.AsymmetricCipherKeyPair)keyReader.ReadObject(); // Assign the rsa parameters. RSAParameters rsaParam = new RSAParameters(); Key.Crypto.Parameters.RsaKeyParameters rsaPrivatePublic = (Key.Crypto.Parameters.RsaKeyParameters)rsaPrivateKey.Public; Key.Crypto.Parameters.RsaPrivateCrtKeyParameters rsaCrtPrivateParam = (Key.Crypto.Parameters.RsaPrivateCrtKeyParameters)rsaPrivateKey.Private; rsaParam.D = rsaCrtPrivateParam.Exponent.ToByteArrayUnsigned(); rsaParam.DP = rsaCrtPrivateParam.DP.ToByteArrayUnsigned(); rsaParam.DQ = rsaCrtPrivateParam.DQ.ToByteArrayUnsigned(); rsaParam.InverseQ = rsaCrtPrivateParam.QInv.ToByteArrayUnsigned(); rsaParam.P = rsaCrtPrivateParam.P.ToByteArrayUnsigned(); rsaParam.Q = rsaCrtPrivateParam.Q.ToByteArrayUnsigned(); rsaParam.Modulus = rsaCrtPrivateParam.Modulus.ToByteArrayUnsigned(); rsaParam.Exponent = rsaCrtPrivateParam.PublicExponent.ToByteArrayUnsigned(); // Create the encyption provider. RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(); rsaProvider.ImportParameters(rsaParam); // Return the rsa provider. return(rsaProvider); }