PGP 免费软件和 c#

发布于 2024-07-29 05:29:59 字数 51 浏览 6 评论 0原文

如何使用 C# 和命令行使用添加到当前密钥环的公钥加密文件?

谢谢。

How to encrypt file with public key, that was added to current keyring, with using c# and command line?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仅冇旳回忆 2024-08-05 05:29:59

那么,C# 的 bouncy castle 加密库可用于加密和解密 PGP; 这是我们内部用于 PGP 加密工作的内容。 它是完全独立的,因此不依赖于现有程序或库。

但请记住,它是一个库而不是命令行程序。 源代码附带了一些示例命令行程序实用程序,如果您需要使用 CLI,您可以构建它们。

Well, the bouncy castle cryptography library for C# can be used to encrypt and decrypt PGP; it's what we use internally for PGP crypto work. It's entirely self-contained, so no dependencies on existing programs or libraries.

Keep in mind, however, that it's a library not a command line program. The source code comes with some example command line program utilities you can build if you need to use it CLI.

养猫人 2024-08-05 05:29:59

您可以使用我一年前编写的实现。 我知道这有点难看。

您需要 pgp.exe 和一些有关它的背景知识。 请参阅我的博客文章。

/// <summary>
        /// Encryps given file using PGP Public Key
        /// </summary>
        /// <param name="filename"></param>
        public string Encrypt(string filename, bool isBinary, ref string outstr){

            string outputfilename = filename;


            //We use stringbuilder for performance considerations
            StringBuilder sb = new StringBuilder();
            sb.Append("/c ");
            sb.Append("");
            sb.Append(PGPLocation);
            sb.Append(" +force -es ");
            sb.Append("\"");
            sb.Append(filename);
            sb.Append("\" ");
            sb.Append(ToUserName);
            sb.Append(" -u ");
            sb.Append(MyUserName);

            sb.Append(" -z ");
            sb.Append(PassPhrase);
            sb.Append(" ");

            // Use binary indicator because PGP produces different outputs for binary and plain text files
            if (isBinary)
                sb.Append("-a");

            proc.StartInfo.Arguments = sb.ToString();


            //proc.StartInfo.Arguments = "/c pgp +force -es "+filename+" cumacam -u bugra";


            proc.Start();
            if (WaitForInfinity)
                proc.WaitForExit();
            else
                proc.WaitForExit(WaitTime);
            //string res = proc.StandardOutput.ReadToEnd();

            outstr = proc.StartInfo.Arguments;
            if (proc.HasExited)
            {
                int ab = proc.ExitCode;
                if (ab != 0)
                {
                    FireError(Convert.ToInt32(ErrorTypes.PGPEncryptError), "Erro No: " + ab.ToString() + "in PGP. Details: "+" "+proc.StandardOutput.ReadToEnd());
                    return null;
                }
                else
                    if (!isBinary)
                        return outputfilename+".pgp";
                return outputfilename + ".asc";
            }

            return null;
        }

You can use my implementation i wrote one year ago. I know that it's a bit ugly.

You need pgp.exe and some background about it. see my blog post.

/// <summary>
        /// Encryps given file using PGP Public Key
        /// </summary>
        /// <param name="filename"></param>
        public string Encrypt(string filename, bool isBinary, ref string outstr){

            string outputfilename = filename;


            //We use stringbuilder for performance considerations
            StringBuilder sb = new StringBuilder();
            sb.Append("/c ");
            sb.Append("");
            sb.Append(PGPLocation);
            sb.Append(" +force -es ");
            sb.Append("\"");
            sb.Append(filename);
            sb.Append("\" ");
            sb.Append(ToUserName);
            sb.Append(" -u ");
            sb.Append(MyUserName);

            sb.Append(" -z ");
            sb.Append(PassPhrase);
            sb.Append(" ");

            // Use binary indicator because PGP produces different outputs for binary and plain text files
            if (isBinary)
                sb.Append("-a");

            proc.StartInfo.Arguments = sb.ToString();


            //proc.StartInfo.Arguments = "/c pgp +force -es "+filename+" cumacam -u bugra";


            proc.Start();
            if (WaitForInfinity)
                proc.WaitForExit();
            else
                proc.WaitForExit(WaitTime);
            //string res = proc.StandardOutput.ReadToEnd();

            outstr = proc.StartInfo.Arguments;
            if (proc.HasExited)
            {
                int ab = proc.ExitCode;
                if (ab != 0)
                {
                    FireError(Convert.ToInt32(ErrorTypes.PGPEncryptError), "Erro No: " + ab.ToString() + "in PGP. Details: "+" "+proc.StandardOutput.ReadToEnd());
                    return null;
                }
                else
                    if (!isBinary)
                        return outputfilename+".pgp";
                return outputfilename + ".asc";
            }

            return null;
        }
蝶舞 2024-08-05 05:29:59

您可以使用 gnupg-sharp,只要您同意使用GPL 和 GnuPG。

You can use gnupg-sharp, as long as you're OK with using the GPL and GnuPG.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文