DES解密问题:通过密钥、偏移量加密正常,解密出错?

DES解密问题:通过密钥、偏移量加密正常,解密出错:Given final block not properly padded.那位大神遇到过这种问题。在线等。

问题

package com.whiteink.pms.util;


import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;
import java.util.Base64;

public class DESUtil {
    static String key = "693207DB8148FBC9D8410179";
    static String iv = "20190225";
    static String encoding = "gb2312";
    static String data = "{\"parkCode\":\"1\",\"plateNo\":\"京A11113\"}";
    /**
     * <一句话功能描述>:加密
     * <功能详细描述>: 将明文加密
     *
     * @Param: key 密钥
     * @Param keyiv 自定义偏移量(8字节)
     * @Param data  明文
     * @Return: byte[]
     */
    public static String des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(keyiv);
        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
        return Base64.getEncoder().encodeToString(cipher.doFinal(data));
    }

    /**
     * <一句话功能描述>:解密
     * <功能详细描述>: 将密文转成明文
     *
     * @Param: key 密钥
     * @Param keyiv 偏移量
     * @Param data 明文
     * @Return: byte[]
     */
    public static String des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception {
        System.out.println(new String(data));
        DESedeKeySpec dks = new DESedeKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("desede");
        SecretKey securekey = keyFactory.generateSecret(dks);
        // 解密
        Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
        IvParameterSpec ivp = new IvParameterSpec(keyiv);
        cipher.init(Cipher.DECRYPT_MODE, securekey, ivp);
        return Base64.getEncoder().encodeToString(cipher.doFinal(data));
    }


    public static void main(String[] args) throws Exception{

        String data = "{\"parkCode\":\"1\",\"plateNo\":\"京A11113\"}";
        String mw = des3EncodeCBC(key.getBytes(), iv.getBytes(), data.getBytes("GB2312"));

        System.out.println(des3DecodeCBC(key.getBytes(), iv.getBytes(), mw.getBytes()));
    }
}

解决方案

===============解决方案===============

package com.whiteink.pms.util;


import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class DESUtil {
    private static String encoding = "gb2312";
    private static String data = "{\"parkCode\":\"1\",\"plateNo\":\"京A11113\"}";


    public static String encrypt(String key, String iv, String data) throws Exception {
        SecretKey secretKey = new SecretKeySpec(key.getBytes(), "desede");

        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ips);
        byte[] encryptData = cipher.doFinal(data.getBytes(encoding));
        return Base64.getEncoder().encodeToString(encryptData);
    }

    public static void main(String[] args) throws Exception {
        String key = "693207DB8148FBC9D8410179";
         String iv = "20190225";
        String encryption = encrypt(key, iv, data);
        System.out.println(encryption);
        String decryption = decrypt(key, iv, encryption);
        System.out.println(decryption);
    }

    public static String decrypt(String key, String iv, String data) throws Exception {
        SecretKey secretKey = new SecretKeySpec(key.getBytes(), "desede");
        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ips);

        byte[] decryptData = cipher.doFinal(Base64.getDecoder().decode(data.getBytes(encoding)));
        return new String(decryptData, "GB2312");
    }
}

1个回答

看看这个能不能帮你解决,3个文章,https://blog.51cto.com/boytnt/1350441

WeiHao0240
Jack魏 已解决,感谢。是我的低级错误
7 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐