一、错误提示:

Given final block not properly padded. Such issues can arise if a bad key is used during decryption.

二、原因

SecureRandom 这个对象在加密时完全会根据操作系统底层进行加密,导致linux下每次加密后的结果都不相同,无法进行解密。

三、解决办法

SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG") ;
secureRandom.setSeed(key.getBytes(charset));
kg.init(keysize, secureRandom);

使用SecureRandom 并制定编码后,就能够正常加密解密

四、附加密工具类

package com.lenovo.mt.utils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Objects;

public class UrlCompressUtils {

    private static final String DES = "DES";
    private static final int keysizeDES = 56;
    private static final String KEY = "9de1cdaa-0bea-4d1d-935c-af9559d2dd76";

    public UrlCompressUtils() {
    }

    public static String dESencode(String res) {
        return keyGeneratorES(res, true);
    }

    public static String dESdecode(String res) {
        return keyGeneratorES(res, false);
    }

    private static String parseByte2HexStr(byte[] buf) {
        StringBuilder sb = new StringBuilder();
        for (byte b : buf) {
            String hex = Integer.toHexString(b & 255);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    private static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) {
            return null;
        } else {
            byte[] result = new byte[hexStr.length() / 2];

            for(int i = 0; i < hexStr.length() / 2; ++i) {
                int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
                int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
                result[i] = (byte)(high * 16 + low);
            }

            return result;
        }
    }

    private static String keyGeneratorES(String res, boolean isEncode) {
        try {
            KeyGenerator kg = KeyGenerator.getInstance(DES);
            String charset = "utf-8";
            if (UrlCompressUtils.keysizeDES == 0) {
                SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG") ;
                secureRandom.setSeed(UrlCompressUtils.KEY.getBytes(charset));
                kg.init(UrlCompressUtils.keysizeDES, secureRandom);
            } else {
                SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG") ;
                secureRandom.setSeed(UrlCompressUtils.KEY.getBytes(charset));
                kg.init(UrlCompressUtils.keysizeDES, secureRandom);
            }

            SecretKey sk = kg.generateKey();
            SecretKeySpec sks = new SecretKeySpec(sk.getEncoded(), "DES");
            Cipher cipher = Cipher.getInstance("DES");
            if (isEncode) {
                cipher.init(1, sks);
                byte[] resBytes = res.getBytes(charset);
                return parseByte2HexStr(cipher.doFinal(resBytes));
            } else {
                cipher.init(2, sks);
                return new String(cipher.doFinal(Objects.requireNonNull(parseHexStr2Byte(res))));
            }
        } catch (Exception var10) {
            var10.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        String url = "www.baidu.com";
        String encode = UrlCompressUtils.dESencode(url);
        System.out.println(encode);

        String sdecode = UrlCompressUtils.dESdecode(encode);
        System.out.println(sdecode);
    }
}

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐