GetKey() public method

The public key contained in the object.
If the key algorithm is not recognised.
public GetKey ( ) : AsymmetricKeyParameter
return Org.BouncyCastle.Crypto.AsymmetricKeyParameter
Ejemplo n.º 1
0
            public override void AddSessionInfo(
                byte[] sessionInfo)
            {
                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 key = pubKey.GetKey();

                c.Init(true, key);

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

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

                case PublicKeyAlgorithmTag.ElGamalEncrypt:
                case PublicKeyAlgorithmTag.ElGamalGeneral:
                    byte[] b1 = new byte[encKey.Length / 2];
                    byte[] b2 = new byte[encKey.Length / 2];

                    Array.Copy(encKey, 0, b1, 0, b1.Length);
                    Array.Copy(encKey, b1.Length, b2, 0, b2.Length);

                    data = new BigInteger[]
                    {
                        new BigInteger(1, b1),
                        new BigInteger(1, b2)
                    };
                    break;

                default:
                    throw new PgpException("unknown asymmetric algorithm: " + encAlgorithm);
                }
            }
            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);
                }
            }
Ejemplo n.º 3
0
            private byte[] EncryptSessionInfo(byte[] sessionInfo, SecureRandom random)
            {
                if (pubKey.Algorithm != PublicKeyAlgorithmTag.EC)
                {
                    IBufferedCipher cipher;
                    switch (pubKey.Algorithm)
                    {
                    case PublicKeyAlgorithmTag.RsaGeneral:
                    case PublicKeyAlgorithmTag.RsaEncrypt:
                        cipher = CipherUtilities.GetCipher("RSA//PKCS1Padding");
                        break;

                    case PublicKeyAlgorithmTag.ElGamalEncrypt:
                    case PublicKeyAlgorithmTag.ElGamalGeneral:
                        cipher = 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(string.Concat((object)"unknown asymmetric algorithm: ", (object)pubKey.Algorithm));
                    }
                    AsymmetricKeyParameter parameters = pubKey.GetKey();
                    cipher.Init(forEncryption: true, new ParametersWithRandom(parameters, random));
                    return(cipher.DoFinal(sessionInfo));
                }
                ECDHPublicBcpgKey eCDHPublicBcpgKey = (ECDHPublicBcpgKey)pubKey.PublicKeyPacket.Key;
                IAsymmetricCipherKeyPairGenerator keyPairGenerator = GeneratorUtilities.GetKeyPairGenerator("ECDH");

                keyPairGenerator.Init(new ECKeyGenerationParameters(eCDHPublicBcpgKey.CurveOid, random));
                AsymmetricCipherKeyPair asymmetricCipherKeyPair = keyPairGenerator.GenerateKeyPair();
                ECPrivateKeyParameters  eCPrivateKeyParameters  = (ECPrivateKeyParameters)asymmetricCipherKeyPair.Private;
                ECPublicKeyParameters   eCPublicKeyParameters   = (ECPublicKeyParameters)asymmetricCipherKeyPair.Public;
                ECPublicKeyParameters   eCPublicKeyParameters2  = (ECPublicKeyParameters)pubKey.GetKey();
                ECPoint      s           = eCPublicKeyParameters2.Q.Multiply(eCPrivateKeyParameters.D).Normalize();
                KeyParameter parameters2 = new KeyParameter(Rfc6637Utilities.CreateKey(pubKey.PublicKeyPacket, s));
                IWrapper     wrapper     = PgpUtilities.CreateWrapper(eCDHPublicBcpgKey.SymmetricKeyAlgorithm);

                wrapper.Init(forWrapping: true, new ParametersWithRandom(parameters2, random));
                byte[] array   = PgpPad.PadSessionData(sessionInfo);
                byte[] array2  = wrapper.Wrap(array, 0, array.Length);
                byte[] encoded = new MPInteger(new BigInteger(1, eCPublicKeyParameters.Q.GetEncoded(compressed: false))).GetEncoded();
                byte[] array3  = new byte[encoded.Length + 1 + array2.Length];
                global::System.Array.Copy((global::System.Array)encoded, 0, (global::System.Array)array3, 0, encoded.Length);
                array3[encoded.Length] = (byte)array2.Length;
                global::System.Array.Copy((global::System.Array)array2, 0, (global::System.Array)array3, encoded.Length + 1, array2.Length);
                return(array3);
            }
Ejemplo n.º 4
0
        /// <summary>Initialise the signature object for verification.</summary>
        public void InitVerify(
            PgpPublicKey pubKey)
        {
            lastb = 0;

            try
            {
                sig.Init(false, pubKey.GetKey());
            }
            catch (InvalidKeyException e)
            {
                throw new PgpException("invalid key.", e);
            }
        }
Ejemplo n.º 5
0
 public void InitVerify(PgpPublicKey pubKey)
 {
     lastb = 0;
     if (sig == null)
     {
         GetSig();
     }
     try
     {
         sig.Init(forSigning: false, pubKey.GetKey());
     }
     catch (InvalidKeyException exception)
     {
         throw new PgpException("invalid key.", exception);
     }
 }
Ejemplo n.º 6
0
 public void InitVerify(PgpPublicKey pubKey)
 {
     lastb = 0;
     try
     {
         sig = SignerUtilities.GetSigner(PgpUtilities.GetSignatureName(sigPack.KeyAlgorithm, sigPack.HashAlgorithm));
     }
     catch (global::System.Exception exception)
     {
         throw new PgpException("can't set up signature object.", exception);
     }
     try
     {
         sig.Init(forSigning: false, pubKey.GetKey());
     }
     catch (InvalidKeyException exception2)
     {
         throw new PgpException("invalid key.", exception2);
     }
 }
		/// <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);
            }
        }
Ejemplo n.º 8
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);
            }
        }
Ejemplo n.º 9
0
            private byte[] EncryptSessionInfo(byte[] sessionInfo, SecureRandom random)
            {
                if (pubKey.Algorithm != PublicKeyAlgorithmTag.ECDH)
                {
                    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));
                    return(c.DoFinal(sessionInfo));
                }

                ECDHPublicBcpgKey ecKey = (ECDHPublicBcpgKey)pubKey.PublicKeyPacket.Key;

                // Generate the ephemeral key pair
                IAsymmetricCipherKeyPairGenerator gen = GeneratorUtilities.GetKeyPairGenerator("ECDH");

                gen.Init(new ECKeyGenerationParameters(ecKey.CurveOid, random));

                AsymmetricCipherKeyPair ephKp   = gen.GenerateKeyPair();
                ECPrivateKeyParameters  ephPriv = (ECPrivateKeyParameters)ephKp.Private;
                ECPublicKeyParameters   ephPub  = (ECPublicKeyParameters)ephKp.Public;

                ECPublicKeyParameters pub = (ECPublicKeyParameters)pubKey.GetKey();
                ECPoint S = pub.Q.Multiply(ephPriv.D).Normalize();

                KeyParameter key = new KeyParameter(Rfc6637Utilities.CreateKey(pubKey.PublicKeyPacket, S));

                IWrapper w = PgpUtilities.CreateWrapper(ecKey.SymmetricKeyAlgorithm);

                w.Init(true, new ParametersWithRandom(key, random));

                byte[] paddedSessionData = PgpPad.PadSessionData(sessionInfo);

                byte[] C  = w.Wrap(paddedSessionData, 0, paddedSessionData.Length);
                byte[] VB = new MPInteger(new BigInteger(1, ephPub.Q.GetEncoded(false))).GetEncoded();

                byte[] rv = new byte[VB.Length + 1 + C.Length];

                Array.Copy(VB, 0, rv, 0, VB.Length);
                rv[VB.Length] = (byte)C.Length;
                Array.Copy(C, 0, rv, VB.Length + 1, C.Length);

                return(rv);
            }