/// <summary> /// Public and secret key provider. /// </summary> /// <param name="publicKey">The public key data.</param> /// <param name="secretKey">The secret key data.</param> /// <param name="keyID">The unique key id of the public secret key pair.</param> /// <param name="password">The password used to protect the secret key.</param> /// <returns>The RSA cryto service provider.</returns> public RSACryptoServiceProvider PublicKeySecretKeyProvider(System.IO.Stream publicKey, System.IO.Stream secretKey, long keyID, string password = null) { // Read the public key data. Key.Bcpg.OpenPgp.PgpPublicKey pgpPublicKey = ReadPublicKey(publicKey); // Find the secret key Key.Bcpg.OpenPgp.PgpPrivateKey privateKey = null; Key.Bcpg.OpenPgp.PgpSecretKeyRingBundle secretKeyRingBundle = new Key.Bcpg.OpenPgp.PgpSecretKeyRingBundle(Key.Bcpg.OpenPgp.PgpUtilities.GetDecoderStream(secretKey)); // Find the private key (secret key). privateKey = FindSecretKey(secretKeyRingBundle, keyID, password.ToArray()); // Assign the rsa parameters. RSAParameters rsaPrivateParam = new RSAParameters(); Key.Crypto.Parameters.RsaKeyParameters rsaPrivatePublic = (Key.Crypto.Parameters.RsaKeyParameters)pgpPublicKey.GetKey(); Key.Crypto.Parameters.RsaPrivateCrtKeyParameters rsaCrtPrivateParam = (Key.Crypto.Parameters.RsaPrivateCrtKeyParameters)privateKey.Key; // Assign the rsa parameters. rsaPrivateParam.D = rsaCrtPrivateParam.Exponent.ToByteArrayUnsigned(); rsaPrivateParam.DP = rsaCrtPrivateParam.DP.ToByteArrayUnsigned(); rsaPrivateParam.DQ = rsaCrtPrivateParam.DQ.ToByteArrayUnsigned(); rsaPrivateParam.InverseQ = rsaCrtPrivateParam.QInv.ToByteArrayUnsigned(); rsaPrivateParam.P = rsaCrtPrivateParam.P.ToByteArrayUnsigned(); rsaPrivateParam.Q = rsaCrtPrivateParam.Q.ToByteArrayUnsigned(); rsaPrivateParam.Modulus = rsaPrivatePublic.Modulus.ToByteArrayUnsigned(); rsaPrivateParam.Exponent = rsaPrivatePublic.Exponent.ToByteArrayUnsigned(); // Create the encyption provider. RSACryptoServiceProvider rsaEncryptProvider = new RSACryptoServiceProvider(); rsaEncryptProvider.ImportParameters(rsaPrivateParam); // Return the rsa provider. return(rsaEncryptProvider); }
/// <summary> /// The public key contained in the object. /// </summary> /// <returns>The RSA cryto service provider.</returns> public RSACryptoServiceProvider GetKey() { // If the pgp public key exists. if (_pgpPublicKey != null) { // Get the ras key parameters Key.Crypto.Parameters.RsaKeyParameters rsaPublicKey = (Key.Crypto.Parameters.RsaKeyParameters)_pgpPublicKey.GetKey(); // 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); } else { return(null); } }