存档

文章标签 ‘GnuPG’

在C#中使用GnuPG加密文件

2010年1月4日

  PGP可以解释为Pretty Good Privacy(汉语翻译:相当好的隐私),是PGP公司的加密和/或签名工具套件,使用了有商业版权的IDEA算法并集成了有商业版权的PGPdisk工具,有别于开源的GPG(GnuPG)。PGP的主要开发者为菲利普·齐默曼(Philip R. Zimmermann)。齐默曼在志愿者的帮助下,突破政府的禁止,于1991年将PGP在因特网上免费发布。

  PGP是一个桌面工具,在程序中使用相当不便。好在我们可以使用开源的GPG(GnuPG)在程序代码中写成文件加密。详见如下代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //EncryptPGP();
            Stopwatch sw = new Stopwatch();
            sw.Start();
            DecryptPGP();
            sw.Stop();
            Console.WriteLine(sw.Elapsed);

            Console.WriteLine("OK");
            Console.Read();
        }
        public static void EncryptPGP()
        {
            ProcessStartInfo pInfo = new ProcessStartInfo();
            pInfo.FileName = @"E:\Test\GnuPGDotNet\GnuPG\gpg.exe";
            pInfo.Arguments = string.Format("  --homedir \"{0}\" --yes --always-trust
             --keyring \"{1}\"  --output \"{2}\" -r zhangwenjie  --encrypt \"{3}\"",
              @"E:\Test\GnuPGDotNet\GnuPG", @"F:\Test\pubring.pkr",
               @"F:\Test\License.txt.pgp", @"F:\Test\License.txt  ");
            pInfo.CreateNoWindow = true;
            pInfo.UseShellExecute = false;

            pInfo.RedirectStandardInput = true;
            pInfo.RedirectStandardOutput = true;
            pInfo.RedirectStandardError = true;
            Process p = Process.Start(pInfo);

            string strInfo;
            while ((strInfo = p.StandardOutput.ReadLine()) != null)
            {
                Console.WriteLine(strInfo);
            }

            p.WaitForExit();

            if (p.ExitCode == 0)
            {
                Console.WriteLine("成功");
            }
            p.Close();

        }
        public static void DecryptPGP()
        {

            ProcessStartInfo pInfo = new ProcessStartInfo();
            pInfo.FileName = @"E:\Test\GnuPGDotNet\GnuPG\gpg.exe";
            pInfo.Arguments = string.Format("  --homedir \"{0}\" --keyring \"{1}\"
           --secret-keyring \"{2}\" --passphrase-fd 0 --output \"{3}\" --decrypt \"{4}\"",
             @"E:\Test\GnuPGDotNet\GnuPG", @"F:\Test\pubring.pkr",
             @"F:\Test\secring.skr", @"F:\Test\Oracle 9i.rar", @"F:\Test\Oracle 9i.rar.pgp");
            pInfo.CreateNoWindow = true;
            pInfo.UseShellExecute = false;
            pInfo.RedirectStandardInput = true;
            pInfo.RedirectStandardOutput = true;
            pInfo.RedirectStandardError = true;
            Process p = Process.Start(pInfo);

            p.StandardInput.WriteLine("123456789");
            p.StandardInput.Flush();
            p.StandardInput.Close();

            string strInfo;
            while ((strInfo = p.StandardOutput.ReadLine()) != null)
            {
                Console.WriteLine(strInfo);
            }

            p.WaitForExit();

            if (p.ExitCode == 0)
            {
                Console.WriteLine("成功");
            }
            p.Close();
        }
    }

}

   其中:pubring.pkr和secring.skr是两个预先生成好的key文件。p.StandardInput.WriteLine(“123456789″);用来输入解密密码的。

  希望本文对希望在C#中使用PGP的朋友的帮助。

  本文首发地址张文杰博客:http://zhangwenjie.net(转载请保留)

Windows , , , , ,