//CgeepApiExample.cs //Copyright (c) SafeLogic, 2000 - 2009 // //Last Updates: // 19 oct. 2009 20:42:14 Nicolas de Pomereu using System; using System.Collections.Generic; using System.Windows.Forms; // The cGeep API Namespace using CGEEPAPI4XLib; namespace CgeepApiCSharp2005 { static class CgeepApiExample { /// <summary> /// Point d'entrée principal de l'application. /// </summary> [STAThread] static void Main() { string TEMP_DIR = "c:\\temp"; // 1) Load a cGeep API instance CgeepApi cgeepApi = new CgeepApi(); //string version = cgeepApi.getVersion(); //MessageBox.Show(version); // 1) Set the keyring in c:\temp cgeepApi.setKeyRingDirectory(TEMP_DIR); // 2) Create two key pairs // (it will generate a private key ring & a public key ring in c:\\temp) // Create a key with test1@test.com as PGP UserId cgeepApi.generateKeyPair("test1@test.com", "passphrase", "RSA", 1024, "AES-256", "NEVER"); if (!cgeepApi.isOperationOk()) { MessageBox.Show("Error : " + cgeepApi.getErrorCode()); MessageBox.Show("Exception: " + cgeepApi.getException()); return; } // Create a key with test2@test.com as PGP UserId cgeepApi.generateKeyPair("test2@test.com", "passphrase", "RSA", 1024, "AES-256", "NEVER"); if (!cgeepApi.isOperationOk()) { MessageBox.Show("Error : " + cgeepApi.getErrorCode()); MessageBox.Show("Exception: " + cgeepApi.getException()); return; } // We display the key list String list = cgeepApi.listKeys(""); if(!cgeepApi.isOperationOk()) { MessageBox.Show("Error: " + cgeepApi.getErrorCode()); MessageBox.Show("Exception: " + cgeepApi.getException()); return; } MessageBox.Show(list); // // 3) Symmetric encryption & decryption // // encryption String fileIn = TEMP_DIR + "\\text.txt"; String fileOut = TEMP_DIR + "\\text.txt.pgp"; // The ecnrypted file cgeepApi.encryptSymmetric(fileIn, fileOut, "paspshrase", 0); if (!cgeepApi.isOperationOk()) { MessageBox.Show("Error : " + cgeepApi.getErrorCode()); MessageBox.Show("Exception: " + cgeepApi.getException()); return; } // decryption String fileEncrypted = TEMP_DIR + "\\text.txt.pgp"; String fileDecrypted = TEMP_DIR + "\\text_decrypted.txt"; // The decrypted file cgeepApi.decryptSymmetric(fileEncrypted, fileDecrypted, "paspshrase", 0); if (!cgeepApi.isOperationOk()) { MessageBox.Show("Error : " + cgeepApi.getErrorCode()); MessageBox.Show("Exception: " + cgeepApi.getException()); return; } // // 4) Asymmetric encryption & decryption // // We reset the recipients list (Cautious to be done before each encryption) cgeepApi.resetRecipientsKeys(); // We add test1@test.com as first recipient cgeepApi.addRecipientKey("test1@test.com"); // We add test2@test.com as second recipient cgeepApi.addRecipientKey("test2@test.com"); fileIn = TEMP_DIR + "\\text.txt"; fileOut = TEMP_DIR + "\\text.txt.pgp"; // The ecnrypted file // Now we encrypt a file for these two keys / recipients cgeepApi.encrypt(fileIn, fileOut, 0); if (!cgeepApi.isOperationOk()) { MessageBox.Show("Error : " + cgeepApi.getErrorCode()); MessageBox.Show("Exception: " + cgeepApi.getException()); return; } // Now, we try to decrypt the encrypted file. // We can decrypt with test1@test.com, our first private key: cgeepApi.decrypt(fileEncrypted, fileDecrypted, "test1@test.com", "paspshrase", 0); if (!cgeepApi.isOperationOk()) { MessageBox.Show("Error : " + cgeepApi.getErrorCode()); MessageBox.Show("Exception: " + cgeepApi.getException()); return; } // // 5) Symmetric encryption example using a Task Number for // asynchronous processing // We assume the test.txt file is a "big" file. ;-) // String fileClear = TEMP_DIR + "\\text.txt"; String fileEnc = TEMP_DIR + "\\text.txt.pgp"; // Task number is 1 cgeepApi.encryptSymmetric(fileClear, fileEnc, "paspshrase", 1); // The previous line of code is executed in a separated // thread/sub task ==> We a have an immediate release. // Loop while encryption is working in background while (cgeepApi.getPercentProcessedForTask(1) < 100) { int percentProgress = cgeepApi.getPercentProcessedForTask(1); MessageBox.Show("Progress: " + percentProgress + "%"); } // Task is done (or killed) if percentProgress = 100 // We can now check the Error Code if (!cgeepApi.isOperationOkForTask(1)) { MessageBox.Show("Error : " + cgeepApi.getErrorCodeForTask(1)); MessageBox.Show("Exception: " + cgeepApi.getExceptionForTask(1)); return; } // We are done: MessageBox.Show("Done!"); } } }