public GPGDecryptedDataReturn Decrypt(string data) { using (var stream = PgpUtilities.GetDecoderStream(Tools.GenerateStreamFromString(data))) { var pgpF = new PgpObjectFactory(stream); var o = pgpF.NextPgpObject(); var enc = o as PgpEncryptedDataList; if (enc == null) { enc = (PgpEncryptedDataList)pgpF.NextPgpObject(); } PgpPublicKeyEncryptedData pbe = null; PgpPrivateKey pgpPrivKey = null; PgpSecretKey pgpSec = null; string lastFingerPrint = "None"; foreach (PgpPublicKeyEncryptedData pked in enc.GetEncryptedDataObjects()) { string keyId = pked.KeyId.ToString("X").ToUpper(); string fingerPrint = keyId.Length < 16 ? FP8TO16[Tools.H8FP(keyId)] : Tools.H16FP(keyId); lastFingerPrint = fingerPrint; if (!decryptedKeys.ContainsKey(fingerPrint)) { continue; } pgpSec = privateKeys[fingerPrint]; pgpPrivKey = decryptedKeys[fingerPrint]; pbe = pked; break; } if (pbe == null) { throw new KeyNotLoadedException(lastFingerPrint); } var clear = pbe.GetDataStream(pgpPrivKey); var plainFact = new PgpObjectFactory(clear); var message = plainFact.NextPgpObject(); var outData = new GPGDecryptedDataReturn { FingerPrint = lastFingerPrint, }; if (message is PgpCompressedData cData) { var pgpFact = new PgpObjectFactory(cData.GetDataStream()); message = pgpFact.NextPgpObject(); } if (message is PgpLiteralData ld) { outData.Filename = ld.FileName; var iss = ld.GetInputStream(); byte[] buffer = new byte[16 * 1024]; using (var ms = new MemoryStream()) { int read; while ((read = iss.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } outData.Base64Data = Convert.ToBase64String(ms.ToArray()); } } else if (message is PgpOnePassSignatureList) { throw new PgpException("Encrypted message contains a signed message - not literal data."); } else { throw new PgpException("Message is not a simple encrypted file - type unknown."); } outData.IsIntegrityProtected = pbe.IsIntegrityProtected(); if (outData.IsIntegrityProtected) { outData.IsIntegrityOK = pbe.Verify(); } return(outData); } }
public static GPGDecryptedDataReturn DecryptStream(Stream stream, PgpPrivateKey key) { var pgpF = new PgpObjectFactory(stream); var o = pgpF.NextPgpObject(); var enc = o as PgpEncryptedDataList; if (enc == null) { enc = (PgpEncryptedDataList)pgpF.NextPgpObject(); } PgpPublicKeyEncryptedData pbe = null; string lastFingerPrint = "None"; foreach (PgpPublicKeyEncryptedData pked in enc.GetEncryptedDataObjects()) { if (pked.KeyId == key.KeyId) { pbe = pked; break; } } if (pbe == null) { throw new NoKeyAvailableException("There is no payload that matches loaded key."); } var clear = pbe.GetDataStream(key); var plainFact = new PgpObjectFactory(clear); var message = plainFact.NextPgpObject(); var outData = new GPGDecryptedDataReturn { FingerPrint = lastFingerPrint, }; if (message is PgpCompressedData cData) { var pgpFact = new PgpObjectFactory(cData.GetDataStream()); message = pgpFact.NextPgpObject(); } if (message is PgpLiteralData ld) { outData.Filename = ld.FileName; var iss = ld.GetInputStream(); byte[] buffer = new byte[16 * 1024]; using (var ms = new MemoryStream()) { int read; while ((read = iss.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } outData.Base64Data = Convert.ToBase64String(ms.ToArray()); } } else if (message is PgpOnePassSignatureList) { throw new PgpException("Encrypted message contains a signed message - not literal data."); } else { throw new PgpException("Message is not a simple encrypted file - type unknown."); } outData.IsIntegrityProtected = pbe.IsIntegrityProtected(); if (outData.IsIntegrityProtected) { outData.IsIntegrityOK = pbe.Verify(); } return(outData); }