Example #1
0
        public void TestVerifyFailInvalidSignature()
        {
            Gpg g = new Gpg(GPG_BINARY_PATH);

            g.Passphrase = GPG_PASSWORD;

            byte[]       data = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04 };
            MemoryStream tbs  = new MemoryStream(data);
            MemoryStream sig  = new MemoryStream();

            g.Sign(tbs, sig);

            Assert.IsTrue(sig.Length > 0);

            // tweak a few bytes on the sig to invalidate it
            sig.Position = sig.Length / 2 - 1;
            sig.WriteByte(0xFF);
            sig.WriteByte(0xFF);
            sig.WriteByte(0xFF);

            // set the postion in the signature stream to start reading from at the beginning
            sig.Position = 0;

            Assert.Throws <GpgException>(() => g.Verify(sig));
        }
Example #2
0
 /// <summary>
 /// Clean up any resources being used.
 /// </summary>
 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
 protected override void Dispose(bool disposing)
 {
     if (disposing && (components != null))
     {
         components.Dispose();
     }
     if (disposing)
     {
         if (Gmo != null)
         {
             Gmo.Dispose();
         }
         if (Gpg != null)
         {
             Gpg.Dispose();
         }
         if (Gopt != null)
         {
             Gopt.Dispose();
         }
         if (_grid != null)
         {
             _grid.Dispose();
         }
     }
     base.Dispose(disposing);
 }
Example #3
0
        /// <summary>
        /// Generates an ecrypted password file at the specified path.
        /// If the path contains directories that do not exist, they will be created automatically.
        /// </summary>
        /// <param name="text">The text to be encrypted.</param>
        /// <param name="path">A relative path specifying where in the password store the password file should be generated.</param>
        public void EncryptText(string text, string path)
        {
            var fullPath = GetPasswordFilePath(path);

            Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
            Gpg.Encrypt(text, fullPath, GetGpgIds(fullPath));
        }
Example #4
0
        /// <summary>
        /// Decrypt a file. The encrypted file should have a .gpg extension, which
        /// is removed to produce the path to the decrypted file.
        /// </summary>
        /// <param name="path">The path, relative to the password store, to the encrypted file.</param>
        /// <returns>An absolute path pointing to the decrypted file.</returns>
        public string DecryptFile(string path)
        {
            if (!path.EndsWith(EncryptedFileExtension))
            {
                throw new ArgumentException($"The encrypted file \"{path}\" should have a filename ending with {EncryptedFileExtension}");
            }

            var encryptedFileName = GetPasswordFilePath(path);
            var decryptedFileName = encryptedFileName.Substring(0, encryptedFileName.Length - EncryptedFileExtension.Length);

            if (File.Exists(decryptedFileName))
            {
                throw new InvalidOperationException($"A plaintext file already exists at \"{decryptedFileName}\".");
            }
            if (!File.Exists(encryptedFileName))
            {
                throw new ArgumentException($"The encrypted file \"{encryptedFileName}\" does not exist.");
            }

            if (PinentryFixEnabled)
            {
                pinentryWatcher.BumpPinentryWindow();
            }
            Gpg.DecryptToFile(encryptedFileName, decryptedFileName);
            return(decryptedFileName);
        }
Example #5
0
        static void Main(string[] args)
        {
            string keyId  = "416AB815";
            string passwd = "abc123!";

            byte[]       byteArray = Encoding.ASCII.GetBytes("Some text");
            MemoryStream input     = new MemoryStream(byteArray);
            MemoryStream output    = new MemoryStream();
            Gpg          gpg       = new Gpg("C:\\Program Files (x86)\\GNU\\GnuPG\\pub\\gpg.exe");

            gpg.OutputType          = OutputTypes.AsciiArmor;
            gpg.Passphrase          = passwd;
            gpg.LocalUser           = keyId;
            gpg.OutputSignatureType = OutputSignatureTypes.ClearText;
            gpg.Sign(input, output);
            output.Position = 0;
            StreamReader reader      = new StreamReader(output);
            string       text        = reader.ReadToEnd();
            MemoryStream outputVer   = new MemoryStream();
            var          verifiedKey = gpg.Verify(output, outputVer);

            outputVer.Position = 0;
            StreamReader reader2 = new StreamReader(outputVer);
            string       text2   = reader2.ReadToEnd();

            Console.WriteLine("Found key " + verifiedKey);
            Console.WriteLine("Output: " + text2);
            Console.ReadKey();
        }
Example #6
0
        public void TestGetKeys()
        {
            Gpg g = new Gpg(GPG_BINARY_PATH);
            GpgKeyCollection col = g.GetKeys();

            Debug.WriteLine("count: " + col.Count.ToString());

            foreach (GpgKey k in col)
            {
                PrintKey(k);
            }
        }
Example #7
0
        public void TestImportKey()
        {
            Gpg g = new Gpg(GPG_BINARY_PATH);

            string testKey = "-----BEGIN PGP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0rX0tn2bnY9RzemBDCc92o7r\r\nyCRqgsTalflIT1TSoP96d3N0CgYvpLd4exSubYAXztGBTu8tozT7/SE8aYK/ao0A\r\nGG72BGPpnwRHNgiBDwFaScLO10hDoeL+fVPQFl/GFg49By9GnM2wTP6G6UqIfcFo\r\nIUTUvmNyxXoxu7Ex85fCCcOXMrWTj5Vyx6z3HHb+Z+MVXgQxXphhABEBAAGJATYE\r\nGAEIACAWIQRQqHmapKYL4iMZN8sT8cK7WOeUCwUCWqQ17gIbDAAKCRAT8cK7WOeU\r\nC+A2B/wLOJD45hyTsvUPR67/j46y/vNvT0oQuzzYGsoFsq5N6eCruHTZsv6eqgsH\r\nEeVHb8kjj437C1aFHN5/evO2fibNw7osHstFevj48YJ0n3TdYEklaSXIQMTpZbLD\r\nOcbvcmzC8r9D+2syCecgTRYcuCouJTJyQsI7JdAxeJInycKI5lvR4+O1SQFz9m/3\r\nyF9WqAtR0AXnoQjS2Y82lAKdaa52IIuiRUjJyP9YVRPTcH6LlyoRp5OXAQzreFGa\r\nP+CnwGYC2WV1hQ7qxpI2fAJ4/DquVgbuX85bLf+rbC7eP+4xti0tSRm9yVlB7qsY\r\nwcLNxOrwIKeLtws5Vmv5XNB7E3Fc\r\n=BllK\r\n-----END PGP PUBLIC KEY BLOCK-----\r\n";

            byte[]       testKeyBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(testKey);
            MemoryStream key          = new MemoryStream(testKeyBytes);
            string       result       = g.Import(key);

            Debug.WriteLine(result);
        }
Example #8
0
        public void TestVerifyFailInvalidData()
        {
            Gpg g = new Gpg(GPG_BINARY_PATH);

            g.LocalUser  = GPG_LOCAL_USER_KEY;
            g.Passphrase = GPG_PASSWORD;

            byte[]       data = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04 };
            MemoryStream sig  = new MemoryStream(data);

            Assert.Throws <GpgException>(() => g.Verify(sig));
        }
Example #9
0
        public void TestImportKey()
        {
            Gpg g = new Gpg(GPG_BINARY_PATH);

            string testK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te[]       testKeyBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(testKey);
            MemoryStream key          = new MemoryStream(testKeyBytes);
            string       result       = g.Import(key);

            Debug.WriteLine(result);
        }
Example #10
0
        /// <summary>
        /// Encrypt a file. The path to the unencrypted file (plus a .gpg extension)
        /// is used to produce the path to the encrypted file.
        /// </summary>
        /// <param name="file">A relative path pointing to the encrypted file in the password store.</param>
        public string EncryptFile(string file)
        {
            var fullFilePath = GetPasswordFilePath(file);

            if (!File.Exists(fullFilePath))
            {
                throw new ArgumentException($"The unencrypted file \"{fullFilePath}\" does not exist.");
            }
            Gpg.EncryptFile(fullFilePath, fullFilePath + EncryptedFileExtension, GetGpgIds(file));

            return(fullFilePath + EncryptedFileExtension);
        }
Example #11
0
        public string DecryptText(string path)
        {
            var fullPath = GetPasswordFilePath(path);

            if (!File.Exists(fullPath))
            {
                throw new ArgumentException($"The password file \"{fullPath}\" does not exist.");
            }

            if (PinentryFixEnabled)
            {
                pinentryWatcher.BumpPinentryWindow();
            }
            return(Gpg.Decrypt(fullPath));
        }
Example #12
0
        public void TestSign(OutputSignatureTypes sigType)
        {
            Gpg g = new Gpg(GPG_BINARY_PATH);

            g.LocalUser           = GPG_LOCAL_USER_KEY;
            g.Passphrase          = GPG_PASSWORD;
            g.OutputSignatureType = sigType;

            byte[]       data = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04 };
            MemoryStream tbs  = new MemoryStream(data);
            MemoryStream sig  = new MemoryStream();

            g.Sign(tbs, sig);

            Assert.IsTrue(sig.Length > 0);
        }
Example #13
0
        public void TestSignVerify()
        {
            Gpg g = new Gpg(GPG_BINARY_PATH);

            g.LocalUser  = GPG_LOCAL_USER_KEY;
            g.Passphrase = GPG_PASSWORD;

            byte[]       data = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04 };
            MemoryStream tbs  = new MemoryStream(data);
            MemoryStream sig  = new MemoryStream();

            g.Sign(tbs, sig);

            Assert.IsTrue(sig.Length > 0);

            // set the postion in the signature stream to start reading from at the beginning
            sig.Position = 0;

            g.Verify(sig);
        }
Example #14
0
        public void TestEncryptDecrypt()
        {
            Gpg g = new Gpg(GPG_BINARY_PATH);

            g.LocalUser  = GPG_LOCAL_USER_KEY;
            g.Recipient  = GPG_RECIPIENT_KEY;
            g.Passphrase = GPG_PASSWORD;

            byte[]       data       = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04 };
            MemoryStream cleartext  = new MemoryStream(data);
            MemoryStream ciphertext = new MemoryStream();

            g.Encrypt(cleartext, ciphertext);

            ciphertext.Position = 0;
            MemoryStream cleartext2 = new MemoryStream();

            g.Decrypt(ciphertext, cleartext2);

            assertEqual(cleartext, cleartext2);
        }
Example #15
0
 private static byte[] DecryptKey(KeyProviderQueryContext ctx)
 {
     return(Gpg.Decrypt(GetAuxFile(ctx)).GetBuffer());
 }