Exemplo n.º 1
0
            public override void AddSessionInfo(
                byte[]                  si,
                SecureRandom random)
            {
                IBufferedCipher c;

                switch (pubKey.Algorithm)
                {
                case PublicKeyAlgorithmTag.RsaEncrypt:
                case PublicKeyAlgorithmTag.RsaGeneral:
                    c = CipherUtilities.GetCipher("RSA//PKCS1Padding");
                    break;

                case PublicKeyAlgorithmTag.ElGamalEncrypt:
                case PublicKeyAlgorithmTag.ElGamalGeneral:
                    c = CipherUtilities.GetCipher("ElGamal/ECB/PKCS1Padding");
                    break;

                case PublicKeyAlgorithmTag.Dsa:
                    throw new PgpException("Can't use DSA for encryption.");

                case PublicKeyAlgorithmTag.ECDsa:
                    throw new PgpException("Can't use ECDSA for encryption.");

                default:
                    throw new PgpException("unknown asymmetric algorithm: " + pubKey.Algorithm);
                }

                AsymmetricKeyParameter akp = pubKey.GetKey();

                c.Init(true, new ParametersWithRandom(akp, random));

                byte[] encKey = c.DoFinal(si);

                switch (pubKey.Algorithm)
                {
                case PublicKeyAlgorithmTag.RsaEncrypt:
                case PublicKeyAlgorithmTag.RsaGeneral:
                    data = new BigInteger[] { new BigInteger(1, encKey) };
                    break;

                case PublicKeyAlgorithmTag.ElGamalEncrypt:
                case PublicKeyAlgorithmTag.ElGamalGeneral:
                    int halfLength = encKey.Length / 2;
                    data = new BigInteger[]
                    {
                        new BigInteger(1, encKey, 0, halfLength),
                        new BigInteger(1, encKey, halfLength, halfLength)
                    };
                    break;

                default:
                    throw new PgpException("unknown asymmetric algorithm: " + encAlgorithm);
                }
            }
Exemplo n.º 2
0
 public void InitVerify(
     PgpPublicKey pubKey)
 {
     lastb = 0;
     if (sig == null)
     {
         GetSig();
     }
     try
     {
         sig.Init(false, pubKey.GetKey());
     }
     catch (InvalidKeyException e)
     {
         throw new PgpException("invalid key.", e);
     }
 }
Exemplo n.º 3
0
        /// <summary>Initialise the signature object for verification.</summary>
        public void InitVerify(
            PgpPublicKey pubKey)
        {
            lastb = 0;

            try
            {
                sig = SignerUtilities.GetSigner(
                    PgpUtilities.GetSignatureName(sigPack.KeyAlgorithm, sigPack.HashAlgorithm));
            }
            catch (Exception e)
            {
                throw new PgpException("can't set up signature object.", e);
            }

            try
            {
                sig.Init(false, pubKey.GetKey());
            }
            catch (InvalidKeyException e)
            {
                throw new PgpException("invalid key.", e);
            }
        }
Exemplo n.º 4
0
        /// <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 PublicKeySecretKey(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);
        }