public static string encryptFile(this API_OpenPgp openPgp, string fileToEncrypt) { try { var publicKey = openPgp.PublicKey; if (publicKey.fileExists().isFalse()) { publicKey = PublicDI.CurrentScript.directoryName().pathCombine(publicKey); } if (fileToEncrypt.fileExists().isFalse()) { "[API_OpenPgp] in API_OpenPgp signFile, the provided file to encrypt doesn't exist: {0}".error(fileToEncrypt); return(""); } var keyIn = File.OpenRead(publicKey); var pathToEncryptedFile = fileToEncrypt + ".asc"; var fos = File.Create(pathToEncryptedFile); EncryptFile(fos, fileToEncrypt, OpenPgp_HelperMethods.ReadPublicKey(keyIn), true, true); fos.Close(); return(pathToEncryptedFile); } catch (Exception ex) { ex.log("[API_OpenPgp] in encryptFile"); return(null); } }
/** * decrypt the passed in message stream */ private static void DecryptFile( Stream inputStream, Stream keyIn, char[] passwd, string pathToDecryptedFile) //DC { try { inputStream = PgpUtilities.GetDecoderStream(inputStream); try { PgpObjectFactory pgpF = new PgpObjectFactory(inputStream); PgpEncryptedDataList enc; PgpObject o = pgpF.NextPgpObject(); // // the first object might be a PGP marker packet. // if (o is PgpEncryptedDataList) { enc = (PgpEncryptedDataList)o; } else { enc = (PgpEncryptedDataList)pgpF.NextPgpObject(); } // // find the secret key // PgpPrivateKey sKey = null; PgpPublicKeyEncryptedData pbe = null; PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle( PgpUtilities.GetDecoderStream(keyIn)); foreach (PgpPublicKeyEncryptedData pked in enc.GetEncryptedDataObjects()) { sKey = OpenPgp_HelperMethods.FindSecretKey(pgpSec, pked.KeyId, passwd); if (sKey != null) { pbe = pked; break; } } if (sKey == null) { throw new ArgumentException("secret key for message not found."); } Stream clear = pbe.GetDataStream(sKey); PgpObjectFactory plainFact = new PgpObjectFactory(clear); PgpObject message = plainFact.NextPgpObject(); PgpObjectFactory pgpFact = null; if (message is PgpCompressedData) { PgpCompressedData cData = (PgpCompressedData)message; pgpFact = new PgpObjectFactory(cData.GetDataStream()); message = pgpFact.NextPgpObject(); } if (message is PgpOnePassSignatureList) // DC { // DC message = pgpFact.NextPgpObject(); // DC } // DC if (message is PgpLiteralData) { PgpLiteralData ld = (PgpLiteralData)message; Stream fOut = File.Create(pathToDecryptedFile); //DC (modified to use the name provided in pathToDecryptedFile Stream unc = ld.GetInputStream(); Streams.PipeAll(unc, fOut); fOut.Close(); } else if (message is PgpOnePassSignatureList) { "[API_OpenPgp][DecryptFile] encrypted message contains a signed message - not literal data.".error(); return; } else { "[API_OpenPgp][DecryptFile] message is not a simple encrypted file - type unknown.".error(); return; } if (pbe.IsIntegrityProtected()) { if (!pbe.Verify()) { "[API_OpenPgp][DecryptFile] message failed integrity check".error(); } else { "[API_OpenPgp][DecryptFile] message integrity check passed".debug(); } } else { "[API_OpenPgp][DecryptFile] no message integrity check".error(); } } catch (PgpException e) { e.log("[API_OpenPgp] in DecryptFile: " + e.StackTrace); /*Console.Error.WriteLine(e); * * Exception underlyingException = e.InnerException; * if (underlyingException != null) * { * Console.Error.WriteLine(underlyingException.Message); * Console.Error.WriteLine(underlyingException.StackTrace); * }*/ } } catch (Exception ex) { ex.log("[API_OpenPgp] in DecryptFile : " + ex.StackTrace); } }