'CgeepApiExample.Cls 'Copyright (c) SafeLogic, 2000 - 2009 ' 'Last Updates: ' 19 oct. 2009 20:42:14 Nicolas de Pomereu
Public Sub Example() ' 1) Load cGeep API Instance Dim cgeepApi As New cgeepApi ' 1) Set the keyring in c:\temp Dim TEMP_DIR As String TEMP_DIR = "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 Call cgeepApi.generateKeyPair("test1@test.com", "passphrase", _ "RSA", 1024, "AES-256", "NEVER") If (cgeepApi.isOperationOk) Then MsgBox ("Success!") Else MsgBox ("Failure!") MsgBox ("Error Code: " & cgeepApi.getErrorCode()) Exit Sub End If ' Create a key with test1@test.com as PGP UserId Call cgeepApi.generateKeyPair("test1@test.com", "passphrase", _ "RSA", 1024, "AES-256", "NEVER") If (cgeepApi.isOperationOk) Then MsgBox ("Success!") Else MsgBox ("Failure!") MsgBox ("Error Code: " & cgeepApi.getErrorCode()) Exit Sub End If 'We display the key list Dim list As String list = cgeepApi.listKeys("") ' ' 3) Symmetric encryption & decryption ' ' encryption Dim fileIn As String Dim fileOut As String fileIn = TEMP_DIR & "\\text.txt" fileOut = TEMP_DIR & "\\text.txt.pgp" Call cgeepApi.encryptSymmetric(fileIn, fileOut, "passphrase", 0) If (cgeepApi.isOperationOk) Then MsgBox ("Success!") Else MsgBox ("Failure!") MsgBox ("Error Code: " & cgeepApi.getErrorCode()) Exit Sub End If ' decryption Dim fileEncrypted As String Dim fileDecrypted As String fileEncrypted = TEMP_DIR & "\\text.txt.pgp" fileDecrypted = TEMP_DIR & "\\text_1.txt" Call cgeepApi.decryptSymmetric(fileEncrypted, fileDecrypted, "passphrase", 0) If (cgeepApi.isOperationOk) Then MsgBox ("Success!") Else MsgBox ("Failure!") MsgBox ("Error Code: " & cgeepApi.getErrorCode()) Exit Sub End If ' ' 4) Asymmetric encryption & decryption ' ' We reset the recipients list (Cautious to be done before each encryption) Call 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 Call cgeepApi.encrypt(fileIn, fileOut, 0) If (cgeepApi.isOperationOk) Then MsgBox ("Success!") Else MsgBox ("Failure!") MsgBox ("Error Code: " & cgeepApi.getErrorCode()) Exit Sub End If ' Now, we try to decrypt the encrypted file. ' We can decrypt with test1@test.com, our first private key: Call cgeepApi.decrypt(fileEncrypted, fileDecrypted, "test1@test.com", "paspshrase", 0) If (cgeepApi.isOperationOk) Then MsgBox ("Success!") Else MsgBox ("Failure!") MsgBox ("Error Code: " & cgeepApi.getErrorCode()) Exit Sub End If ' ' 5) Symmetric encryption example using a Task Number for ' asynchronous processing ' We assume the test.txt file is a "big" file. -) ' Dim fileClear As String Dim fileEnc As String fileClear = TEMP_DIR & "\\text.txt" fileEnc = TEMP_DIR & "\\text.txt.pgp" ' Task number is 1 Call 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. Dim percentProgress As Integer ' Loop while encryption is working in background While (cgeepApi.getPercentProcessedForTask(1) < 100) percentProgress = cgeepApi.getPercentProcessedForTask(1) MsgBox "Progress: " & percentProgress & "%" Wend ' Task is done (or killed) if percentProgress = 100 ' We can now check the Error Code If (cgeepApi.isOperationOkForTask(1) = False) Then MsgBox ("Failure!") MsgBox ("Error Code: " & cgeepApi.getErrorCodeForTask(1)) MsgBox ("Exception : " & cgeepApi.getExceptionForTask(1)) Exit Sub End If ' We are done: System.out.println ("Done!") End Sub