This is a discussion on How to decrypt a file with PGPsdk within the Linux Security forums, part of the System Security and Security Related category; I am trying to decide which SDK is the best for decrypting a lot of files. The PGPsdk has excellent ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I am trying to decide which SDK is the best for decrypting
a lot of files. The PGPsdk has excellent documentation, as can be seen in this sample code taken from the manual. -Ramon -------------------------------------------------------------- /* ==== Decrypt a File code fragment ========================== */ /* * Demonstrates simple file decryption with the PGPsdk. * * Decrypts file "testEncrypted.pgp" encrypted with key * "test@pgp.com", producing decrypted file "testDecrypted.txt". * */ /* Specify the input filename, output filename, and key passphrase to use */ char const *inFileName = "testEncrypted.asc"; char const *outFileName = "testDecrypted.txt"; char const *keyPassphrase = "testPassphrase"; /* Declare and initialize variables */ PGPError err = kPGPError_NoErr; PGPContextRef context = kInvalidPGPContextRef; PGPKeySetRef defaultKeyRing = kInvalidPGPKeySetRef; PGPFileSpecRef inFileRef = kInvalidPGPFileSpecRef; PGPFileSpecRef outFileRef = kInvalidPGPFileSpecRef; /* ---- PGP Setup ------------------------------------------- */ /* NOTE: These calls appear here for clarity only. * In a real-world application, you would call PGPsdkInit() * and PGPNewContext() at application startup time, not on * an operation-by-operation basis. */ /* Initialize the PGPsdk libs */ err = PGPsdkInit(); if (IsPGPError(err)) goto Exit; /* Create the PGPContext */ err = PGPNewContext(kPGPsdkAPIVersion, &context); if (IsPGPError(err)) goto Exit; /* ---- Prepare Decryption Operation Parameters ------------- */ /* Open the default keyring */ err = PGPOpenDefaultKeyRings(context, kPGPKeyRingOpenFlags_Mutable, &defaultKeyRing); if (IsPGPError(err)) goto Exit; /* Create the file descriptions */ err = PGPNewFileSpecFromFullPath(context, inFileName, &inFileRef); if (IsPGPError(err)) goto Exit; err = PGPNewFileSpecFromFullPath(context, outFileName, &outFileRef); if (IsPGPError(err)) goto Exit; /* ---- Perform the Decryption operation ------------- */ /* This is the main event; everything above and * below exists only to support this call. * * Produces the decrypted output file in the default directory. */ err = PGPDecode(context, PGPOInputFile(context, inFileRef), PGPOOutputFile(context, outFileRef), PGPOKeySetRef(context, defaultKeyRing), PGPOPassphrase(context, keyPassphrase), PGPOLastOption(context)); /* ---- Release resources used in this operation ------------- */ Exit: if (PGPFileSpecRefIsValid(inFileRef)) PGPFreeFileSpec(inFileRef); if (PGPFileSpecRefIsValid(outFileRef)) PGPFreeFileSpec(outFileRef); if (PGPKeySetRefIsValid(defaultKeyRing)) PGPFreeKeySet(defaultKeyRing); /* ---- PGP Shutdown ----------------------------------------- */ /* NOTE: These calls appear here for clarity only. * In a real-world application, you would make these calls to * PGPFreeContext() and PGPsdkInit() at application shutdown time, * and not on an operation-by-operation basis. */ /* Release the PGP context we've been using */ if (PGPContextRefIsValid(context)) PGPFreeContext(context); /* PGP library shutdown */ PGPsdkCleanup(); /* ========== End of Decrypt a File code fragment ========== */ exit(0); |