Added exception handling for incorrect decryption

This commit is contained in:
Keith Armstrong 2025-01-02 16:10:38 -05:00
parent 9b189174b4
commit a372161dd5
3 changed files with 32 additions and 34 deletions

View File

@ -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;
}
}
}

View File

@ -62,8 +62,7 @@ public class userVerify
while (it.hasNext())
{
Map.Entry<Integer, Integer> 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();
}

View File

@ -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<Map.Entry<Integer, Integer>> it = hashMap.entrySet().iterator();
while (it.hasNext())
{
Map.Entry<Integer, Integer> 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();
}
}