From a372161dd559ba5dbc7839898291f1f288235d1f Mon Sep 17 00:00:00 2001 From: Keith Armstrong Date: Thu, 2 Jan 2025 16:10:38 -0500 Subject: [PATCH] Added exception handling for incorrect decryption --- src/com/belkast/soap/salter.java | 21 +++++++------- src/com/belkast/soap/userVerify.java | 3 +- src/com/belkast/soap/webService.java | 42 ++++++++++++++-------------- 3 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/com/belkast/soap/salter.java b/src/com/belkast/soap/salter.java index 268846c..c0bf75c 100644 --- a/src/com/belkast/soap/salter.java +++ b/src/com/belkast/soap/salter.java @@ -1,12 +1,15 @@ package com.belkast.soap; -import javax.crypto.Cipher; -import javax.crypto.SecretKey; -import javax.crypto.SecretKeyFactory; +import javax.crypto.*; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.SecretKeySpec; +import java.io.UnsupportedEncodingException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; +import java.security.spec.InvalidKeySpecException; import java.security.spec.KeySpec; import java.util.Base64; @@ -15,7 +18,7 @@ public class salter { private static final int KEY_LENGTH = 256; private static final int ITERATION_COUNT = 65536; - public static String encrypt(String strToEncrypt, String secretKey, String salt) { + public static String encrypt(String strToEncrypt, String secretKey, String salt) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException, BadPaddingException, InvalidKeyException { try { @@ -39,13 +42,11 @@ public class salter { return Base64.getEncoder().encodeToString(encryptedData); } catch (Exception e) { - // Handle the exception properly - e.printStackTrace(); - return null; + throw e; } } - public static String decrypt(String strToDecrypt, String secretKey, String salt) { + public static String decrypt(String strToDecrypt, String secretKey, String salt) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, UnsupportedEncodingException, BadPaddingException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException { try { if (strToDecrypt == null) @@ -72,9 +73,7 @@ public class salter { byte[] decryptedText = cipher.doFinal(cipherText); return new String(decryptedText, "UTF-8"); } catch (Exception e) { - // Handle the exception properly - e.printStackTrace(); - return null; + throw e; } } } \ No newline at end of file diff --git a/src/com/belkast/soap/userVerify.java b/src/com/belkast/soap/userVerify.java index 0851cfc..e084f11 100644 --- a/src/com/belkast/soap/userVerify.java +++ b/src/com/belkast/soap/userVerify.java @@ -62,8 +62,7 @@ public class userVerify while (it.hasNext()) { Map.Entry pair = it.next(); - System.out.println("record " + varIter + " token : " + pair.getKey()); - System.out.println("record " + varIter + " value : " + pair.getValue()); + System.out.println("record " + varIter + " : [" + pair.getKey() + "] => " + pair.getValue()); } System.out.println(); } diff --git a/src/com/belkast/soap/webService.java b/src/com/belkast/soap/webService.java index 3189a37..4f81175 100644 --- a/src/com/belkast/soap/webService.java +++ b/src/com/belkast/soap/webService.java @@ -3,6 +3,7 @@ package com.belkast.soap; import com.beust.jcommander.ParameterException; import com.beust.jcommander.*; +import javax.crypto.BadPaddingException; import java.io.File; import java.net.HttpURLConnection; import java.text.SimpleDateFormat; @@ -74,17 +75,18 @@ public class webService { System.exit(0); } - doSoap(new File(varProps)); + if (varProps != null) + { + doSoap(new File(varProps)); + } + System.exit(0); } - private static String doSoap(File thisProps) throws Exception { - String varPassword = new String(); + private static void doSoap(File thisProps) throws Exception { + String varPassword = ""; String varDelimiter = propertyProcessor.getProperty(thisProps, "ARGUMENT_DELIMITER"); - debugProcessor.writer(true, "============ START ============"); - - debugProcessor.writer(true, "Start Time: " + varFormat.format(dateStart)); debugProcessor.writer(false, "Debug is set to : " + varDebug); System.out.println(); @@ -121,7 +123,7 @@ public class webService { File checkRole = new File (varRoleFile); if (!checkRole.exists()) { - debugProcessor.writer(true, thisProps + " => The file specified in INPUT_FILE does not exist [ " + varRoleFile + " ]"); + debugProcessor.writer(true, thisProps + " => The file specified in CSV_FILE does not exist [ " + varRoleFile + " ]"); System.exit(0); } @@ -135,7 +137,7 @@ public class webService { ArrayList thisCSV = userVerify.readCSV(varRoleFile, false, !varEmptyColumns); - boolean validfile = (thisCSV.size() > 0); + boolean validfile = (!thisCSV.isEmpty()); debugProcessor.writer(true, varRoleFile + " => CSV file is valid : " + validfile); debugProcessor.writer(true, varRoleFile + " => records to process : " + thisCSV.size()); @@ -148,39 +150,37 @@ public class webService { while (mapper1.hasNext()) { varCount++; debugProcessor.writer(false, ""); - debugProcessor.writer(true, "Processing record " + varCount); + debugProcessor.writer(true, "processing record " + varCount); Map hashMap = mapper1.next(); - debugProcessor.writer(varDebug, "Record " + varCount + " : " + hashMap.values().toString()); + debugProcessor.writer(varDebug, "record " + varCount + " : " + hashMap.values().toString()); Iterator> it = hashMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = it.next(); - debugProcessor.writer(varDebug, "Record " + varCount + " : " + pair.getKey() + " => " + pair.getValue()); + debugProcessor.writer(varDebug, "record " + varCount + " : " + pair.getKey() + " => " + pair.getValue()); } String varDocument = fileProcessor.replaceInFile(varInputFile, hashMap); - if (varDocument.length() > 0) ; + if (varDocument != null && !varDocument.isEmpty()) { String varCredentials = soapProcessor.getCredentials(varUsername, varPassword); Boolean isSSL = Boolean.parseBoolean(varUseSSL); - debugProcessor.writer(varDebug, "Connecting to URL : " + varURL); + debugProcessor.writer(varDebug, "connecting to URL : " + varURL); HttpURLConnection varConn = soapProcessor.getConnection(varCredentials, varURL, isSSL, varKeystore_LOC, varKeystore_PW); - debugProcessor.writer(varDebug, "Sending document : " + varDocument); + debugProcessor.writer(varDebug, "sending document : " + varDocument); String response = soapProcessor.send(varConn, varInputFile, hashMap, varDocument.getBytes()); - debugProcessor.writer(varDebug, "Response received : " + response); - soapProcessor.closeConnection(varConn); + debugProcessor.writer(varDebug, "response received : " + response); + if (varConn != null) + { + soapProcessor.closeConnection(varConn); + } } } - Date dateEnd = new Date(); - System.out.println(); - debugProcessor.writer(true, "Finish Time: " + varFormat.format(dateEnd)); - debugProcessor.writer(true, "============ FINISH ============"); - return new String(); } } \ No newline at end of file