Java 无法写入JSON:使用填充密码解密时,输入长度必须是8的倍数

Java 无法写入JSON:使用填充密码解密时,输入长度必须是8的倍数,java,encryption,des,jce,Java,Encryption,Des,Jce,我遵循了一个简单的加密和解密字符串的指南,但我无法以某种方式使它工作 我想要一个常量键,这样我就不需要将它保存到数据库中,从而浪费空间 我只想加密一些个人数据,而不是密码 你们知道吗 我在跟着导游走 public String getAction() throws Exception { String encodedKey = "eightkey"; byte[] key = encodedKey.getBytes();

我遵循了一个简单的加密和解密字符串的指南,但我无法以某种方式使它工作

我想要一个常量键,这样我就不需要将它保存到数据库中,从而浪费空间

我只想加密一些个人数据,而不是密码

你们知道吗

我在跟着导游走

      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;
        }
这里是完全错误

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"])

使用byte[]actionBytes而不是字符串action排序:

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;
}
或者,如果要继续使用字符串操作,则应执行以下操作:

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

我已经修改了你的代码一点,并能够运行它。下面是一个运行示例:

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

}
输出:

hello
world

您不应使用
“”+byte[]
将其转换为字符串。Base64编码,即
byte[]
不在新工作中使用DES,它不再被认为是安全的,已被AES(高级加密标准)取代,使用起来不再困难。DES仅有的密钥大小仅为56位,这被认为是不安全的,AES支持128192和256位的密钥大小。请参阅。基本上,问题是您正在创建的加密字符串需要使用UTF8编码创建。byte[]textEncrypted=desCipher.doFinal(action.getBytes(“UTF8”);