public static byte[] Decrypt(string encryptedText, RetrievePgpKeys keys) { AlgorithmAgreement m_algorithmsAgreed = new AlgorithmAgreement(keys.PublicKeys); byte[] encryptedBytes = Encoding.UTF8.GetBytes(encryptedText); return(Decrypt(encryptedBytes, keys, m_algorithmsAgreed)); }
public PgpEncryptFile(RetrievePgpKeys keys) { if (keys == null) { throw new ArgumentNullException("keys"); } m_encryptionKeys = keys; m_algorithmsAgreed = new AlgorithmAgreement(keys.PublicKeys); }
public static void AddSignature(PgpPublicKey key, KeyStoreDB keyDb, string keyExportName, PgpSecretKey secKey, char[] passPhrase, SignatureOperation op, DateTime expiryDate, PgpSignature certLevel = null, string userId = "") { string fileData = string.Empty; bool useTemp = true; if (key == null) { throw new ArgumentNullException("key"); } if (keyDb == null) { throw new ArgumentNullException("keyDb"); } if (!Enum.IsDefined(typeof(SignatureOperation), op)) { throw new ArgumentOutOfRangeException(string.Format("op: {0}", op)); } AlgorithmAgreement algorithms = new AlgorithmAgreement(new List <PgpPublicKey>() { key }); PgpPrivateKey privateKey = secKey.ExtractPrivateKey(passPhrase); PgpSignatureGenerator sigGen = new PgpSignatureGenerator(key.Algorithm, algorithms.AgreedHashAlgorithm); switch (op) { case SignatureOperation.AddUserId: break; case SignatureOperation.RevokeKey: MemoryStream mStream = new MemoryStream(); using (ArmoredOutputStream outArmour = new ArmoredOutputStream(mStream)) { outArmour.SetHeader("Version", "Lynx Privacy"); sigGen.InitSign(PgpSignature.KeyRevocation, privateKey); PgpSignature sig = sigGen.GenerateCertification(secKey.PublicKey); PgpPublicKey.AddCertification(secKey.PublicKey, sig); sig.InitVerify(secKey.PublicKey); if (!sig.VerifyCertification(secKey.PublicKey)) { throw new PgpException("revocation verification failed."); } sig.Encode(outArmour); outArmour.Close(); } mStream.Position = 0; StreamReader srdr = new StreamReader(mStream); string armour = srdr.ReadToEnd(); string outstr = armour.Replace("BEGIN PGP SIGNATURE", "BEGIN PGP PUBLIC KEY BLOCK") .Replace("END PGP SIGNATURE", "END PGP PUBLIC KEY BLOCK"); mStream.Close(); if (string.IsNullOrEmpty(keyExportName)) { useTemp = true; string tempPath = Path.GetTempPath(); keyExportName = Path.Combine(tempPath, Guid.NewGuid().ToString() + ".tmppgp"); } File.WriteAllText(keyExportName, outstr); keyExportName = ""; //Debug.Assert(secKey.PublicKey.IsRevoked() == true); break; case SignatureOperation.SetKeyExpiry: break; case SignatureOperation.CertifyKey: break; default: break; } if (secKey.PublicKey != null) { if (string.IsNullOrEmpty(keyExportName)) { useTemp = true; string tempPath = Path.GetTempPath(); keyExportName = Path.Combine(tempPath, Guid.NewGuid().ToString() + ".tmppgp"); } ExportKey expKey = new ExportKey(keyDb); expKey.UpdateDbSecretKey(secKey, keyExportName); if (useTemp) { //File.Delete(keyExportName); } } }
public static byte[] Decrypt(byte[] encryptedData, RetrievePgpKeys keys, AlgorithmAgreement agreed) { Stream inputStream = new MemoryStream(encryptedData); inputStream = PgpUtilities.GetDecoderStream(inputStream); PgpObjectFactory pgpObjFactory = new PgpObjectFactory(inputStream); PgpEncryptedDataList enc; PgpObject pgpObj = pgpObjFactory.NextPgpObject(); if (pgpObj is PgpEncryptedDataList) { enc = (PgpEncryptedDataList)pgpObj; } else { enc = (PgpEncryptedDataList)pgpObjFactory.NextPgpObject(); } PgpPrivateKey privKey = keys.PrivateKey; PgpPublicKeyEncryptedData pbe = null; foreach (PgpPublicKeyEncryptedData pked in enc.GetEncryptedDataObjects()) { if (privKey != null) { pbe = pked; break; } } PgpOnePassSignatureList onePassSigList = null; PgpSignatureList signatureList = null; PgpLiteralData literalData = null; byte[] returnBytes = null; Stream clear = pbe.GetDataStream(privKey); PgpObjectFactory plainFactory = new PgpObjectFactory(clear); for (; ;) { PgpObject message = plainFactory.NextPgpObject(); if (message == null) { break; } if (message is PgpCompressedData) { PgpCompressedData compressedData = (PgpCompressedData)message; plainFactory = new PgpObjectFactory(compressedData.GetDataStream()); } else if (message is PgpLiteralData) { literalData = (PgpLiteralData)message; Stream unc = literalData.GetInputStream(); returnBytes = Org.BouncyCastle.Utilities.IO.Streams.ReadAll(unc); unc.Close(); } else if (message is PgpOnePassSignatureList) { onePassSigList = (PgpOnePassSignatureList)message; } else if (message is PgpSignatureList) { signatureList = (PgpSignatureList)message; } else if (message is PgpMarker) { } else { throw new PgpException("message contains unknown message type."); } } //Stream unc = literalData.GetInputStream(); //byte[] returnBytes = Org.BouncyCastle.Utilities.IO.Streams.ReadAll(unc); if ((onePassSigList != null) && (signatureList != null)) { VerifySignature(onePassSigList, signatureList, keys.SecretKey.PublicKey, returnBytes); } //unc.Close(); clear.Close(); inputStream.Close(); return(returnBytes); }
public static byte[] Decrypt2(byte[] encryptedData, RetrievePgpKeys keys, AlgorithmAgreement agreed) { Stream inputStream = new MemoryStream(encryptedData); inputStream = PgpUtilities.GetDecoderStream(inputStream); PgpObjectFactory pgpObjFactory = new PgpObjectFactory(inputStream); PgpEncryptedDataList enc; PgpObject pgpObj = pgpObjFactory.NextPgpObject(); if (pgpObj is PgpEncryptedDataList) { enc = (PgpEncryptedDataList)pgpObj; } else { enc = (PgpEncryptedDataList)pgpObjFactory.NextPgpObject(); } PgpPrivateKey privKey = keys.PrivateKey; PgpPublicKeyEncryptedData pbe = null; foreach (PgpPublicKeyEncryptedData pked in enc.GetEncryptedDataObjects()) { if (privKey != null) { pbe = pked; break; } } PgpOnePassSignatureList onePassSigList = null; PgpSignatureList signatureList = null; PgpLiteralData literalData = null; Stream clear = pbe.GetDataStream(privKey); PgpObjectFactory plainFactory = new PgpObjectFactory(clear); PgpObject message = plainFactory.NextPgpObject(); if (message is PgpCompressedData) { PgpCompressedData compressedData = (PgpCompressedData)message; Stream compDataIn = compressedData.GetDataStream(); PgpObjectFactory objectFactory = new PgpObjectFactory(compDataIn); message = objectFactory.NextPgpObject(); if (message is PgpOnePassSignatureList) { onePassSigList = (message as PgpOnePassSignatureList); message = objectFactory.NextPgpObject(); if (message is PgpLiteralData) { literalData = (PgpLiteralData)message; } message = objectFactory.NextPgpObject(); if (message is PgpSignatureList) { signatureList = (PgpSignatureList)message; } } compDataIn.Close(); } else { if (message is PgpOnePassSignatureList) { onePassSigList = (message as PgpOnePassSignatureList); } message = plainFactory.NextPgpObject(); if (message is PgpLiteralData) { literalData = (PgpLiteralData)message; } message = plainFactory.NextPgpObject(); if (message is PgpSignatureList) { signatureList = (PgpSignatureList)message; } } Stream unc = literalData.GetInputStream(); byte[] returnBytes = Org.BouncyCastle.Utilities.IO.Streams.ReadAll(unc); if (onePassSigList != null && signatureList != null) { VerifySignature(onePassSigList, signatureList, keys.SecretKey.PublicKey, returnBytes); } unc.Close(); clear.Close(); inputStream.Close(); return(returnBytes); }