How to decrypt a file with PGPsdk

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 ...


Go Back   Usenet Forums > System Security and Security Related > Linux Security

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-01-2004
Ramon F Herrera
 
Posts: n/a
Default How to decrypt a file with PGPsdk

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);
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 01:43 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0