0

I am encrypting file with symmetric key(AES), and then encrypting key with rsa key. Encrption is working fine but in decryption it gives error: Here is the stacktrace: http://pastebin.com/37AB7EPH

I have tried everything please help me thanks.

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

Decrypt Asmmetric File...

@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);
    }
}
1
  • What did you check? Symmetric key values? Input / output of your cipher? Note that SO is not a community debugger. Commented May 17, 2015 at 19:43

1 Answer 1

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

It is no padded RSA in decryption (like RSA/ECB/NoPadding). Try to change it to the same value as in encryption ("RSA/ECB/PKCS1Padding"). RSA encrypted values can be also unpadded, sorry for my english if I am unclear :/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.