public override void Encode(BcpgOutputStream bcpgOut) { MemoryStream memoryStream = new MemoryStream(); BcpgOutputStream bcpgOutputStream = new BcpgOutputStream(memoryStream); bcpgOutputStream.WriteByte((byte)version); if (version == 3 || version == 2) { bcpgOutputStream.Write(5, (byte)signatureType); bcpgOutputStream.WriteInt((int)(creationTime / 1000)); bcpgOutputStream.WriteLong(keyId); bcpgOutputStream.Write((byte)keyAlgorithm, (byte)hashAlgorithm); } else { if (version != 4) { throw new IOException("unknown version: " + version); } bcpgOutputStream.Write((byte)signatureType, (byte)keyAlgorithm, (byte)hashAlgorithm); EncodeLengthAndData(bcpgOutputStream, GetEncodedSubpackets(hashedData)); EncodeLengthAndData(bcpgOutputStream, GetEncodedSubpackets(unhashedData)); } bcpgOutputStream.Write(fingerprint); if (signature != null) { bcpgOutputStream.WriteObjects(signature); } else { bcpgOutputStream.Write(signatureEncoding); } bcpgOut.WritePacket(PacketTag.Signature, memoryStream.ToArray(), oldFormat: true); }
private void WriteHeader(BcpgOutputStream outStr, char format, byte[] encName, long modificationTime) { outStr.Write((byte)format, (byte)encName.Length); outStr.Write(encName); long num = modificationTime / 1000; outStr.Write((byte)(num >> 24), (byte)(num >> 16), (byte)(num >> 8), (byte)num); }
private void WriteHeader(BcpgOutputStream outStr, char format, string name, long modificationTime) { byte[] encName = Strings.ToUtf8ByteArray(name); outStr.Write((byte) format, (byte) encName.Length); outStr.Write(encName); long modDate = modificationTime/1000L; outStr.Write((byte) (modDate >> 24), (byte) (modDate >> 16), (byte) (modDate >> 8), (byte) modDate); }
private void WriteHeader(BcpgOutputStream outStr, char format, string name, long modificationTime) { byte[] encName = Strings.ToUtf8ByteArray(name); outStr.Write((byte)format, (byte)encName.Length); outStr.Write(encName); long modDate = modificationTime / 1000L; outStr.Write((byte)(modDate >> 24), (byte)(modDate >> 16), (byte)(modDate >> 8), (byte)modDate); }
public override void Encode(BcpgOutputStream bcpgOut) { MemoryStream memoryStream = new MemoryStream(); BcpgOutputStream bcpgOutputStream = new BcpgOutputStream(memoryStream); bcpgOutputStream.Write((byte)version, (byte)encAlgorithm); bcpgOutputStream.WriteObject(s2k); if (secKeyData != null && secKeyData.Length > 0) { bcpgOutputStream.Write(secKeyData); } bcpgOut.WritePacket(PacketTag.SymmetricKeyEncryptedSessionKey, memoryStream.ToArray(), oldFormat: true); }
public void Close() { if (cOut != null) { if (digestOut != null) { BcpgOutputStream bcpgOutputStream = new BcpgOutputStream(digestOut, PacketTag.ModificationDetectionCode, 20L); bcpgOutputStream.Flush(); digestOut.Flush(); byte[] array = DigestUtilities.DoFinal(digestOut.WriteDigest()); cOut.Write(array, 0, array.Length); } cOut.Flush(); try { pOut.Write(c.DoFinal()); pOut.Finish(); } catch (Exception ex) { throw new IOException(ex.Message, ex); } cOut = null; pOut = null; } }
public void Close() { //IL_008c: Unknown result type (might be due to invalid IL or missing references) if (cOut != null) { if (digestOut != null) { BcpgOutputStream bcpgOutputStream = new BcpgOutputStream((Stream)(object)digestOut, PacketTag.ModificationDetectionCode, 20L); ((Stream)bcpgOutputStream).Flush(); ((Stream)digestOut).Flush(); byte[] array = DigestUtilities.DoFinal(digestOut.WriteDigest()); ((Stream)cOut).Write(array, 0, array.Length); } ((Stream)cOut).Flush(); try { pOut.Write(c.DoFinal()); pOut.Finish(); } catch (global::System.Exception ex) { throw new IOException(ex.get_Message(), ex); } cOut = null; pOut = null; } }
public override void Encode(BcpgOutputStream bcpgOut) { byte[] encoded = oid.GetEncoded(); bcpgOut.Write(encoded, 1, encoded.Length - 1); MPInteger bcpgObject = new MPInteger(point); bcpgOut.WriteObject(bcpgObject); }
private void WriteHeader(char format, string name, long modificationTime) { var encName = Strings.ToUtf8ByteArray(name); _pkOut.Write( (byte)format, (byte)encName.Length); _pkOut.Write(encName); var modDate = modificationTime / 1000L; _pkOut.Write( (byte)(modDate >> 24), (byte)(modDate >> 16), (byte)(modDate >> 8), (byte)modDate); }
public override void Encode(BcpgOutputStream bcpgOut) { MemoryStream memoryStream = new MemoryStream(); BcpgOutputStream bcpgOutputStream = new BcpgOutputStream(memoryStream); bcpgOutputStream.Write((byte)version, (byte)sigType, (byte)hashAlgorithm, (byte)keyAlgorithm); bcpgOutputStream.WriteLong(keyId); bcpgOutputStream.WriteByte((byte)nested); bcpgOut.WritePacket(PacketTag.OnePassSignature, memoryStream.ToArray(), oldFormat: true); }
public static void SignFile(Stream input, Stream outputStream, Stream keyIn, char[] pass) { var hashAlgorithm = HashAlgorithmTag.Sha512; var secretKey = ReadSecretKey(keyIn); var privateKey = secretKey.ExtractPrivateKey(pass); var signatureGenerator = new PgpSignatureGenerator(secretKey.PublicKey.Algorithm, hashAlgorithm); var subpacketGenerator = new PgpSignatureSubpacketGenerator(); signatureGenerator.InitSign(PgpSignature.CanonicalTextDocument, privateKey); foreach (string userId in secretKey.PublicKey.GetUserIds()) { var signatureSubpacketGenerator = new PgpSignatureSubpacketGenerator(); signatureSubpacketGenerator.SetSignerUserId(isCritical: false, userId: userId); signatureGenerator.SetHashedSubpackets(signatureSubpacketGenerator.Generate()); // Just the first one! break; } // Closing armouredOutputStream does not close the underlying stream var armouredOutputStream = new ArmoredOutputStream(outputStream); using (var bcpgOutputStream = new BcpgOutputStream(armouredOutputStream)) { armouredOutputStream.BeginClearText(hashAlgorithm); int chr; while ((chr = input.ReadByte()) > 0) { signatureGenerator.Update((byte)chr); bcpgOutputStream.Write((byte)chr); } // For some reason we need to add a trailing newline bcpgOutputStream.Write((byte)'\n'); armouredOutputStream.EndClearText(); signatureGenerator.Generate().Encode(bcpgOutputStream); } }
private void WriteHeader( BcpgOutputStream outStr, char format, string name, long modificationTime) { byte[] asciiName = Encoding.ASCII.GetBytes(name); outStr.Write( (byte)format, (byte)asciiName.Length); outStr.Write(asciiName); long modDate = modificationTime / 1000L; outStr.Write( (byte)(modDate >> 24), (byte)(modDate >> 16), (byte)(modDate >> 8), (byte)modDate); }
public byte[] GetEncodedContents() { MemoryStream memoryStream = new MemoryStream(); BcpgOutputStream bcpgOutputStream = new BcpgOutputStream(memoryStream); bcpgOutputStream.Write(pubKeyPacket.GetEncodedContents()); bcpgOutputStream.WriteByte((byte)s2kUsage); if (s2kUsage == 255 || s2kUsage == 254) { bcpgOutputStream.WriteByte((byte)encAlgorithm); bcpgOutputStream.WriteObject(s2k); } if (iv != null) { bcpgOutputStream.Write(iv); } if (secKeyData != null && secKeyData.Length > 0) { bcpgOutputStream.Write(secKeyData); } return(memoryStream.ToArray()); }
public override void Encode(BcpgOutputStream bcpgOut) { MemoryStream memoryStream = new MemoryStream(); BcpgOutputStream bcpgOutputStream = new BcpgOutputStream(memoryStream); bcpgOutputStream.WriteByte((byte)version); bcpgOutputStream.WriteLong(keyId); bcpgOutputStream.WriteByte((byte)algorithm); for (int i = 0; i < data.Length; i++) { bcpgOutputStream.Write(data[i]); } Platform.Dispose(bcpgOutputStream); bcpgOut.WritePacket(PacketTag.PublicKeyEncryptedSession, memoryStream.ToArray(), oldFormat: true); }
public override void Encode(BcpgOutputStream bcpgOut) { bcpgOut.WriteByte((byte)type); bcpgOut.WriteByte((byte)algorithm); if (type != 101) { if (type != 0) { bcpgOut.Write(iv); } if (type == 3) { bcpgOut.WriteByte((byte)itCount); } } else { bcpgOut.WriteByte(71); bcpgOut.WriteByte(78); bcpgOut.WriteByte(85); bcpgOut.WriteByte((byte)protectionMode); } }
/// <summary> /// <p> /// Close off the encrypted object - this is equivalent to calling Close() on the stream /// returned by the Open() method. /// </p> /// <p> /// <b>Note</b>: This does not close the underlying output stream, only the stream on top of /// it created by the Open() method. /// </p> /// </summary> public void Close() { if (cOut != null) { // TODO Should this all be under the try/catch block? if (digestOut != null) { // // hand code a mod detection packet // BcpgOutputStream bOut = new BcpgOutputStream( digestOut, PacketTag.ModificationDetectionCode, 20); bOut.Flush(); digestOut.Flush(); // TODO byte[] dig = DigestUtilities.DoFinal(digestOut.WriteDigest()); cOut.Write(dig, 0, dig.Length); } cOut.Flush(); try { pOut.Write(c.DoFinal()); pOut.Finish(); } catch (Exception e) { throw new IOException(e.Message, e); } cOut = null; pOut = null; } }
internal PgpSecretKey( PgpPrivateKey privKey, PgpPublicKey pubKey, SymmetricKeyAlgorithmTag encAlgorithm, char[] passPhrase, bool useSha1, ISecureRandom rand, bool isMasterKey) { BcpgObject secKey; _pub = pubKey; switch (pubKey.Algorithm) { case PublicKeyAlgorithmTag.RsaEncrypt: case PublicKeyAlgorithmTag.RsaSign: case PublicKeyAlgorithmTag.RsaGeneral: var rsK = (RsaPrivateCrtKeyParameters)privKey.Key; secKey = new RsaSecretBcpgKey(rsK.Exponent, rsK.P, rsK.Q); break; case PublicKeyAlgorithmTag.Dsa: var dsK = (DsaPrivateKeyParameters)privKey.Key; secKey = new DsaSecretBcpgKey(dsK.X); break; case PublicKeyAlgorithmTag.ElGamalEncrypt: case PublicKeyAlgorithmTag.ElGamalGeneral: var esK = (ElGamalPrivateKeyParameters)privKey.Key; secKey = new ElGamalSecretBcpgKey(esK.X); break; case PublicKeyAlgorithmTag.Ecdh: case PublicKeyAlgorithmTag.Ecdsa: var ecK = (ECPrivateKeyParameters)privKey.Key; secKey = new ECSecretBcpgKey(ecK.D); break; default: throw new PgpException("unknown key class"); } try { using (var bOut = new MemoryStream()) { using (var pOut = new BcpgOutputStream(bOut)) { pOut.WriteObject(secKey); var keyData = bOut.ToArray(); var checksumBytes = Checksum(useSha1, keyData, keyData.Length); pOut.Write(checksumBytes); var bOutData = bOut.ToArray(); if (encAlgorithm == SymmetricKeyAlgorithmTag.Null) { this._secret = isMasterKey ? new SecretKeyPacket(_pub.PublicKeyPacket, encAlgorithm, null, null, bOutData) : new SecretSubkeyPacket(_pub.PublicKeyPacket, encAlgorithm, null, null, bOutData); } else { S2k s2K; byte[] iv; var encData = EncryptKeyData(bOutData, encAlgorithm, passPhrase, rand, out s2K, out iv); var s2KUsage = useSha1 ? SecretKeyPacket.UsageSha1 : SecretKeyPacket.UsageChecksum; this._secret = isMasterKey ? new SecretKeyPacket(_pub.PublicKeyPacket, encAlgorithm, s2KUsage, s2K, iv, encData) : new SecretSubkeyPacket(_pub.PublicKeyPacket, encAlgorithm, s2KUsage, s2K, iv, encData); } } } } catch (PgpException) { throw; } catch (Exception e) { throw new PgpException("Exception encrypting key", e); } }
internal PgpSecretKey( PgpPrivateKey privKey, PgpPublicKey pubKey, SymmetricKeyAlgorithmTag encAlgorithm, char[] passPhrase, bool useSha1, SecureRandom rand, bool isMasterKey) { BcpgObject secKey; this.pub = pubKey; switch (pubKey.Algorithm) { case PublicKeyAlgorithmTag.RsaEncrypt: case PublicKeyAlgorithmTag.RsaSign: case PublicKeyAlgorithmTag.RsaGeneral: RsaPrivateCrtKeyParameters rsK = (RsaPrivateCrtKeyParameters)privKey.Key; secKey = new RsaSecretBcpgKey(rsK.Exponent, rsK.P, rsK.Q); break; case PublicKeyAlgorithmTag.Dsa: DsaPrivateKeyParameters dsK = (DsaPrivateKeyParameters)privKey.Key; secKey = new DsaSecretBcpgKey(dsK.X); break; case PublicKeyAlgorithmTag.ElGamalEncrypt: case PublicKeyAlgorithmTag.ElGamalGeneral: ElGamalPrivateKeyParameters esK = (ElGamalPrivateKeyParameters)privKey.Key; secKey = new ElGamalSecretBcpgKey(esK.X); break; default: throw new PgpException("unknown key class"); } try { MemoryStream bOut = new MemoryStream(); BcpgOutputStream pOut = new BcpgOutputStream(bOut); pOut.WriteObject(secKey); byte[] keyData = bOut.ToArray(); byte[] checksumBytes = Checksum(useSha1, keyData, keyData.Length); pOut.Write(checksumBytes); byte[] bOutData = bOut.ToArray(); if (encAlgorithm == SymmetricKeyAlgorithmTag.Null) { if (isMasterKey) { this.secret = new SecretKeyPacket(pub.publicPk, encAlgorithm, null, null, bOutData); } else { this.secret = new SecretSubkeyPacket(pub.publicPk, encAlgorithm, null, null, bOutData); } } else { S2k s2k; byte[] iv; byte[] encData = EncryptKeyData(bOutData, encAlgorithm, passPhrase, rand, out s2k, out iv); int s2kUsage = useSha1 ? SecretKeyPacket.UsageSha1 : SecretKeyPacket.UsageChecksum; if (isMasterKey) { this.secret = new SecretKeyPacket(pub.publicPk, encAlgorithm, s2kUsage, s2k, iv, encData); } else { this.secret = new SecretSubkeyPacket(pub.publicPk, encAlgorithm, s2kUsage, s2k, iv, encData); } } } catch (PgpException e) { throw e; } catch (Exception e) { throw new PgpException("Exception encrypting key", e); } }
private void WriteHeader( BcpgOutputStream outStr, char format, string name, long modificationTime) { byte[] asciiName = Encoding.ASCII.GetBytes(name); outStr.Write( (byte) format, (byte) asciiName.Length); outStr.Write(asciiName); long modDate = modificationTime / 1000L; outStr.Write( (byte)(modDate >> 24), (byte)(modDate >> 16), (byte)(modDate >> 8), (byte)modDate); }
private static SecretKeyPacket buildSecretKeyPacket(bool isMasterKey, PgpPrivateKey privKey, PgpPublicKey pubKey, IPbeSecretKeyEncryptor keyEncryptor) { BcpgObject secKey = (BcpgObject)privKey.Key; if (secKey == null) { if (isMasterKey) { return(new SecretKeyPacket(pubKey.publicPk, SymmetricKeyAlgorithmTag.Null, null, null, new byte[0])); } else { return(new SecretSubkeyPacket(pubKey.publicPk, SymmetricKeyAlgorithmTag.Null, null, null, new byte[0])); } } try { MemoryOutputStream bOut = new MemoryOutputStream(); BcpgOutputStream pOut = new BcpgOutputStream(bOut); pOut.WriteObject(secKey); byte[] keyData = bOut.ToArray(); byte[] checkData = checksum(keyEncryptor.ChecksumCalculatorFactory, keyData, keyData.Length); pOut.Write(checkData, 0, checkData.Length); PgpPbeKeyEncryptionParameters encParams = keyEncryptor.AlgorithmDetails; SymmetricKeyAlgorithmTag encAlgorithm = (keyEncryptor != null) ? encParams.Algorithm : SymmetricKeyAlgorithmTag.Null; if (encAlgorithm != SymmetricKeyAlgorithmTag.Null) { keyData = bOut.ToArray(); // include checksum byte[] encData = keyEncryptor.Wrap(keyData).Collect(); byte[] iv = encParams.GetIV(); S2k s2k = encParams.S2k; int s2kUsage; if (keyEncryptor.ChecksumCalculatorFactory != null) { if (keyEncryptor.ChecksumCalculatorFactory.AlgorithmDetails.Algorithm != HashAlgorithmTag.Sha1) { throw new PgpException("only SHA1 supported for key checksum calculations."); } s2kUsage = SecretKeyPacket.UsageSha1; } else { s2kUsage = SecretKeyPacket.UsageChecksum; } if (isMasterKey) { return(new SecretKeyPacket(pubKey.publicPk, encAlgorithm, s2kUsage, s2k, iv, encData)); } else { return(new SecretSubkeyPacket(pubKey.publicPk, encAlgorithm, s2kUsage, s2k, iv, encData)); } } else { if (isMasterKey) { return(new SecretKeyPacket(pubKey.publicPk, encAlgorithm, null, null, bOut.ToArray())); } else { return(new SecretSubkeyPacket(pubKey.publicPk, encAlgorithm, null, null, bOut.ToArray())); } } } catch (PgpException e) { throw e; } catch (Exception e) { throw new PgpException("Exception encrypting key", e); } }
public override void Encode(BcpgOutputStream bcpgOut) { bcpgOut.WriteShort((short)val.BitLength); bcpgOut.Write(val.ToByteArrayUnsigned()); }
private static void EncodeLengthAndData(BcpgOutputStream pOut, byte[] data) { pOut.WriteShort((short)data.Length); pOut.Write(data); }
internal PgpSecretKey( PgpPrivateKey privKey, PgpPublicKey pubKey, SymmetricKeyAlgorithmTag encAlgorithm, char[] passPhrase, bool useSha1, SecureRandom rand, bool isMasterKey) { BcpgObject secKey; this.pub = pubKey; switch (pubKey.Algorithm) { case PublicKeyAlgorithmTag.RsaEncrypt: case PublicKeyAlgorithmTag.RsaSign: case PublicKeyAlgorithmTag.RsaGeneral: RsaPrivateCrtKeyParameters rsK = (RsaPrivateCrtKeyParameters) privKey.Key; secKey = new RsaSecretBcpgKey(rsK.Exponent, rsK.P, rsK.Q); break; case PublicKeyAlgorithmTag.Dsa: DsaPrivateKeyParameters dsK = (DsaPrivateKeyParameters) privKey.Key; secKey = new DsaSecretBcpgKey(dsK.X); break; case PublicKeyAlgorithmTag.ElGamalEncrypt: case PublicKeyAlgorithmTag.ElGamalGeneral: ElGamalPrivateKeyParameters esK = (ElGamalPrivateKeyParameters) privKey.Key; secKey = new ElGamalSecretBcpgKey(esK.X); break; default: throw new PgpException("unknown key class"); } try { MemoryStream bOut = new MemoryStream(); BcpgOutputStream pOut = new BcpgOutputStream(bOut); pOut.WriteObject(secKey); byte[] keyData = bOut.ToArray(); byte[] checksumBytes = Checksum(useSha1, keyData, keyData.Length); pOut.Write(checksumBytes); byte[] bOutData = bOut.ToArray(); if (encAlgorithm == SymmetricKeyAlgorithmTag.Null) { if (isMasterKey) { this.secret = new SecretKeyPacket(pub.publicPk, encAlgorithm, null, null, bOutData); } else { this.secret = new SecretSubkeyPacket(pub.publicPk, encAlgorithm, null, null, bOutData); } } else { S2k s2k; byte[] iv; byte[] encData = EncryptKeyData(bOutData, encAlgorithm, passPhrase, rand, out s2k, out iv); int s2kUsage = useSha1 ? SecretKeyPacket.UsageSha1 : SecretKeyPacket.UsageChecksum; if (isMasterKey) { this.secret = new SecretKeyPacket(pub.publicPk, encAlgorithm, s2kUsage, s2k, iv, encData); } else { this.secret = new SecretSubkeyPacket(pub.publicPk, encAlgorithm, s2kUsage, s2k, iv, encData); } } } catch (PgpException e) { throw e; } catch (Exception e) { throw new PgpException("Exception encrypting key", e); } }
internal PgpSecretKey( PgpKeyPair keyPair, SymmetricKeyAlgorithmTag encAlgorithm, char[] passPhrase, bool useSHA1, SecureRandom rand) { PublicKeyPacket pubPk = keyPair.PublicKey.publicPk; BcpgObject secKey; switch (keyPair.PublicKey.Algorithm) { case PublicKeyAlgorithmTag.RsaEncrypt: case PublicKeyAlgorithmTag.RsaSign: case PublicKeyAlgorithmTag.RsaGeneral: RsaPrivateCrtKeyParameters rsK = (RsaPrivateCrtKeyParameters)keyPair.PrivateKey.Key; secKey = new RsaSecretBcpgKey(rsK.Exponent, rsK.P, rsK.Q); break; case PublicKeyAlgorithmTag.Dsa: DsaPrivateKeyParameters dsK = (DsaPrivateKeyParameters)keyPair.PrivateKey.Key; secKey = new DsaSecretBcpgKey(dsK.X); break; case PublicKeyAlgorithmTag.ElGamalEncrypt: case PublicKeyAlgorithmTag.ElGamalGeneral: ElGamalPrivateKeyParameters esK = (ElGamalPrivateKeyParameters)keyPair.PrivateKey.Key; secKey = new ElGamalSecretBcpgKey(esK.X); break; default: throw new PgpException("unknown key class"); } string cName = PgpUtilities.GetSymmetricCipherName(encAlgorithm); IBufferedCipher c = null; if (cName != null) { try { c = CipherUtilities.GetCipher(cName + "/CFB/NoPadding"); } catch (Exception e) { throw new PgpException("Exception creating cipher", e); } } try { MemoryStream bOut = new MemoryStream(); BcpgOutputStream pOut = new BcpgOutputStream(bOut); pOut.WriteObject(secKey); byte[] keyData = bOut.ToArray(); byte[] checksumBytes = Checksum(useSHA1, keyData, keyData.Length); pOut.Write(checksumBytes); byte[] bOutData = bOut.ToArray(); if (c != null) { byte[] iv = new byte[8]; rand.NextBytes(iv); S2k s2k = new S2k(HashAlgorithmTag.Sha1, iv, 0x60); KeyParameter key = PgpUtilities.MakeKeyFromPassPhrase(encAlgorithm, s2k, passPhrase); iv = new byte[c.GetBlockSize()]; rand.NextBytes(iv); c.Init(true, new ParametersWithIV(key, iv)); byte[] encData = c.DoFinal(bOutData); int usage = useSHA1 ? SecretKeyPacket.UsageSha1 : SecretKeyPacket.UsageChecksum; this.secret = new SecretKeyPacket(pubPk, encAlgorithm, usage, s2k, iv, encData); } else { this.secret = new SecretKeyPacket(pubPk, encAlgorithm, null, null, bOutData); } this.trust = null; } catch (PgpException e) { throw e; } catch (Exception e) { throw new PgpException("Exception encrypting key", e); } this.keySigs = new ArrayList(); }
internal static void Encode(BcpgOutputStream bcpgOut, BigInteger val) { bcpgOut.WriteShort((short)val.BitLength); bcpgOut.Write(val.ToByteArrayUnsigned()); }
internal PgpSecretKey( PgpKeyPair keyPair, SymmetricKeyAlgorithmTag encAlgorithm, char[] passPhrase, bool useSHA1, SecureRandom rand) { PublicKeyPacket pubPk = keyPair.PublicKey.publicPk; BcpgObject secKey; switch (keyPair.PublicKey.Algorithm) { case PublicKeyAlgorithmTag.RsaEncrypt: case PublicKeyAlgorithmTag.RsaSign: case PublicKeyAlgorithmTag.RsaGeneral: RsaPrivateCrtKeyParameters rsK = (RsaPrivateCrtKeyParameters) keyPair.PrivateKey.Key; secKey = new RsaSecretBcpgKey(rsK.Exponent, rsK.P, rsK.Q); break; case PublicKeyAlgorithmTag.Dsa: DsaPrivateKeyParameters dsK = (DsaPrivateKeyParameters) keyPair.PrivateKey.Key; secKey = new DsaSecretBcpgKey(dsK.X); break; case PublicKeyAlgorithmTag.ElGamalEncrypt: case PublicKeyAlgorithmTag.ElGamalGeneral: ElGamalPrivateKeyParameters esK = (ElGamalPrivateKeyParameters) keyPair.PrivateKey.Key; secKey = new ElGamalSecretBcpgKey(esK.X); break; default: throw new PgpException("unknown key class"); } string cName = PgpUtilities.GetSymmetricCipherName(encAlgorithm); IBufferedCipher c = null; if (cName != null) { try { c = CipherUtilities.GetCipher(cName + "/CFB/NoPadding"); } catch (Exception e) { throw new PgpException("Exception creating cipher", e); } } try { MemoryStream bOut = new MemoryStream(); BcpgOutputStream pOut = new BcpgOutputStream(bOut); pOut.WriteObject(secKey); byte[] keyData = bOut.ToArray(); byte[] checksumBytes = Checksum(useSHA1, keyData, keyData.Length); pOut.Write(checksumBytes); byte[] bOutData = bOut.ToArray(); if (c != null) { byte[] iv = new byte[8]; rand.NextBytes(iv); S2k s2k = new S2k(HashAlgorithmTag.Sha1, iv, 0x60); KeyParameter key = PgpUtilities.MakeKeyFromPassPhrase(encAlgorithm, s2k, passPhrase); iv = new byte[c.GetBlockSize()]; rand.NextBytes(iv); c.Init(true, new ParametersWithIV(key, iv)); byte[] encData = c.DoFinal(bOutData); int usage = useSHA1 ? SecretKeyPacket.UsageSha1 : SecretKeyPacket.UsageChecksum; this.secret = new SecretKeyPacket(pubPk, encAlgorithm, usage, s2k, iv, encData); } else { this.secret = new SecretKeyPacket(pubPk, encAlgorithm, null, null, bOutData); } this.trust = null; } catch (PgpException e) { throw e; } catch (Exception e) { throw new PgpException("Exception encrypting key", e); } this.keySigs = new ArrayList(); }