Mark
Mark

Reputation: 313

Could not write JSON: Input length must be multiple of 8 when decrypting with padded cipher

I have followed a guide to simply encrypt and decrypt a string but I can't somehow make it work

I want to have a constant key so I don't need to save it to my database and waste space

I just want to encrypt some personal data not password

do you guys have any idea?

I'm following this guide please it

      public String getAction() throws Exception {
            String encodedKey = "eightkey";
            byte[] key = encodedKey.getBytes();
            decodedKey.length, "DES");

            SecretKey myDesKey = new SecretKeySpec(key, "DES");
            Cipher desCipher;
            desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
            byte[] text = action.getBytes();
            byte[] textEncrypted = desCipher.doFinal(text);
            String getAct = ""+textEncrypted;

                return getAct;
        }

        public void setAction(String action) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {

            String encodedKey = "eightkey";
            byte[] key = encodedKey.getBytes();
            SecretKey myDesKey = new SecretKeySpec(key, "DES");
            Cipher desCipher;
            desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            byte[] text = action.getBytes();
            desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
            byte[] textEncrypted = desCipher.doFinal(text);
            String setAct = ""+textEncrypted;
            this.action = setAct;
        }

Full error here

2018-04-12 17:06:34.587  WARN 1572 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Input length must be multiple of 8 when decrypting with padded cipher; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Input length must be multiple of 8 when decrypting with padded cipher (through reference chain: com.capstone.codegum.Codegum.Objects.Logs["action"])

Upvotes: 0

Views: 220

Answers (2)

Amitabha Roy
Amitabha Roy

Reputation: 797

I have modified your code a bit and able to run it. Here is a running example:

Pojo.java

package com.test;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class Pojo {
    private byte[] action = null;
    private SecretKey myDesKey = null;
    private String encodedKey = "eightkey";

    public String getAction() throws Exception {
        Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        desCipher.init(Cipher.DECRYPT_MODE, myDesKey);

        byte[] text = action;
        byte[] textEncrypted = desCipher.doFinal(text);
        String getAct = new String(textEncrypted);

        return getAct;
    }

    public void setAction(String action) throws Exception {
        Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        byte[] key = encodedKey.getBytes();
        this.myDesKey = new SecretKeySpec(key, "DES");
        desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);

        byte[] text = action.getBytes();
        byte[] textEncrypted = desCipher.doFinal(text);
        this.action = textEncrypted;
    }
}

MainClass.java

package com.test;

public class MainClass {

    public static void main(String[] args) throws Exception {
        Pojo p = new Pojo();
        p.setAction("hello");
        String s = p.getAction();
        System.out.println(s);
        p.setAction("world");
        s = p.getAction();
        System.out.println(s);
    }

}

Output:

hello
world

Upvotes: 1

piy26
piy26

Reputation: 1592

Use byte[] actionBytes instead of String action something of the sort:

private byte[] actionBytes;

public String getAction() throws Exception {

    String encodedKey = "eightkey";
    byte[] key = encodedKey.getBytes("UTF8");
    SecretKey myDesKey = new SecretKeySpec(key, "DES");

    Cipher desCipher;
    desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    desCipher.init(Cipher.DECRYPT_MODE, myDesKey);      
    byte[] textEncrypted = desCipher.doFinal(actionBytes);
    return new String(textEncrypted);
}

public void setAction(String action) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {

    String encodedKey = "eightkey";
    byte[] key = encodedKey.getBytes("UTF8");       

    SecretKey myDesKey = new SecretKeySpec(key, "DES");

    Cipher desCipher;
    desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    byte[] text = action.getBytes("UTF8");
    desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
    byte[] textEncrypted = desCipher.doFinal(text);
    actionBytes = textEncrypted;
}

Or if you want to keep using String action then you should do this:

public String action;

public String getAction() throws Exception {

    String encodedKey = "eightkey";
    byte[] key = encodedKey.getBytes("UTF8");
    SecretKey myDesKey = new SecretKeySpec(key, "DES");

    Cipher desCipher;
    desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    desCipher.init(Cipher.DECRYPT_MODE, myDesKey);      
    byte[] textEncrypted = desCipher.doFinal(action.getBytes("UTF8"));
    return new String(textEncrypted);
}

public void setAction(String action) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {

    String encodedKey = "eightkey";
    byte[] key = encodedKey.getBytes("UTF8");       

    SecretKey myDesKey = new SecretKeySpec(key, "DES");

    Cipher desCipher;
    desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    byte[] text = action.getBytes("UTF8");
    desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
    byte[] textEncrypted = desCipher.doFinal(text);
    action = new String(textEncrypted, "UTF8");
}

Upvotes: 0

Related Questions