@H_
944_9@public void SignFile(
String fil
ename,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(thi
s.hash.To
String())
);
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.GenerateOnePa
ssversion
(false).Encode(bOut
);
FileInfo file = new FileInfo(file
Name);
FileStream fIn = new FileStream(fil
ename,FileMode
.open,FileAcces
s.Read,FileShare.Read
);
PgpLiteralDataGenerator lGen = new PgpLiteralDataGenerator(
);
Stream lOut = lGen
.open(bOut,PgpLiteralData
.binary,fil
E);
int ch = 0;
while ((ch = fIn.ReadByte()) >= 0)
{
lOut.WriteByte((byt
E)ch
);
sGen.
update((byt
E) ch
);
}
fIn.Close(
);
sGen.Generate().Encode(bOut
);
lGen.Close(
);
cGen.Close(
);
outStrea
m.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;
}