如何使用Bouncy Castle库在C#中使用PGP密钥签署txt文件
发布时间:2020-12-16 01:44:42 所属栏目:百科 来源:网络整理
导读:有没有人有一个如何使用C#和Bouncy Castle库中的PGP密钥签署txt文件的示例.不加密文件,只添加签名. 解决方法 public void SignFile(String fileName,Stream privateKeyStream,String privateKeyPassword,Stream outStream){ PgpSecretKey pgpSec = ReadSigni
有没有人有一个如何使用C#和Bouncy Castle库中的PGP密钥签署txt文件的示例.不加密文件,只添加签名.
解决方法public void SignFile(String fileName,Stream privateKeyStream,String privateKeyPassword,Stream outStream) { PgpSecretKey pgpSec = ReadSigningSecretKey(privateKeyStream); PgpPrivateKey pgpPrivKey = null; pgpPrivKey = pgpSec.ExtractPrivateKey(privateKeyPassword.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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |