tags:

views:

409

answers:

4

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

Thanks.

+1  A: 

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

Vinay Sajip
keep in mind that gnupg-sharp is a wrapper around the command line utility, and therefore requires the command line utility to be installed.
Randolpho
True, but you don't need a full installation on the client. Just gpg.exe and iconv.dll are enough - which you can install with your application. No registry changes needed, either.
Vinay Sajip
+3  A: 

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.

Randolpho
@Randolpho, can you show me an example of decrypting a PGP file using bouncy castle? I was unable to find any examples in the downloads.
Jonathan Allen
+1  A: 

SharpPrivacy - OpenPGP for C#

Dinah
+1  A: 

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;
        }
ercu
Thank you ercu! You answered all my unasked questions. Going to read ryou blog.
iburlakov