コード例 #1
0
ファイル: Decryptor.cs プロジェクト: cescopete/WebPGP
        private static PgpPrivateKey FindSecretKey(PgpSecretKeyRingBundle pgpSec, long keyId, char[] passPhrase)
        {
            PgpSecretKey pgpSecKey = pgpSec.GetSecretKey(keyId);

            if (pgpSecKey == null)
            {
                return(null);
            }
            return(pgpSecKey.ExtractPrivateKey(passPhrase));
        }
コード例 #2
0
        private PgpPrivateKey FindSecretKey(long keyId)
        {
            PgpSecretKey pgpSecKey = pgpSec.GetSecretKey(keyId);

            if (pgpSecKey == null)
            {
                return(null);
            }
            return(pgpSecKey.ExtractPrivateKey(password));
        }
コード例 #3
0
		/**
		 * Search a secret key ring collection for a secret key corresponding to keyID if it
		 * exists.
		 * 
		 * @param pgpSec a secret key ring collection.
		 * @param keyID keyID we want.
		 * @param pass passphrase to decrypt secret key with.
		 * @return
		 * @throws PGPException
		 * @throws NoSuchProviderException
		 */
		internal static PgpPrivateKey FindSecretKey(PgpSecretKeyRingBundle pgpSec, long keyID, char[] pass)
		{
			PgpSecretKey pgpSecKey = pgpSec.GetSecretKey(keyID);

			if (pgpSecKey == null)
			{
				return null;
			}

			return pgpSecKey.ExtractPrivateKey(pass);
		}
コード例 #4
0
ファイル: PGPEncryption.cs プロジェクト: markwuintw/Open-PGP
            /**
             * Search a secret key ring collection for a secret key corresponding to keyID if it
             * exists.
             *
             * @param pgpSec a secret key ring collection.
             * @param keyID keyID we want.
             * @param pass passphrase to decrypt secret key with.
             * @return
             * @throws PGPException
             * @throws NoSuchProviderException
             */
            internal static PgpPrivateKey FindSecretKey(PgpSecretKeyRingBundle pgpSec, long keyID, char[] pass)
            {
                PgpSecretKey pgpSecKey = pgpSec.GetSecretKey(keyID);

                if (pgpSecKey == null)
                {
                    return(null);
                }

                return(pgpSecKey.ExtractPrivateKey(pass));
            }
コード例 #5
0
        /// <summary>
        /// Search a secret key ring collection for a secret key corresponding to keyID if it exists.
        /// </summary>
        /// <param name="secretKeyRingBundle"></param>
        /// <param name="keyID"></param>
        /// <param name="passPhrase"></param>
        /// <returns></returns>
        /// <exception cref="PgpException"></exception>
        internal static PgpPrivateKey FindSecretKey(PgpSecretKeyRingBundle secretKeyRingBundle, long keyId, char[] passPhrase)
        {
            PgpSecretKey _pgpSecretKey = secretKeyRingBundle.GetSecretKey(keyId);

            if (_pgpSecretKey == null)
            {
                return(null);
            }

            return(_pgpSecretKey.ExtractPrivateKey(passPhrase));
        }
コード例 #6
0
        public static PgpPrivateKey FindSecretKeybyKeyId(PgpSecretKeyRingBundle pgpSec, string keyID, char[] pass)
        {
            var pgpSecKey = pgpSec.GetSecretKey(System.Convert.ToInt64(keyID, 16));

            if (pgpSecKey == null)
            {
                return(null);
            }

            return(pgpSecKey.ExtractPrivateKey(pass));
        }
コード例 #7
0
        private static PgpPrivateKey ExtractSecretKey(PgpSecretKeyRingBundle pgpSec, long keyId, char[] pass)
        {
            var pgpSecKey = pgpSec.GetSecretKey(keyId);

            if (pgpSecKey == null)
            {
                return(null);
            }

            return(pgpSecKey.ExtractPrivateKey(pass));
        }
コード例 #8
0
        /**
         * Search a secret key ring collection for a secret key corresponding to keyID if it
         * exists.
         *
         * @param pgpSec a secret key ring collection.
         * @param keyID keyID we want.
         * @param pass passphrase to decrypt secret key with.
         * @return
         * @throws PGPException
         * @throws NoSuchProviderException
         */
        public static PgpPrivateKey FindSecretKeybyKeyId(PgpSecretKeyRingBundle pgpSec, long keyID, char[] pass)
        {
            var pgpSecKey = pgpSec.GetSecretKey(keyID);

            if (pgpSecKey == null)
            {
                return(null);
            }

            return(pgpSecKey.ExtractPrivateKey(pass));
        }
コード例 #9
0
        private PgpPrivateKey FindKeyById(PgpSecretKeyRingBundle privRings, long keyId)
        {
            PgpSecretKey pgpSecKey = privRings.GetSecretKey(keyId);

            if (pgpSecKey == null)
            {
                return(null);
            }

            return(pgpSecKey.ExtractPrivateKey(null));
        }
コード例 #10
0
        private static PgpPrivateKey FindSecretKey(Stream keyIn, long keyId, char[] pass)
        {
            PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(
                PgpUtilities.GetDecoderStream(keyIn));

            PgpSecretKey pgpSecKey = pgpSec.GetSecretKey(keyId);

            if (pgpSecKey == null)
            {
                return(null);
            }

            return(pgpSecKey.ExtractPrivateKey(pass));
        }
コード例 #11
0
        private static PgpPrivateKey FindSecretKey(
            PgpSecretKeyRingBundle secretRingBundle,
            long keyId,
            char[] pass)
        {
            PgpSecretKey pgpSecKey = secretRingBundle.GetSecretKey(keyId);

            if (pgpSecKey == null)
            {
                return(null);
            }

            return(pgpSecKey.ExtractPrivateKey(pass));
        }
コード例 #12
0
        /// <summary>
        /// private method to get the secret key value.
        /// </summary>
        /// <param name="_privKeyPath">string representing the private key path</param>
        /// <returns>a PGPSecretKey</returns>
        private PgpSecretKey getSecretKey(string _privKeyPath)
        {
            PgpSecretKey secKey;

            using (Stream keyin = File.OpenRead(_privKeyPath))
                using (Stream s = PgpUtilities.GetDecoderStream(keyin))
                {
                    PgpSecretKeyRingBundle secKeyBundle = new PgpSecretKeyRingBundle(s);
                    secKey = secKeyBundle.GetSecretKey(_keyID);
                    if (secKey == null)
                    {
                        throw new Exception("The secret key value is null!");
                    }
                }
            return(secKey);
        }
コード例 #13
0
        public void SignAndEncryptFile(string strActualFileName, string strEmbeddedFileName,
                                       System.IO.Stream strmKeyIn, long lngKeyId, System.IO.Stream strmOutputStream,
                                       char[] szPassword, bool bArmor, bool bWithIntegrityCheck, PgpPublicKey PGP_PublicKey)
        {
            const int iBUFFER_SIZE = 1 << 16; // should always be power of 2

            if (bArmor)
            {
                strmOutputStream = new ArmoredOutputStream(strmOutputStream);
            }

            // Init encrypted data generator
            PgpEncryptedDataGenerator PGP_EncryptedDataGenerator = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Cast5, bWithIntegrityCheck, new SecureRandom());

            PGP_EncryptedDataGenerator.AddMethod(PGP_PublicKey);
            System.IO.Stream strmEncryptedOut = PGP_EncryptedDataGenerator.Open(strmOutputStream, new byte[iBUFFER_SIZE]);

            // Init compression
            PgpCompressedDataGenerator PGP_CompressedDataGenerator = new PgpCompressedDataGenerator(CompressionAlgorithmTag.Zip);

            System.IO.Stream strmCompressedOut = PGP_CompressedDataGenerator.Open(strmEncryptedOut);

            // Init signature
            PgpSecretKeyRingBundle PGP_SecretKeyBundle = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(strmKeyIn));
            PgpSecretKey           PGP_SecretKey       = PGP_SecretKeyBundle.GetSecretKey(lngKeyId);

            if (PGP_SecretKey == null)
            {
                throw new System.ArgumentException(lngKeyId.ToString("X") + " could not be found in specified key ring bundle.", "keyId");
            }

            PgpPrivateKey         PGP_PrivateKey         = PGP_SecretKey.ExtractPrivateKey(szPassword);
            PgpSignatureGenerator PGP_SignatureGenerator = new PgpSignatureGenerator(PGP_SecretKey.PublicKey.Algorithm, HashAlgorithmTag.Sha1);

            PGP_SignatureGenerator.InitSign(PgpSignature.BinaryDocument, PGP_PrivateKey);

            foreach (string strUserId in PGP_SecretKey.PublicKey.GetUserIds())
            {
                PgpSignatureSubpacketGenerator PGP_SignatureSubpacketGenerator = new PgpSignatureSubpacketGenerator();
                PGP_SignatureSubpacketGenerator.SetSignerUserId(false, strUserId);
                PGP_SignatureGenerator.SetHashedSubpackets(PGP_SignatureSubpacketGenerator.Generate());
                // Just the first one!
                break;
            }
            PGP_SignatureGenerator.GenerateOnePassVersion(false).Encode(strmCompressedOut);

            // Create the Literal Data generator output stream
            PgpLiteralDataGenerator PGP_LiteralDataGenerator = new PgpLiteralDataGenerator();

            System.IO.FileInfo fiEmbeddedFile = new System.IO.FileInfo(strEmbeddedFileName);
            System.IO.FileInfo fiActualFile   = new System.IO.FileInfo(strActualFileName);
            // TODO: Use lastwritetime from source file
            System.IO.Stream strmLiteralOut = PGP_LiteralDataGenerator.Open(strmCompressedOut, PgpLiteralData.Binary,
                                                                            fiEmbeddedFile.Name, fiActualFile.LastWriteTime, new byte[iBUFFER_SIZE]);

            // Open the input file
            System.IO.FileStream strmInputStream = fiActualFile.OpenRead();

            byte[] baBuffer = new byte[iBUFFER_SIZE];
            int    iReadLength;

            while ((iReadLength = strmInputStream.Read(baBuffer, 0, baBuffer.Length)) > 0)
            {
                strmLiteralOut.Write(baBuffer, 0, iReadLength);
                PGP_SignatureGenerator.Update(baBuffer, 0, iReadLength);
            }

            strmLiteralOut.Close();
            PGP_LiteralDataGenerator.Close();
            PGP_SignatureGenerator.Generate().Encode(strmCompressedOut);
            strmCompressedOut.Close();
            PGP_CompressedDataGenerator.Close();
            strmEncryptedOut.Close();
            PGP_EncryptedDataGenerator.Close();
            strmInputStream.Close();

            if (bArmor)
            {
                strmOutputStream.Close();
            }
        }
コード例 #14
0
        public void Decrypt(string inputFile, string outputFile)
        {
            try
            {
                PgpObjectFactory          pgpF   = null;
                PgpEncryptedDataList      enc    = null;
                PgpObject                 o      = null;
                PgpPublicKeyEncryptedData pbe    = null;
                PgpSecretKeyRingBundle    pgpSec = null;

                pgpF = new PgpObjectFactory(PgpUtilities.GetDecoderStream(File.OpenRead(inputFile)));
                // find secret key
                pgpSec = new PgpSecretKeyRingBundle(_encriptionKeys.SecretKey.GetEncoded());

                if (pgpF != null)
                {
                    o = pgpF.NextPgpObject();
                }

                // the first object might be a PGP marker packet.
                if (o is PgpEncryptedDataList)
                {
                    enc = (PgpEncryptedDataList)o;
                }
                else
                {
                    enc = (PgpEncryptedDataList)pgpF.NextPgpObject();
                }

                foreach (PgpPublicKeyEncryptedData pked in enc.GetEncryptedDataObjects())
                {
                    var key = pgpSec.GetSecretKey(pked.KeyId);

                    if (key != null)
                    {
                        pbe = pked;
                        break;
                    }
                }

                PgpObjectFactory plainFact = null;

                using (Stream clear = pbe.GetDataStream(_encriptionKeys.PrivateKey))
                {
                    plainFact = new PgpObjectFactory(clear);
                }

                PgpObject message = plainFact.NextPgpObject();

                if (message is PgpCompressedData)
                {
                    PgpCompressedData cData = (PgpCompressedData)message;
                    PgpObjectFactory  of    = null;

                    using (Stream compDataIn = cData.GetDataStream())
                    {
                        of = new PgpObjectFactory(compDataIn);
                    }

                    message = of.NextPgpObject();
                    if (message is PgpOnePassSignatureList)
                    {
                        message = of.NextPgpObject();
                        PgpLiteralData Ld = null;
                        Ld = (PgpLiteralData)message;
                        using (Stream output = File.Create(outputFile))
                        {
                            Stream unc = Ld.GetInputStream();
                            Streams.PipeAll(unc, output);
                        }
                    }
                    else
                    {
                        PgpLiteralData Ld = null;
                        Ld = (PgpLiteralData)message;
                        using (Stream output = File.Create(outputFile))
                        {
                            Stream unc = Ld.GetInputStream();
                            Streams.PipeAll(unc, output);
                        }
                    }
                }
                else if (message is PgpLiteralData)
                {
                    PgpLiteralData ld          = (PgpLiteralData)message;
                    string         outFileName = ld.FileName;

                    using (Stream fOut = File.Create(outputFile))
                    {
                        Stream unc = ld.GetInputStream();
                        Streams.PipeAll(unc, fOut);
                    }
                }
                else if (message is PgpOnePassSignatureList)
                {
                    throw new PgpException("Encrypted message contains a signed message - not literal data.");
                }
                else
                {
                    throw new PgpException("Message is not a simple encrypted file - type unknown.");
                }
            }
            catch (PgpException ex)
            {
                throw ex;
            }
        }
コード例 #15
0
        /// <summary>
        /// It doesn't get any simpler than this.  Toss a PGP encrypted string in and get the unencrypted string out.
        /// Make sure they PrivateKey and Password (If you have one) are populated
        /// </summary>
        /// <param name="encryptedString">Your PGP Encrypted String</param>
        /// <returns>Unencrypted String</returns>
        //public static string DecryptString(string encryptedString)
        //{
        //    if(string.IsNullOrWhiteSpace(encryptedString))
        //        throw new ArgumentNullException(nameof(encryptedString));

        //    using (var stream = PSS_PGPEncrypt.StringToStream(encryptedString))
        //    {
        //        return PSS_PGPEncrypt.StreamToString(DecryptStream(stream));
        //    }

        //}

        private static PgpPrivateKey FindSecretKey(PgpSecretKeyRingBundle pgpSec, long keyID, char[] pass)
        {
            var pgpSecKey = pgpSec.GetSecretKey(keyID);

            return(pgpSecKey?.ExtractPrivateKey(pass));
        }
コード例 #16
0
        private PgpPrivateKey FindKeyById(PgpSecretKeyRingBundle privRings, long keyId)
        {
            var pgpSecKey = privRings.GetSecretKey(keyId);

            return(pgpSecKey?.ExtractPrivateKey(null));
        }
コード例 #17
0
ファイル: PgpDecryptor.cs プロジェクト: iarovyi/e-crypt
        private static PgpPrivateKey FindSecretKeyByKeyId(PgpSecretKeyRingBundle secretKeyRing, long keyId, char[] passPhrase)
        {
            PgpSecretKey pgpSecKey = secretKeyRing.GetSecretKey(keyId);

            return(pgpSecKey == null ? null : pgpSecKey.ExtractPrivateKey(passPhrase));
        }
コード例 #18
0
ファイル: Program.cs プロジェクト: nashokin/BCCrypto
        private static void DecryptPgp(Stream input, Stream output, Stream key, char[] password)
        {
            try
            {
                PgpObjectFactory pgpObjectFactory = new PgpObjectFactory(PgpUtilities.GetDecoderStream(input));
                PgpObject        pgpObject        = pgpObjectFactory.NextPgpObject();

                // The first object might be a PGP marker packet
                PgpEncryptedDataList pgpEncryptedDataList;
                if (pgpObject is PgpEncryptedDataList)
                {
                    pgpEncryptedDataList = (PgpEncryptedDataList)pgpObject;
                }
                else
                {
                    pgpEncryptedDataList = (PgpEncryptedDataList)pgpObjectFactory.NextPgpObject();
                }

                // Find private key for decryption
                PgpPrivateKey             privateKey                = null;
                PgpSecretKeyRingBundle    pgpSecretKeyRing          = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(key));
                PgpPublicKeyEncryptedData pgpPublicKeyEncryptedData = null;
                foreach (PgpPublicKeyEncryptedData pked in pgpEncryptedDataList.GetEncryptedDataObjects())
                {
                    PgpSecretKey pgpDescretKey = pgpSecretKeyRing.GetSecretKey(pked.KeyId);
                    privateKey = pgpDescretKey.ExtractPrivateKey(password);

                    if (privateKey != null)
                    {
                        pgpPublicKeyEncryptedData = pked;
                        break;
                    }
                }

                if (privateKey == null)
                {
                    throw new ArgumentException("Private key for decryption not found.");
                }

                Stream decrypted = pgpPublicKeyEncryptedData.GetDataStream(privateKey);
                pgpObjectFactory = new PgpObjectFactory(decrypted);
                pgpObject        = pgpObjectFactory.NextPgpObject();

                if (pgpObject is PgpCompressedData)
                {
                    PgpCompressedData pgpCompressedData = (PgpCompressedData)pgpObject;
                    pgpObjectFactory = new PgpObjectFactory(pgpCompressedData.GetDataStream());
                    pgpObject        = pgpObjectFactory.NextPgpObject();
                }

                if (pgpObject is PgpLiteralData)
                {
                    PgpLiteralData pgpLiteralData = (PgpLiteralData)pgpObject;
                    Stream         literal        = pgpLiteralData.GetInputStream();
                    Streams.PipeAll(literal, output);
                }
                else if (pgpObject is PgpOnePassSignatureList)
                {
                    throw new PgpException("Encrypted message contains a signed message, not a literal data.");
                }
                else
                {
                    throw new PgpException("Message is not a simple encrypted file, type is unknown.");
                }

                if (pgpPublicKeyEncryptedData.IsIntegrityProtected())
                {
                    if (!pgpPublicKeyEncryptedData.Verify())
                    {
                        Console.Error.WriteLine("Message failed integrity check.");
                    }
                    else
                    {
                        Console.Error.WriteLine("Message integrity check passed.");
                    }
                }
                else
                {
                    Console.Error.WriteLine("No message integrity check.");
                }

                Console.WriteLine("OpenPGP decryption successfull.");
            }
            catch (PgpException ex)
            {
                Console.Error.WriteLine(ex);

                Exception pgpInnerException = ex.InnerException;
                if (pgpInnerException != null)
                {
                    Console.Error.WriteLine(pgpInnerException.Message);
                    Console.Error.WriteLine(pgpInnerException.StackTrace);
                }
            }
        }
コード例 #19
0
 /// <summary>
 /// Search a secret keyring collection for a secret key corresponding to
 /// key identifier if it exists.
 /// </summary>
 /// <param name="secretKeyring">
 /// The PGP secret keyring bundle.
 /// </param>
 /// <param name="keyId">
 /// The key identifier to search for.
 /// </param>
 /// <param name="pass">
 /// The passphrase to use for accessing the secret keyring.
 /// </param>
 /// <returns>
 /// The found <see cref="PgpPrivateKey"/>; null otherwise.
 /// </returns>
 public static PgpPrivateKey FindSecretKey(PgpSecretKeyRingBundle secretKeyring, long keyId, char[] pass)
 {
     return(secretKeyring?.GetSecretKey(keyId)?.ExtractPrivateKey(pass));
 }
コード例 #20
0
ファイル: Utilities.cs プロジェクト: pefthymiou/PGPConsoleApp
        internal static PgpPrivateKey FindSecretKey(PgpSecretKeyRingBundle pgpSecretKey, long keyId, char[] pass)
        {
            PgpSecretKey pgpSecKey = pgpSecretKey.GetSecretKey(keyId);

            return(pgpSecKey?.ExtractPrivateKey(pass));
        }