private byte[] RecoverSessionData(PgpPrivateKey privKey)
    {
        byte[][] encSessionKey = keyData.GetEncSessionKey();
        if (keyData.Algorithm == PublicKeyAlgorithmTag.EC)
        {
            ECDHPublicBcpgKey eCDHPublicBcpgKey = (ECDHPublicBcpgKey)privKey.PublicKeyPacket.Key;
            X9ECParameters    x9ECParameters    = ECKeyPairGenerator.FindECCurveByOid(eCDHPublicBcpgKey.CurveOid);
            byte[]            array             = encSessionKey[0];
            int    num    = (((array[0] & 0xFF) << 8) + (array[1] & 0xFF) + 7) / 8;
            byte[] array2 = new byte[num];
            Array.Copy(array, 2, array2, 0, num);
            byte[] array3 = new byte[array[num + 2]];
            Array.Copy(array, 2 + num + 1, array3, 0, array3.Length);
            ECPoint eCPoint = x9ECParameters.Curve.DecodePoint(array2);
            ECPrivateKeyParameters eCPrivateKeyParameters = (ECPrivateKeyParameters)privKey.Key;
            ECPoint      s          = eCPoint.Multiply(eCPrivateKeyParameters.D).Normalize();
            KeyParameter parameters = new KeyParameter(Rfc6637Utilities.CreateKey(privKey.PublicKeyPacket, s));
            IWrapper     wrapper    = PgpUtilities.CreateWrapper(eCDHPublicBcpgKey.SymmetricKeyAlgorithm);
            wrapper.Init(forWrapping: false, parameters);
            return(PgpPad.UnpadSessionData(wrapper.Unwrap(array3, 0, array3.Length)));
        }
        IBufferedCipher keyCipher = GetKeyCipher(keyData.Algorithm);

        try
        {
            keyCipher.Init(forEncryption: false, privKey.Key);
        }
        catch (InvalidKeyException exception)
        {
            throw new PgpException("error setting asymmetric cipher", exception);
        }
        if (keyData.Algorithm == PublicKeyAlgorithmTag.RsaEncrypt || keyData.Algorithm == PublicKeyAlgorithmTag.RsaGeneral)
        {
            byte[] array4 = encSessionKey[0];
            keyCipher.ProcessBytes(array4, 2, array4.Length - 2);
        }
        else
        {
            ElGamalPrivateKeyParameters elGamalPrivateKeyParameters = (ElGamalPrivateKeyParameters)privKey.Key;
            int size = (elGamalPrivateKeyParameters.Parameters.P.BitLength + 7) / 8;
            ProcessEncodedMpi(keyCipher, size, encSessionKey[0]);
            ProcessEncodedMpi(keyCipher, size, encSessionKey[1]);
        }
        try
        {
            return(keyCipher.DoFinal());
        }
        catch (Exception exception2)
        {
            throw new PgpException("exception decrypting secret key", exception2);
        }
    }
Пример #2
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("unknown asymmetric algorithm: " + pubKey.Algorithm);
                }
                AsymmetricKeyParameter key = pubKey.GetKey();
                cipher.Init(forEncryption: true, new ParametersWithRandom(key, 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 parameters = new KeyParameter(Rfc6637Utilities.CreateKey(pubKey.PublicKeyPacket, s));
            IWrapper     wrapper    = PgpUtilities.CreateWrapper(eCDHPublicBcpgKey.SymmetricKeyAlgorithm);

            wrapper.Init(forWrapping: true, new ParametersWithRandom(parameters, 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];
            Array.Copy(encoded, 0, array3, 0, encoded.Length);
            array3[encoded.Length] = (byte)array2.Length;
            Array.Copy(array2, 0, array3, encoded.Length + 1, array2.Length);
            return(array3);
        }