java - 无法写入 JSON : Input length must be multiple of 8 when decrypting with padded cipher

标签 java encryption des jce

我按照指南简单地加密和解密字符串,但我无法以某种方式使其工作

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

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

你们有什么想法吗?

我正在关注this请指导一下

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

最佳答案

我对你的代码做了一些修改并且能够运行它。这是一个运行示例:

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

关于java - 无法写入 JSON : Input length must be multiple of 8 when decrypting with padded cipher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49792516/

相关文章:

java - EnumSet中枚举的限制数是怎么来的?

java - 如何使用 JSON 动态创建 POJO?

java - 给定 3d 点数组,量化方向的变化

.net - .NET 的 FIPS 兼容密码加密

encryption - OpenPGP 标签 18/19 描述困惑

java - python中的DES无法使用pycrypto获得正确的编码数据

python - 以十六进制形式显示而不是特殊字符

java - 将数据从 Bean 传递到 jsf 页面上的 javascript。谷歌 API 说明

c - 不兼容的 AES 实现?

java - DESFire 身份验证的 DES 发送和接收模式