How To Encrypt/Decrypt using GPG in PowerBuilder

GPG is a powerful tool used by most organizations for Encrypting and Decrypting files and documents. However, when it came to implement this in PowerBuilder, it was a challenge since I did not find any good articles on doing the same. That is when I thought I should write this article on How To Encrypt/Decrypt using GPG in PowerBuilder and share with you all. I do hope you will find it useful for any similar implementations.

Here is how I implemented this functionality.

First I created a new userobject and called it nv_pgp.

Created couple of functions as below:

uf_pgp_encrypt

  1. I have a listbox on a window control which will have a list of the files I require to Encrypt
  2. The code will go through the list and run the Encrypt command for each file.
  3. Encrypt log file will capture all the messages during the process
  4. There is code to wait for a specific period of time to allow encryption to complete for bigger files.

Here is my code for uf_pgp_encrypt

Arguments: window (aw_window)

Returns: String

//Code below:

String ls_ret, ls_pgp_file_name, ls_cmd, ls_output_pgp_file,ls_pgp_batch, ls_pgp_batch_path, ls_file_name, ls_encryption_key
Long ll_count, ll_index, ll_FileNum, li_retcode_pgp, ll_file_write, i, ll_pos, ll_prev
Integer li_move
ListBox lb_files
gs_modulename = ‘nv_pgp.uf_pgp_encrypt’

//We need to Encrypt only the files which have a corresponding .flg file

//First empty the GPGWorking Folder
aw_window.openuserobject(lb_files)
lb_files.DirList(is_gpg_working, 0)
ll_count = lb_files.TotalItems()

FOR ll_index = 1 TO ll_count

If FileExists(is_gpg_working + lb_files.Text(ll_index)) Then
FileDelete(is_gpg_working + lb_files.Text(ll_index))
End If
NEXT

//Now Process the files to be Encrypted
aw_window.openuserobject(lb_files)
lb_files.DirList(gs_app_path+ “\OUT\*.flg”, 0)

ll_count = lb_files.TotalItems()

FOR ll_index = 1 TO ll_count

//Now get the corresponding file without the .flg extension
ls_file_name = lb_files.Text(ll_index)

ls_pgp_file_name = gs_app_path+ “\OUT\” + ls_file_name

ls_output_pgp_file = is_gpg_working + ls_file_name + “.pgp”

//If the file exists in the destination folder, first delete it before Move
If FileExists(ls_output_pgp_file) Then
FileDelete(ls_output_pgp_file)
End If

If Not FileExists(ls_pgp_file_name) Then

//You can create a function of_audit to write to a log file else skip this
of_audit(“File Skipped From Encryption:” + ls_pgp_file_name)
Continue
End If

ls_cmd = ‘ gpg.exe –logger-file “‘ + is_log_filename + ‘” –homedir “C:\gnupg”‘ + ‘ –always-trust –yes -r “‘ + is_encryption_key + ‘” -o “‘ + ls_output_pgp_file + ‘” -e “‘ + ls_pgp_file_name + ‘”‘

//encrypt and sign option – Use this command if you want to SIGN the file which gives an added layer of security
// ls_cmd = ‘gpg.exe –logger-file “‘ + is_log_filename + ‘” –passphrase “‘ + ls_encryption_psw + ‘” -u “‘ + ls_public_key + ‘” -r “‘ + ls_encryption_key + ‘” -o “‘ + ls_output_pgp_file + ‘” -es “‘ + ls_pgp_file_name + ‘”‘

// building batch file
ls_pgp_batch = gs_appname + “_pgp_encrypt.bat”
ls_pgp_batch_path = is_app_path + “log\” + ls_pgp_batch

If fileexists(ls_pgp_batch_path) then
FileDelete(ls_pgp_batch_path)
End IF

of_filewrite(ls_pgp_batch_path, “TITLE ” + ls_pgp_batch + “~n” + ls_cmd)

// Run batch file
li_retcode_pgp = run(ls_pgp_batch_path)

// check returne code
if li_retcode_pgp = -1 or isnull( li_retcode_pgp ) then
of_audit(“Function Uf_pgp_encrypt: Error processing encrypttion ~r~n” + ls_cmd)
return ‘Error’
end if

// check if Encryption process finished Do not run the rest of the script until command done
of_wait_until_done(ls_pgp_batch)

sleep(10)

// Wait upto 15 min
for i = 1 to 900
if FileLength(ls_output_pgp_file) > 0 then
exit
else
sleep(1)
end if
next

if FileLength(ls_output_pgp_file) = 0 then
of_audit(“Function Uf_pgp: Error processing encrypttion. encryption file size = 0 ~r~n” + ls_cmd)
return ‘Error’
end if

NEXT

Return ‘Success’

 

uf_pgp_decrypt

Arguments: window (aw_window)

Returns: String

//Here is the Code:

String ls_ret, ls_pgp_file_name, ls_cmd, ls_output_pgp_file,ls_pgp_batch, ls_pgp_batch_path
Long ll_count, ll_index, ll_FileNum, li_retcode_pgp, ll_file_write, i, ll_pos, ll_prev
ListBox lb_files
aw_window.openuserobject(lb_files)
lb_files.DirList(is_working_dir, 0)

ll_count = lb_files.TotalItems()

If ll_count > 0 then
FOR ll_index = 1 TO ll_count
ls_pgp_file_name = ”
ls_pgp_file_name = is_working_dir + lb_files.Text(ll_index)

End If

ls_output_pgp_file = is_app_path + “in\” + lb_files.Text(ll_index)

//is_pass_txt will be the pass code for decryption
ls_cmd = ‘ gpg.exe –logger-file “‘ + is_log_filename + ‘” –homedir “C:\gnupg”‘ + ‘ –passphrase-fd 0 < “‘ + is_pass_txt + ‘” -o “‘ + ls_output_pgp_file + ‘” –decrypt “‘ + ls_pgp_file_name + ‘”‘

// building batch file
ls_pgp_batch = gs_appname + “_pgp.bat”
ls_pgp_batch_path = is_app_path + “log\” + ls_pgp_batch

If fileexists(ls_pgp_batch_path) then
FileDelete(ls_pgp_batch_path)
End IF

of_filewrite(ls_pgp_batch_path, “TITLE ” + ls_pgp_batch + “~n” + ls_cmd)

// Run batch file
li_retcode_pgp = run(ls_pgp_batch_path)

// check returne code
if li_retcode_pgp = -1 or isnull( li_retcode_pgp ) then
of_audit(“Function Uf_pgp: Error processing encrypttion ~r~n” + ls_cmd)
return ‘Error’
end if

// check if Encryption process finished Do not run the rest of the script until command done
of_wait_until_done(ls_pgp_batch)

sleep(10)

// Wait upto 15 min
for i = 1 to 900
if FileLength(ls_output_pgp_file) > 0 then
exit
else
sleep(1)
end if
next

if FileLength(ls_output_pgp_file) = 0 then
of_audit(“Function Uf_pgp: Error processing encrypttion. encrypttion file size = 0 ~r~n” + ls_cmd)
return ‘Error’
end if

NEXT
End If
Return ‘Success’

of_wait_until_done

This function will be used to wait for the GPG tool to finish the encryption or decryption process.

//here is the code:

Arguments: string (a_wname)

Returns integer

ulong l_handle
long i
string ls_handle
sleep(5)

for i = 1 to 30
l_handle = FindWindowA(0, a_wname)
if isNull(l_handle) then
ls_handle = “Null”
else
ls_handle = string(l_handle)
end if

if l_handle <= 0 then
exit
else
of_audit(“Running Batch file: ” + a_wname)
end if

if i >= 30 then
of_audit(“Batch file ” + a_wname + ” is runnin for more than ” + string(i / 60) + ” minutes”)

exit
end if
sleep(60)
next

return 0

 

The call to these functions will be simple and will pass the parent window as an argument. This window argument is used to build the list box within the function.

I do hope this article helps you to implement the GPG encryption technique in your powerbuilder application. Do let me know if you have any questions or suggestions on the same.

About pbgeeks 11 Articles
PBGeeks is a website for PowerBuilder Professionals to come together and explore the new features being released in PB. Participate in the Forum and exchange your thoughts with the other PB Gurus. Job seekers can find plenty of most recent jobs all around the world in the Jobs Listing section. Employers can post their job openings to be made available to the entire PB community.