本文介绍了如何使用Bouncy Castle库在C#中使用PGP密钥对txt文件签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有人举过一个例子,说明我如何使用C#和Bouncy Castle库中的PGP密钥对txt文件进行签名。不加密文件,仅添加签名。
Does anyone have an example of how I can sign a txt file, using a PGP key in C# and Bouncy Castle library. Not to encrypt the file, only to add a signature.
推荐答案
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;
}
这篇关于如何使用Bouncy Castle库在C#中使用PGP密钥对txt文件签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!