PgP加密解密使用BouncyCastlec#看了很多帖子,有的有教程,但是好像没有。有时,他们引用了一些找不到的类。我可以指点一个地方吗,在那里我可以获得一个简单的教程,展示如何加密和解密文件。我是Pgp的新手,欢迎任何帮助。这个怎么样:BouncycastlePGP解密期间的PartialInputStream此外,该zip包含以下示例:http://www.bouncycastle.org/csharp/希望这有帮助。如果您仍然卡住,请发布有关编译器抱怨的类的更多详细信息,社区会看一看。我知道这个问题已经有好几年了,但谷歌仍然是使用BouncyCastle进行PGP解密相关搜索的第一名或第二名。由于似乎很难找到一个完整、简洁的示例,我想在这里分享我的工作解决方案来解密PGP文件。这只是源文件中包含的BouncyCastle示例的修改版本。以上就是C#学习教程:使用BouncyCastlec#进行PgP加解密分享。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注——usingSystem;使用System.IO;使用Org.BouncyCastle.Bcp??g.OpenPgp;使用Org.BouncyCastle.Utilities.IO;namespacePGPDecrypt{classProgram{staticvoidMain(string[]args){DecryptFile(@"path_to_encrypted_file.pgp",@"path_to_secret_key.asc","your_password_here".ToCharArray(),"output.txt");}privatestaticvoidDecryptFile(stringinputFileName,stringkeyFileName,char[]passwd,stringdefaultFileName){使用(Streaminput=File.OpenRead(inputFileName),keyIn=File.OpenRead(keyFileName)){DecryptFile(input,keyIn,passwd,默认文件名);}}privatestaticvoidDecryptFile(StreaminputStream,StreamkeyIn,char[]passwd,stringdefaultFileName){inputStream=PgpUtilities.GetDecoderStream(inputStream);尝试{PgpObjectFactorypgpF=newPgpObjectFactory(inputStream);PgpEncryptedDataList编码;PgpObjecto=pgpF.NextPgpObject();////第一个t对象可能是PGP标记数据包。//if(o是PgpEncryptedDataList){enc=(PgpEncryptedDataList)o;}else{enc=(PgpEncryptedDataList)pgpF.NextPgpObject();}////找到密钥//PgpPrivateKeysKey=null;PgpPublicKeyEncryptedDatapbe=null;PgpSecretKeyRingBundlepgpSec=newPgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(keyIn));foreach(PgpPublicKeyEncryptedDatapkedinenc.GetEncryptedDataObjects()){sKey=FindSecretKey(pgpSec,pked.KeyId,passwd);if(sKey!=null){pbe=pked;休息;}}if(sKey==null){thrownewArgumentException("找不到消息的密钥。");}Streamclear=pbe.GetDataStream(sKey);PgpObjectFactoryplainFact=newPgpObjectFactory(clear);PgpObjectmessage=plainFact.NextPgpObject();如果(消息是PgpCompressedData){PgpCompressedDatacData=(PgpCompressedData)消息;PgpObjectFactorypgpFact=newPgpObjectFactory(cData.GetDataStream());消息=pgpFact.NextPgpObject();}如果(消息是PgpLiteralData){PgpLiteralDatald=(PgpLiteralData)消息;stringoutFileName=ld.FileName;如果(outFileName.Length==0){outFileName=defaultFileName;}StreamfOut=File.Create(outFileName);流unc=ld.GetInputStream();Streams.PipeAll(unc,fOut);fOut.Close();}elseif(messageisPgpOnePassSignatureList){thrownewPgpException("加密消息包含签名消息-不是文字数据。");}else{thrownewPgpException("消息不是简单的加密文件-类型未知。");}if(pbe.IsIntegrityProtected()){if(!pbe.Verify()){Console.Error.WriteLine("消息完整性检查失败");}else{Console.Error.WriteLine("消息完整性检查通过");}}else{Console.Error.WriteLine("无消息完整性检查");}}catch(PgpExceptione){Console.Error.WriteLine(e);异常underlyingException=e.InnerException;如果(underlyingException!=null){Console.Error.WriteLine(underlyingException.信息);Console.Error.WriteLine(underlyingException.StackTrace);}}}privatestaticPgpPrivateKeyFindSecretKey(PgpSecretKeyRingBundlepgpSec,longkeyID,char[]pass){PgpSecretKeypgpSecKey=pgpSec.GetSecretKey(keyID);如果(){返回空值;}返回pgpSecKey.ExtractPrivateKey(通过);}}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
