Added exception handling for incorrect decryption
This commit is contained in:
parent
9b189174b4
commit
a372161dd5
@ -1,12 +1,15 @@
|
|||||||
package com.belkast.soap;
|
package com.belkast.soap;
|
||||||
|
|
||||||
import javax.crypto.Cipher;
|
import javax.crypto.*;
|
||||||
import javax.crypto.SecretKey;
|
|
||||||
import javax.crypto.SecretKeyFactory;
|
|
||||||
import javax.crypto.spec.IvParameterSpec;
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
import javax.crypto.spec.PBEKeySpec;
|
import javax.crypto.spec.PBEKeySpec;
|
||||||
import javax.crypto.spec.SecretKeySpec;
|
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.SecureRandom;
|
||||||
|
import java.security.spec.InvalidKeySpecException;
|
||||||
import java.security.spec.KeySpec;
|
import java.security.spec.KeySpec;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
|
|
||||||
@ -15,7 +18,7 @@ public class salter {
|
|||||||
private static final int KEY_LENGTH = 256;
|
private static final int KEY_LENGTH = 256;
|
||||||
private static final int ITERATION_COUNT = 65536;
|
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 {
|
try {
|
||||||
|
|
||||||
@ -39,13 +42,11 @@ public class salter {
|
|||||||
|
|
||||||
return Base64.getEncoder().encodeToString(encryptedData);
|
return Base64.getEncoder().encodeToString(encryptedData);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// Handle the exception properly
|
throw e;
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
try {
|
||||||
if (strToDecrypt == null)
|
if (strToDecrypt == null)
|
||||||
@ -72,9 +73,7 @@ public class salter {
|
|||||||
byte[] decryptedText = cipher.doFinal(cipherText);
|
byte[] decryptedText = cipher.doFinal(cipherText);
|
||||||
return new String(decryptedText, "UTF-8");
|
return new String(decryptedText, "UTF-8");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// Handle the exception properly
|
throw e;
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -62,8 +62,7 @@ public class userVerify
|
|||||||
while (it.hasNext())
|
while (it.hasNext())
|
||||||
{
|
{
|
||||||
Map.Entry<Integer, Integer> pair = it.next();
|
Map.Entry<Integer, Integer> pair = it.next();
|
||||||
System.out.println("record " + varIter + " token : " + pair.getKey());
|
System.out.println("record " + varIter + " : [" + pair.getKey() + "] => " + pair.getValue());
|
||||||
System.out.println("record " + varIter + " value : " + pair.getValue());
|
|
||||||
}
|
}
|
||||||
System.out.println();
|
System.out.println();
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package com.belkast.soap;
|
|||||||
import com.beust.jcommander.ParameterException;
|
import com.beust.jcommander.ParameterException;
|
||||||
import com.beust.jcommander.*;
|
import com.beust.jcommander.*;
|
||||||
|
|
||||||
|
import javax.crypto.BadPaddingException;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
@ -74,17 +75,18 @@ public class webService {
|
|||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
doSoap(new File(varProps));
|
if (varProps != null)
|
||||||
|
{
|
||||||
|
doSoap(new File(varProps));
|
||||||
|
}
|
||||||
|
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String doSoap(File thisProps) throws Exception {
|
private static void doSoap(File thisProps) throws Exception {
|
||||||
String varPassword = new String();
|
String varPassword = "";
|
||||||
String varDelimiter = propertyProcessor.getProperty(thisProps, "ARGUMENT_DELIMITER");
|
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);
|
debugProcessor.writer(false, "Debug is set to : " + varDebug);
|
||||||
|
|
||||||
System.out.println();
|
System.out.println();
|
||||||
@ -121,7 +123,7 @@ public class webService {
|
|||||||
File checkRole = new File (varRoleFile);
|
File checkRole = new File (varRoleFile);
|
||||||
if (!checkRole.exists())
|
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);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,7 +137,7 @@ public class webService {
|
|||||||
|
|
||||||
ArrayList thisCSV = userVerify.readCSV(varRoleFile, false, !varEmptyColumns);
|
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 + " => CSV file is valid : " + validfile);
|
||||||
debugProcessor.writer(true, varRoleFile + " => records to process : " + thisCSV.size());
|
debugProcessor.writer(true, varRoleFile + " => records to process : " + thisCSV.size());
|
||||||
|
|
||||||
@ -148,39 +150,37 @@ public class webService {
|
|||||||
while (mapper1.hasNext()) {
|
while (mapper1.hasNext()) {
|
||||||
varCount++;
|
varCount++;
|
||||||
debugProcessor.writer(false, "");
|
debugProcessor.writer(false, "");
|
||||||
debugProcessor.writer(true, "Processing record " + varCount);
|
debugProcessor.writer(true, "processing record " + varCount);
|
||||||
|
|
||||||
Map hashMap = mapper1.next();
|
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();
|
Iterator<Map.Entry<Integer, Integer>> it = hashMap.entrySet().iterator();
|
||||||
while (it.hasNext())
|
while (it.hasNext())
|
||||||
{
|
{
|
||||||
Map.Entry<Integer, Integer> pair = it.next();
|
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);
|
String varDocument = fileProcessor.replaceInFile(varInputFile, hashMap);
|
||||||
|
|
||||||
if (varDocument.length() > 0) ;
|
if (varDocument != null && !varDocument.isEmpty())
|
||||||
{
|
{
|
||||||
String varCredentials = soapProcessor.getCredentials(varUsername, varPassword);
|
String varCredentials = soapProcessor.getCredentials(varUsername, varPassword);
|
||||||
Boolean isSSL = Boolean.parseBoolean(varUseSSL);
|
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);
|
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());
|
String response = soapProcessor.send(varConn, varInputFile, hashMap, varDocument.getBytes());
|
||||||
|
|
||||||
debugProcessor.writer(varDebug, "Response received : " + response);
|
debugProcessor.writer(varDebug, "response received : " + response);
|
||||||
soapProcessor.closeConnection(varConn);
|
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user