1 import java.io.File; 2 3 import com.safelogic.pgp.api.toolkit.CgeepApi; 4 5 //CgeepApiExample.java 6 //Copyright (c) SafeLogic, 2000 - 2009 7 // 8 //Last Updates: 9 // 28 oct. 2009 17:55 Nicolas de Pomereu 10 11 /** 12 * @author Nicolas de Pomereu 13 * 14 * Example of cGeep API calls in Java 15 */ 16 17 public class CgeepApiExample 18 { 19 /** We store the name of our temporary directory */ 20 private static String TEMP_DIR = null; 21 22 /** 23 * @param args 24 */ 25 public static void main(String[] args) throws Exception 26 { 27 String osName = System.getProperty("os.name"); 28 if (osName.toLowerCase().contains("windows")) 29 { 30 TEMP_DIR = "c:\\temp"; 31 } 32 else 33 { 34 TEMP_DIR = "/tmp"; 35 } 36 37 // Init: create the c:\temp dir & clean previous instances of keyring 38 File fileTempDir = new File(TEMP_DIR); 39 fileTempDir.mkdir(); 40 new File(TEMP_DIR + File.separator + "secring.skr").delete(); 41 new File(TEMP_DIR + File.separator + "pubring.pkr").delete(); 42 43 // Create cGeep API instance 44 CgeepApi cgeepApi = new CgeepApi(); 45 46 // 1) Set the keyring in c:\temp 47 cgeepApi.setKeyRingDirectory(TEMP_DIR); 48 49 // 2) Create two key pairs 50 // (it will generate a private key ring & a public key ring in c:\\temp) 51 52 // Create a key with test1@test.com as PGP UserId 53 cgeepApi.generateKeyPair("test1@test.com", "passphrase".toCharArray(), 54 "RSA", 1024, "AES-256", "NEVER"); 55 56 if (!cgeepApi.isOperationOk()) 57 { 58 System.out.println("Error : " + cgeepApi.getErrorCode()); 59 System.out.println("Exception: " + cgeepApi.getException()); 60 return; 61 } 62 63 // Create a key with test2@test.com as PGP UserId 64 cgeepApi.generateKeyPair("test2@test.com", "passphrase".toCharArray(), 65 "RSA", 1024, "AES-256", "NEVER"); 66 67 if (!cgeepApi.isOperationOk()) 68 { 69 System.out.println("Error : " + cgeepApi.getErrorCode()); 70 System.out.println("Exception: " + cgeepApi.getException()); 71 return; 72 } 73 74 // We display the key list 75 String list = cgeepApi.listKeys(""); 76 77 if(!cgeepApi.isOperationOk()) 78 { 79 System.out.println("Error: " + cgeepApi.getErrorCode()); 80 System.out.println("Exception: " + cgeepApi.getException()); 81 return; 82 } 83 84 System.out.println(list); 85 86 // 87 // 3) Symmetric encryption & decryption 88 // 89 90 // encryption 91 String fileIn = TEMP_DIR + File.separator + "text.txt"; 92 String fileOut = TEMP_DIR + File.separator + "text.txt.pgp"; // The ecnrypted file 93 cgeepApi.encryptSymmetric(fileIn, fileOut, "paspshrase".toCharArray(), 0); 94 95 if (!cgeepApi.isOperationOk()) 96 { 97 System.out.println("Error : " + cgeepApi.getErrorCode()); 98 System.out.println("Exception: " + cgeepApi.getException()); 99 return; 100 } 101 102 // decryption 103 String fileEncrypted = TEMP_DIR + File.separator + "text.txt.pgp"; 104 String fileDecrypted = TEMP_DIR + File.separator + "text_decrypted.txt"; // The decrypted file 105 cgeepApi.decryptSymmetric(fileEncrypted, fileDecrypted, "paspshrase".toCharArray(), 0); 106 107 if (!cgeepApi.isOperationOk()) 108 { 109 System.out.println("Error : " + cgeepApi.getErrorCode()); 110 System.out.println("Exception: " + cgeepApi.getException()); 111 return; 112 } 113 114 // 115 // 4) Asymmetric encryption & decryption 116 // 117 118 // We reset the recipients list (Cautious to be done before each encryption) 119 cgeepApi.resetRecipientsKeys(); 120 121 // We add test1@test.com as first recipient 122 cgeepApi.addRecipientKey("test1@test.com"); 123 124 // We add test2@test.com as second recipient 125 cgeepApi.addRecipientKey("test2@test.com"); 126 127 fileIn = TEMP_DIR + File.separator + "text.txt"; 128 fileOut = TEMP_DIR + File.separator + "text.txt.pgp"; // The ecnrypted file 129 130 // Now we encrypt a file for these two keys / recipients 131 cgeepApi.encrypt(fileIn, fileOut, 0); 132 133 if (!cgeepApi.isOperationOk()) 134 { 135 System.out.println("Error : " + cgeepApi.getErrorCode()); 136 System.out.println("Exception: " + cgeepApi.getException()); 137 return; 138 } 139 140 // Now, we try to decrypt the encrypted file. 141 // We can decrypt with test1@test.com, our first private key: 142 143 cgeepApi.decrypt(fileEncrypted, fileDecrypted, "test1@test.com", "paspshrase".toCharArray(), 0); 144 145 if (!cgeepApi.isOperationOk()) 146 { 147 System.out.println("Error : " + cgeepApi.getErrorCode()); 148 System.out.println("Exception: " + cgeepApi.getException()); 149 return; 150 } 151 152 // 153 // 5) Symmetric encryption example using a Task Number for 154 // asynchronous processing 155 // We assume the test.txt file is a "big" file. ;-) 156 // 157 158 String fileClear = TEMP_DIR + File.separator + "text.txt"; 159 String fileEnc = TEMP_DIR + File.separator + "text.txt.pgp"; 160 161 // Task number is 1 162 cgeepApi.encryptSymmetric(fileClear, 163 fileEnc, 164 "paspshrase".toCharArray(), 165 1); 166 167 // The previous line of code is executed in a separated 168 // thread/sub task ==> We a have an immediate release. 169 170 // Loop while encryption is working in background 171 while (cgeepApi.getPercentProcessedForTask(1) < 100) 172 { 173 Thread.sleep(1000); // Sleep One second 174 175 int percentProgress = cgeepApi.getPercentProcessedForTask(1); 176 System.out.println("Progress: " + percentProgress + "%"); 177 } 178 179 // Task is done (or killed) if percentProgress = 100 180 // We can now check the Error Code 181 if (!cgeepApi.isOperationOkForTask(1)) 182 { 183 System.out.println("Error : " 184 + cgeepApi.getErrorCodeForTask(1)); 185 System.out.println("Exception: " 186 + cgeepApi.getExceptionForTask(1)); 187 return; 188 } 189 190 // We are done: 191 System.out.println("Done!"); 192 } 193 194 }