gpt4 book ai didi

java - RSA 解密错误 BadPaddingException

转载 作者:行者123 更新时间:2023-12-02 04:39:58 25 4
gpt4 key购买 nike

我正在使用对称 key (AES)加密文件,然后使用 rsa key 加密 key 。 加密工作正常,但在解密时出现错误: 这是堆栈跟踪: http://pastebin.com/37AB7EPH

我已经尝试了一切,请帮助我,谢谢。

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

最佳答案

Cipher cipher = Cipher.getInstance("RSA");

解密时没有填充 RSA(如 RSA/ECB/NoPadding)。尝试将其更改为与加密中相同的值(“RSA/ECB/PKCS1Padding”)。 RSA 加密值也可以取消填充,如果我不清楚,请原谅我的英语:/

关于java - RSA 解密错误 BadPaddingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30290921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com