Java RSA解密错误BadPaddingException
我用对称密钥(AES)加密文件,然后用rsa密钥加密密钥。 加密工作正常,但在解密过程中会出现错误: 以下是stacktrace: 我什么都试过了,请帮帮我,谢谢Java RSA解密错误BadPaddingException,java,spring,encryption,rsa,public-key-encryption,Java,Spring,Encryption,Rsa,Public Key Encryption,我用对称密钥(AES)加密文件,然后用rsa密钥加密密钥。 加密工作正常,但在解密过程中会出现错误: 以下是stacktrace: 我什么都试过了,请帮帮我,谢谢 @RequestMapping(value= "/{userId}/uploadresource/{userEmail:.*}", method = RequestMethod.POST ) @ResponseBody public void GetResourcesByUser(@PathVariable("userId") in
@RequestMapping(value= "/{userId}/uploadresource/{userEmail:.*}", method = RequestMethod.POST )
@ResponseBody
public void GetResourcesByUser(@PathVariable("userId") int UserId, @PathVariable("userEmail") String userEmail, HttpServletRequest request, @RequestParam MultipartFile file ) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException{
Users recieverUser =userService.GetUserByEmail(userEmail);
Users senderUser= userService.getUserById(UserId);
int receiverUserId = recieverUser.getUser_id();
Profile receiverProfile = userService.getUserProfile(receiverUserId);
byte[] receiverPublicKey=receiverProfile.getPublicKey();
PublicKey testPubKey=X509CertificateGenerator.encodedByteToPublicKey(receiverPublicKey);
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(192); // for example
SecretKey secretKey = keyGen.generateKey();
byte[] secretKeyEncoded= secretKey.getEncoded();
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] cipherData = cipher.doFinal(file.getBytes());
Cipher cipher1 = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher1.init(Cipher.ENCRYPT_MODE, testPubKey);
byte[] aesKeyEncryptedBytes = cipher.doFinal(secretKeyEncoded);
String senderUserName= senderUser.getUser_email();
AsymetricSharing sharing= new AsymetricSharing();
sharing.setReceiverId(receiverUserId);
sharing.setResourceFile(cipherData);
sharing.setResourceName(file.getOriginalFilename());
sharing.setSenderId(senderUser.getUser_id());
sharing.setSenderName(senderUserName);
sharing.setSymetricKey(aesKeyEncryptedBytes);
resourseService.uploadAsymmetricResource(sharing);
//resources=this.resourseService.GetResourcesInGroup(group_id);
}
解密Asmmetric文件
@RequestMapping(value="/{userId}/downloadfile/{sharingId}", method = RequestMethod.GET, produces="application/json")
public ResponseEntity<?> downloadAsymmetricFile(@PathVariable("sharingId") int sharingId, @PathVariable("userId") int userId, HttpServletResponse response) throws IOException, SQLException, InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
AsymetricSharing file= resourseService.getFile(sharingId);
if(file!=null){
Profile receiverProfile= userService.getUserProfile(userId);
byte [] receiverPrivateKey=receiverProfile.getPrivateKey();
PrivateKey testPvtKey=Converter.encodedByteToKey(receiverPrivateKey);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, testPvtKey);
byte[] symetricKeyBytes = cipher.doFinal(file.getSymetricKey());
SecretKey symetricKey = new SecretKeySpec(symetricKeyBytes, "AES");
Cipher cipher1 = Cipher.getInstance("AES");
cipher1.init(Cipher.DECRYPT_MODE, symetricKey);
byte[] plainText = cipher.doFinal(file.getResourceFile());
response.setContentLength(plainText.length);
response.setHeader("Content-Disposition","attachment; filename=\"" + file.getResourceName() +"\"");
FileCopyUtils.copy(plainText, response.getOutputStream());
return new ResponseEntity<>(file, HttpStatus.OK);
}
else
{
//if no entity present against id, return not found and bad request Http status.
return new ResponseEntity<>("Not found", HttpStatus.BAD_REQUEST);
}
}
@RequestMapping(value=“/{userId}/downloadfile/{sharingId}”,method=RequestMethod.GET,products=“application/json”)
public ResponseEntity downloadAsymmetricFile(@PathVariable(“sharingId”)int-sharingId,@PathVariable(“userId”)int-userId,HttpServletResponse-response)引发IOException、SQLException、InvalidKeyException、InvalidGorithmParameterException、NoSuchAlgorithmException、NoSuchProviderException、NoSuchPaddingException、IllegalBlockSizeException、,BadPaddingException,InvalidKeySpecException{
AsymetricSharing file=resourceservice.getFile(sharingId);
如果(file!=null){
Profile receiverProfile=userService.getUserProfile(userId);
字节[]receiverPrivateKey=receiverProfile.getPrivateKey();
PrivateKey testPvtKey=Converter.encodedByteToKey(receiverPrivateKey);
Cipher Cipher=Cipher.getInstance(“RSA”);
cipher.init(cipher.DECRYPT_模式,testPvtKey);
字节[]symetricKeyBytes=cipher.doFinal(file.getSymetricKey());
SecretKey symetricKey=新的SecretKey规范(symetricKeyBytes,“AES”);
Cipher cipher1=Cipher.getInstance(“AES”);
cipher1.init(Cipher.DECRYPT_模式,symetricKey);
字节[]明文=cipher.doFinal(file.getResourceFile());
response.setContentLength(纯文本.length);
response.setHeader(“内容处置”、“附件;文件名=\”+文件.getResourceName()+”\”);
copyUtils.copy(纯文本,response.getOutputStream());
返回新的响应属性(文件,HttpStatus.OK);
}
其他的
{
//如果id中没有实体,则返回not found and bad request Http状态。
返回新的响应属性(“未找到”,HttpStatus.BAD_请求);
}
}
它在解密中没有填充RSA(如RSA/ECB/NoPadding)。尝试将其更改为与加密中相同的值(“RSA/ECB/PKCS1Padding”)。RSA加密值也可以取消添加,如果我不清楚,请原谅我的英语:/您检查了什么?对称键值?密码的输入/输出?请注意,SO不是社区调试器。
Cipher cipher = Cipher.getInstance("RSA");