C#   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用Bouncy Castle库在C#中使用PGP密钥签署txt文件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有人有一个如何使用C#和Bouncy Castle库中的PGP密钥签署txt文件的示例.不加密文件,只添加签名.

解决方法

@H_944_9@public void SignFile(String filename,Stream privateKeyStream,String privateKey@R_450_5747@d,Stream outStream) { PgpSecretKey pgpSec = ReadSigningSecretKey(privateKeyStream); PgpPrivateKey pgpPrivKey = null; pgpPrivKey = pgpSec.ExtractPrivateKey(privateKey@R_450_5747@d.tocharArray()); PgpSignatureGenerator sGen = new PgpSignatureGenerator(pgpSec.PublicKey.Algorithm,KeyStore.ParseHashAlgorithm(this.hash.ToString())); sGen.InitSign(PgpSignature.binaryDocument,pgpPrivKey); foreach (String userId in pgpSec.PublicKey.GetUserIds()) { PgpSignatureSubpacketGenerator spGen = new PgpSignatureSubpacketGenerator(); spGen.SetSignerUserId(false,userId); sGen.SetHashedSubpackets(spGen.Generate()); } CompressionAlgorithmTag compression = PreferredCompression(pgpSec.PublicKey); PgpCompressedDataGenerator cGen = new PgpCompressedDataGenerator(compression); BcpgOutputStream bOut = new BcpgOutputStream(cGen.open(outStream)); sGen.GenerateOnePassversion(false).Encode(bOut); FileInfo file = new FileInfo(fileName); FileStream fIn = new FileStream(filename,FileMode.open,FileAccess.Read,FileShare.Read); PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator(); Stream lOut = lGen.open(bOut,PgpLiteralData.binary,filE); int ch = 0; while ((ch = fIn.ReadByte()) >= 0) { lOut.WriteByte((bytE)ch); sGen.update((bytE) ch); } fIn.Close(); sGen.Generate().Encode(bOut); lGen.Close(); cGen.Close(); outStream.Close(); } public PgpSecretKey ReadSigningSecretKey(Stream instream) // throws IOException,PGPException,WrongPrivateKeyException { PgpSecretKeyRingBundle pgpSec = CreatePgpSecretKeyRingBundle(instream); PgpSecretKey key = null; IEnumerator rIt = pgpSec.GetKeyRings().GetEnumerator(); while (key == null && rIt.MoveNext()) { PgpSecretKeyRing kRing = (PgpSecretKeyRing)rIt.Current; IEnumerator kIt = kRing.GetSecretKeys().GetEnumerator(); while (key == null && kIt.MoveNext()) { PgpSecretKey k = (PgpSecretKey)kIt.Current; if(k.IsSigningKey) key = k; } } if(key == null) throw new WrongPrivateKeyException("Can't find signing key in key ring."); else return key; }

大佬总结

以上是大佬教程为你收集整理的如何使用Bouncy Castle库在C#中使用PGP密钥签署txt文件全部内容,希望文章能够帮你解决如何使用Bouncy Castle库在C#中使用PGP密钥签署txt文件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。