Example #1
0
 private void Unencrypt(string EncryptedFile)
 {
     PgpHelper.DecryptFile(EncryptedFile, GetSecretKeyRingBundle(), "7CvhR4hGsLWNpZ3JQ4qwYH182wAPTnxBJGtfT+CfXzw=");
     PgpSecretKeyRingBundle GetSecretKeyRingBundle()
     {
         using (Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream("CapStoneTestForm.secring.skr")){
             return(new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(s)));
         }
     }
 }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PgpFileDecryptingStream"/> class.
        /// </summary>
        /// <param name="inputStream">The encrypted input stream.</param>
        /// <param name="secretKeyRingBundle">The secret key ring bundle.</param>
        /// <param name="passPhrase">The pass phrase.</param>
        /// <exception cref="System.ArgumentException">Secret key for message not found.</exception>
        /// <exception cref="PgpException">
        /// Encrypted message contains a signed message - not literal data.
        /// or
        /// Message is not a simple encrypted file - type unknown.
        /// </exception>
        public PgpFileDecryptingStream(
            Stream inputStream,
            PgpSecretKeyRingBundle secretKeyRingBundle,
            string passPhrase)
        {
            inputStream = PgpUtilities.GetDecoderStream(inputStream);

            PgpObjectFactory encryptedObjectFactory = new PgpObjectFactory(inputStream);

            // the first object might be a PGP marker packet.
            PgpEncryptedDataList encryptedList = encryptedObjectFactory.NextPgpObject() as PgpEncryptedDataList;

            if (encryptedList == null)
            {
                encryptedList = (PgpEncryptedDataList)encryptedObjectFactory.NextPgpObject();
            }

            // decrypt
            PgpPrivateKey             privateKey    = null;
            PgpPublicKeyEncryptedData encryptedData = null;

            foreach (PgpPublicKeyEncryptedData pked in encryptedList.GetEncryptedDataObjects())
            {
                privateKey = PgpHelper.FindSecretKey(secretKeyRingBundle, pked.KeyId, passPhrase.ToCharArray());

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

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

            PgpObjectFactory decryptedObjectFactory = null;

            using (Stream decryptedStream = encryptedData.GetDataStream(privateKey))
            {
                decryptedObjectFactory = new PgpObjectFactory(decryptedStream);
            }

            PgpObject message = decryptedObjectFactory.NextPgpObject();

            if (message is PgpCompressedData)
            {
                PgpCompressedData compressedData = (PgpCompressedData)message;

                PgpObjectFactory decompressedObjectFactory = null;
                using (Stream decompressedStream = compressedData.GetDataStream())
                {
                    decompressedObjectFactory = new PgpObjectFactory(decompressedStream);
                }

                message = decompressedObjectFactory.NextPgpObject();
                if (message is PgpOnePassSignatureList)
                {
                    message = decompressedObjectFactory.NextPgpObject();
                }
            }

            if (message is PgpOnePassSignatureList)
            {
                throw new PgpException("Encrypted message contains a signed message - not literal data.");
            }
            else if (!(message is PgpLiteralData))
            {
                throw new PgpException("Message is not a simple encrypted file - type unknown.");
            }

            PgpLiteralData ld = (PgpLiteralData)message;

            this.WrappedFileName         = ld.FileName;
            this.WrappedFileModifiedTime = ld.ModificationTime;
            this.wrappedStream           = ld.GetInputStream();
        }