为什么解密时会出现错误的填充错误,为什么还会显示缺少的参数? 我已经编码了加密和解密方法,但是对于zip文件,这些zip文件是通过加密从服务器下载的,但是它不执行解密过程并且卡在以下错误中,我的所有密钥都位于正确的位置。请请按照我的以下代码作为参考,如果我需要任何其他实体,请还原。
DecryptUtil.java
public class DecryptUtil {
private final static Logger LOGGER = LoggerFactory.getLogger(DecryptUtil.class);
public static byte[] decodeFile(byte[] key, byte[] fileData) {
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
byte[] decrypted = null;
try {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
decrypted = cipher.doFinal(fileData);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException | BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch(Exception e){
// for all other exception
e.printStackTrace();
}
return decrypted;
}
public static byte[] decryptSecKeyToBytes(byte[] secretKey, PrivateKey privateKey) {
byte[] secretKeyBytesDecrypted = null;
try {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey );
secretKeyBytesDecrypted = cipher.doFinal(secretKey);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException | BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch(Exception e){
// for all other exception
e.printStackTrace();
}
return secretKeyBytesDecrypted;
}
public static PrivateKey loadPrivateKey() {
File filePrivateKey = new File(new CommonUtils().getKeyInputDirAbsolute() + File.separator + UEConstants.PRIVATE_KEY);
PrivateKey privateKey = null;
try {
FileInputStream fis = new FileInputStream(filePrivateKey);
DataInputStream dis = new DataInputStream(fis);
byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()];
dis.readFully(encodedPrivateKey);
dis.close();
fis.close();
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
privateKey = keyFactory.generatePrivate(privateKeySpec);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch(Exception e){
// for all other exception
e.printStackTrace();
}
return privateKey;
}
public static boolean decryptResource(String encryptedSource, String decryptedDestination){
try {
byte[] secretKeyDecrypted = loadSecretKey();
if(secretKeyDecrypted == null){
LOGGER.error("Resource decryption failed as Secret key returns null");
return false;
}
LOGGER.debug("Decrypting source file");
File encryptedSourceFile = new File(encryptedSource);
byte[] bytesArray = new byte[(int) encryptedSourceFile.length()];
FileInputStream fis = new FileInputStream(encryptedSourceFile);
fis.read(bytesArray);
fis.close();
byte[] fileBytes = DecryptUtil.decodeFile(secretKeyDecrypted, bytesArray);
File decryptedDestinationFile = new File(decryptedDestination);
if(!FileUtil.createFilrOrDir(decryptedDestinationFile)){
LOGGER.error("Resource Decryption failed as Invalid Source file path : " + decryptedDestination);
return false;
}
FileOutputStream fos = new FileOutputStream(decryptedDestinationFile);
fos.write(fileBytes);
fos.close();
} catch (Exception e) {
LOGGER.error("Resource Decryption failed ", e);
return false;
}
return true;
}
private static byte[] loadSecretKey() {
byte[] secretKeyDecrypted = null;
try {
LOGGER.debug("Loading SECURE key");
File key = new File(new CommonUtils().getKeyInputDirAbsolute() + File.separator + UEConstants.SECRET_KEY);
FileInputStream readStream = new FileInputStream(key);
DataInputStream dis = new DataInputStream(readStream);
byte secretKeyBytes[] = new byte[(int) key.length()];
dis.readFully(secretKeyBytes);
dis.close();
readStream.close();
LOGGER.debug("Decrypting SECURE key");
secretKeyDecrypted = DecryptUtil.decryptSecKeyToBytes(secretKeyBytes, DecryptUtil.loadPrivateKey());
} catch (IOException e) {
LOGGER.error("Secret Key Load failed ", e);
}
return secretKeyDecrypted;
}
}
错误日志
2018-09-25 14:43:06.556 INFO 8248 --- [ taskExecutor-1] c.userexperior.jobs.service.BatchHelper : job_id: null, job_status: IN_PROGRESS, message: Step 1 Section 1 started , user_task_id: null
2018-09-25 14:43:06.569 INFO 8248 --- [ taskExecutor-1] c.userexperior.jobs.service.BatchHelper : job_id: 224, job_status: S3_DOWNLOAD, message: Download zip from s3 : ue testproj-2/ue testproj_version-2/Task-2/1537866670618_35ec4eff226ebf7d_newer_sdk.zip to:B:/Users/amitk/public/ue/resources/zip/input/1537866670618_35ec4eff226ebf7d_newer_sdk.zip, user_task_id: 0
Tue Sep 25 14:43:06 IST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
2018-09-25 14:43:08.552 INFO 8248 --- [ taskExecutor-1] c.userexperior.jobs.service.BatchHelper : job_id: 224, job_status: ZIP_DECRYPTION, message: Zip decryption started, user_task_id: 0
javax.crypto.BadPaddingException: Decryption error
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:383)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:294)
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
at javax.crypto.Cipher.doFinal(Cipher.java:2164)
at com.userexperior.util.DecryptUtil.decryptSecKeyToBytes(DecryptUtil.java:60)
at com.userexperior.batchFramework.configuration.CustomItemProcessor.decryptZip(CustomItemProcessor.java:156)
at com.userexperior.batchFramework.configuration.CustomItemProcessor.process(CustomItemProcessor.java:97)
at com.userexperior.batchFramework.configuration.CustomItemProcessor.process(CustomItemProcessor.java:34)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.doProcess(SimpleChunkProcessor.java:126)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.transform(SimpleChunkProcessor.java:293)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:192)
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:75)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:271)
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81)
at org.springframework.batch.repeat.support.TaskExecutorRepeatTemplate$ExecutingRunnable.run(TaskExecutorRepeatTemplate.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
2018-09-25 14:43:08.656 INFO 8248 --- [ Thread-4] com.userexperior.util.FileUtil : File is deleted : B:\Users\amitk\public\ue\resources\zip\input\1537866670618_35ec4eff226ebf7d_newer_sdk.zip
2018-09-25 14:43:08.656 INFO 8248 --- [ Thread-4] c.userexperior.jobs.service.BatchHelper : job_id: 224 ZipFolder clean up done.
2018-09-25 14:43:08.664 ERROR 8248 --- [ taskExecutor-1] c.userexperior.jobs.service.BatchHelper : job_id: 224, job_status: ZIP_DECRYPTION, message: Step1 Section 1 failed due to Missing argument, version_key: ed8a31a5-c5b2-4f99-be2d-c636dc8c5c8e, task_id: 2, user_task_id: 0
java.lang.IllegalArgumentException: Missing argument
at javax.crypto.spec.SecretKeySpec.<init>(SecretKeySpec.java:93) ~[na:1.8.0_171]
at com.userexperior.util.DecryptUtil.decodeFile(DecryptUtil.java:33) ~[classes/:na]
at com.userexperior.batchFramework.configuration.CustomItemProcessor.decryptZip(CustomItemProcessor.java:163) [classes/:na]
at com.userexperior.batchFramework.configuration.CustomItemProcessor.process(CustomItemProcessor.java:97) [classes/:na]
at com.userexperior.batchFramework.configuration.CustomItemProcessor.process(CustomItemProcessor.java:34) [classes/:na]
at org.springframework.batch.core.step.item.SimpleChunkProcessor.doProcess(SimpleChunkProcessor.java:126) [spring-batch-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.batch.core.step.item.SimpleChunkProcessor.transform(SimpleChunkProcessor.java:293) [spring-batch-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:192) [spring-batch-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:75) [spring-batch-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406) [spring-batch-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330) [spring-batch-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133) [spring-tx-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:271) [spring-batch-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81) [spring-batch-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.batch.repeat.support.TaskExecutorRepeatTemplate$ExecutingRunnable.run(TaskExecutorRepeatTemplate.java:262) [spring-batch-infrastructure-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_181]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]
2018-09-25 14:43:08.951 INFO 8248 --- [ taskExecutor-1] c.userexperior.jobs.service.BatchHelper : job_id: null, job_status: IN_PROGRESS, message: Step 1 Section 1 started , user_task_id: null
2018-09-25 14:43:08.951 INFO 8248 --- [ taskExecutor-1] c.userexperior.jobs.service.BatchHelper : job_id: 225, job_status: S3_DOWNLOAD, message: Download zip from s3 : ue testproj-2/ue testproj_version-2/Task-2/1537866693171_35ec4eff226ebf7d_newer_sdk.zip to:B:/Users/amitk/public/ue/resources/zip/input/1537866693171_35ec4eff226ebf7d_newer_sdk.zip, user_task_id: 0
2018-09-25 14:43:09.306 INFO 8248 --- [ taskExecutor-1] c.userexperior.jobs.service.BatchHelper : job_id: 225, job_status: ZIP_DECRYPTION, message: Zip decryption started, user_task_id: 0
javax.crypto.BadPaddingException: Decryption error
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:383)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:294)
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
at javax.crypto.Cipher.doFinal(Cipher.java:2164)
at com.userexperior.util.DecryptUtil.decryptSecKeyToBytes(DecryptUtil.java:60)
at com.userexperior.batchFramework.configuration.CustomItemProcessor.decryptZip(CustomItemProcessor.java:156)
at com.userexperior.batchFramework.configuration.CustomItemProcessor.process(CustomItemProcessor.java:97)
at com.userexperior.batchFramework.configuration.CustomItemProcessor.process(CustomItemProcessor.java:34)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.doProcess(SimpleChunkProcessor.java:126)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.transform(SimpleChunkProcessor.java:293)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:192)
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:75)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:271)
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81)
at org.springframework.batch.repeat.support.TaskExecutorRepeatTemplate$ExecutingRunnable.run(TaskExecutorRepeatTemplate.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
2018-09-25 14:43:09.324 ERROR 8248 --- [ taskExecutor-1] c.userexperior.jobs.service.BatchHelper : job_id: 225, job_status: ZIP_DECRYPTION, message: Step1 Section 1 failed due to Missing argument, version_key: ed8a31a5-c5b2-4f99-be2d-c636dc8c5c8e, task_id: 2, user_task_id: 0
java.lang.IllegalArgumentException: Missing argument
at javax.crypto.spec.SecretKeySpec.<init>(SecretKeySpec.java:93) ~[na:1.8.0_171]
at com.userexperior.util.DecryptUtil.decodeFile(DecryptUtil.java:33) ~[classes/:na]
at com.userexperior.batchFramework.configuration.CustomItemProcessor.decryptZip(CustomItemProcessor.java:163) [classes/:na]
at com.userexperior.batchFramework.configuration.CustomItemProcessor.process(CustomItemProcessor.java:97) [classes/:na]
at com.userexperior.batchFramework.configuration.CustomItemProcessor.process(CustomItemProcessor.java:34) [classes/:na]
at org.springframework.batch.core.step.item.SimpleChunkProcessor.doProcess(SimpleChunkProcessor.java:126) [spring-batch-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.batch.core.step.item.SimpleChunkProcessor.transform(SimpleChunkProcessor.java:293) [spring-batch-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:192) [spring-batch-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:75) [spring-batch-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406) [spring-batch-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330) [spring-batch-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133) [spring-tx-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:271) [spring-batch-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81) [spring-batch-core-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at org.springframework.batch.repeat.support.TaskExecutorRepeatTemplate$ExecutingRunnable.run(TaskExecutorRepeatTemplate.java:262) [spring-batch-infrastructure-3.0.5.RELEASE.jar:3.0.5.RELEASE]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_181]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]