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

时间:2018-04-12 09:11:04

标签: java encryption des jce

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

我想要一个恒定的密钥,所以我不需要将它保存到我的数据库和浪费空间

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

你们有什么想法吗?

我关注this指南,请

map.entrySet().stream().sorted(Comparator.comparing(entry -> entry.getValue().param1)
                                                         .thenComparing(entry -> entry.getValue().param2)
                                                         .thenComparing(entry -> entry.getValue().param3))
        .collect(Collectors.toList());

完全错误

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

2 个答案:

答案 0 :(得分:1)

我已经修改了你的代码并且能够运行它。这是一个运行的例子:

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

答案 1 :(得分:0)

使用byte [] actionBytes而不是String动作类似的东西:

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

或者如果你想继续使用String动作,那么你应该这样做:

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");
}
相关问题