encryption - C# How to simply encrypt a text file with a PGP Public Key? -


i've researched bit how achieve said in question , found several apis of them complicated , since i'm noobie in area want simple method like:

public string encrypt(string message, publickey publickey) 

don't know if can done? if not please enlighten me way achieve :)

thank you.

update:

so far have seen of library openpgp encryption require both public key , private key encrypt while want encrypt public key (because don't have private key use it)!

i found tutorial here requires both secret key , public key encrypt data. i've modified codes bit require public key (no signing, no compress) , thought should publish here in case looking solution question. belows modified codes, credits author - mr. kim.

public class pgpencrypt     {         private pgpencryptionkeys m_encryptionkeys;         private const int buffersize = 0x10000;          /// <summary>         /// instantiate new pgpencrypt class initialized pgpencryptionkeys.         /// </summary>         /// <param name="encryptionkeys"></param>         /// <exception cref="argumentnullexception">encryptionkeys null</exception>         public pgpencrypt(pgpencryptionkeys encryptionkeys)         {             if (encryptionkeys == null)             {                 throw new argumentnullexception("encryptionkeys", "encryptionkeys null.");             }             m_encryptionkeys = encryptionkeys;         }         /// <summary>         /// encrypt , sign file pointed unencryptedfileinfo ,         /// write encrypted content outputstream.         /// </summary>         /// <param name="outputstream">the stream contain         /// encrypted data when method returns.</param>         /// <param name="filename">fileinfo of file encrypt</param>         public void encrypt(stream outputstream, fileinfo unencryptedfileinfo)         {             if (outputstream == null)             {                 throw new argumentnullexception("outputstream", "outputstream null.");             }             if (unencryptedfileinfo == null)             {                 throw new argumentnullexception("unencryptedfileinfo", "unencryptedfileinfo null.");             }             if (!file.exists(unencryptedfileinfo.fullname))             {                 throw new argumentexception("file encrypt not found.");             }             using (stream encryptedout = chainencryptedout(outputstream))             {                 using (stream literalout = chainliteralout(encryptedout, unencryptedfileinfo))                 using (filestream inputfile = unencryptedfileinfo.openread())                 {                     writeoutput(literalout, inputfile);                 }             }         }          private static void writeoutput(stream literalout,             filestream inputfile)         {             int length = 0;             byte[] buf = new byte[buffersize];             while ((length = inputfile.read(buf, 0, buf.length)) > 0)             {                 literalout.write(buf, 0, length);             }         }          private stream chainencryptedout(stream outputstream)         {             pgpencrypteddatagenerator encrypteddatagenerator;             encrypteddatagenerator =                 new pgpencrypteddatagenerator(symmetrickeyalgorithmtag.tripledes,                                               new securerandom());             encrypteddatagenerator.addmethod(m_encryptionkeys.publickey);             return encrypteddatagenerator.open(outputstream, new byte[buffersize]);         }          private static stream chainliteralout(stream encryptedout, fileinfo file)         {             pgpliteraldatagenerator pgpliteraldatagenerator = new pgpliteraldatagenerator();             return pgpliteraldatagenerator.open(encryptedout, pgpliteraldata.binary,   file);             }  } 

of course run these codes have include bouncycastle library in project.
i've tested encrypting , decrypting , runs fine :)


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -