DES加密工具类在不同系统编码导致加密失败

5 篇文章 0 订阅
3 篇文章 0 订阅

一、错误提示:

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BCArchive 中文版是一款使用简单的高速加密工具。它具有简单明了的使用界面,用户还可以通过鼠标右键快速对文件进行加密,而不需要先启动软件。软件还可以生成自动解密程序,这样收到加密文件的人就不需要也安装同样的软件了。软件支持多种加密算法,如Triple DES, Blowfish, 以及 Rijndael.另外,它还可以用来彻底删除文件。 简单易用的高速加密工具 BCArchive 中文版简单易用的高速加密工具 BCArchive 中文版 好处与优势 压缩实用程序支持强加密 BCArchive 利用下面的加密算法,标准和规范: 对称算法:Rijndael 算法(AES),河豚-256,河豚-448,IDEA,CAST5,GOST 28147-89,三重DES。 安全散列算法:SHA-256,SHA-1,MD5和RIPEMD-160。 非对称(公钥/私钥对)算法:RSA,ElGamal公钥/的Diffie-Hellman。 规格为公/私钥对格式:PKCS#12,X.509。 PKCS#为基于密码的加密实施5建议。 RFC 2440规范加密的对称或公钥加密算法的会话密钥。 友好的用户加密 BCArchive 很简单但非常安全的 – 只要指定的存档密码,当您创建并输入相同的密码进行解密,并提取数据。不再需要密码的,如果你有你的接收方的公钥 – 只需选择你的接收方的公钥,然后单击编码。 使用自解压文件 – 避免需要收件人安装软件 BCArchive 为您提供设施,以创建一个“自解压”的文件。数据被压缩和加密为单个档案文件,它被转换成一个可执行程序。在此后任何时间你或你的收件人可以运行任何计算机上的程序,并提取数据,而无需安装任何特殊软件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值