javax.crypto.IllegalBlockSizeException: last block incomplete in decryption exception

前端 未结 2 1957
甜味超标
甜味超标 2021-02-10 06:22

I am trying to decrypt a string in android. I keep getting the following exception:

08-21 03:56:56.700: W/System.err(4208): javax.crypto.IllegalBlockSizeExceptio         


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-02-10 06:45

    I got the solution.Following is new code that worked for me

    // Encryption
    public  String encrypt(String message) throws Exception
    {
        String message1=Base64.encodeBytes(message.getBytes(),Base64.NO_OPTIONS);
        String salt = SharedVariables.globalContext.getString(R.string.EncryptionKey);
        SecretKeySpec key = new SecretKeySpec(salt.getBytes(), "AES");
        Cipher c = Cipher.getInstance("AES");
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(message1.getBytes());
        String encrypted=Base64.encodeToString(encVal, Base64.NO_OPTIONS);
        return encrypted;
    }
    
    //Decryption    
    public String decrypt(String message) throws Exception
    {
        String salt = SharedVariables.globalContext.getString(R.string.EncryptionKey);
        Cipher c = Cipher.getInstance("AES");
        SecretKeySpec key = new SecretKeySpec(salt.getBytes(), "AES");
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = Base64.decode(message.getBytes(), Base64.NO_OPTIONS);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        String decoded=new String(Base64.decode(decryptedValue,Base64.NO_OPTIONS));
        return decoded;
    }
    

提交回复
热议问题