首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RSA解密错误BadPaddingException

RSA解密错误BadPaddingException
EN

Stack Overflow用户
提问于 2015-05-18 03:02:49
回答 1查看 970关注 0票数 0

我用对称密钥(AES)加密文件,然后用rsa密钥加密密钥。加密运行良好,但在解密过程中出现错误:堆栈跟踪如下:http://pastebin.com/37AB7EPH

我什么都试过了,请帮帮我谢谢。

代码语言:javascript
复制
@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);     
}

解密对称文件...

代码语言:javascript
复制
@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);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2015-05-31 12:27:14

代码语言:javascript
复制
Cipher cipher = Cipher.getInstance("RSA");

它在解密时没有填充RSA (就像RSA/ECB/NoPadding.尝试将其更改为与加密中相同的值(“RSA/ECB/PKCS1Padding.”)。RSA加密值也可以是无填充的,如果我不清楚,很抱歉我的英语:/

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30290921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档