欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java与.net的DES加密解密

程序员文章站 2022-05-15 14:59:08
...

今天遇到一个需求,是对账号的密码用java实现DES加密,但是库里的密码都是由.net语言的DES算法加密好的,关于.net那边的加密方式我只有一张截图:

java与.net的DES加密解密


我很奇怪,代码里面有两个key,之前我用过的DES加密都只有一个,于是百度,看到一篇文章,解释很详细:

https://www.cnblogs.com/Lawson/archive/2012/05/20/2510781.html

于是我肯定,不是常用的那种java中的DES加密能解决的问题;其中还有个向量的概念,是DES加密的CBC模式,于是改代码:


package com.lieni.apollo.auth.utils;

import java.io.IOException;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

import org.bouncycastle.util.encoders.Base64;

public class DesUtil {

    public final static String KEY = "RUYUKEY1";
    private final static String DES = "DES";
    private final static String IV = "RUYUKEY2";
    private final static String CHARSET = "UTF-8";
    private final static String CBC = "DES/CBC/PKCS5Padding";

    /**
     * Description 根据键值进行加密
     * 
     * @param data
     * @param key 加密键byte数组
     * @return
     * @throws Exception
     */
    public static String encodeString(String data, String key) {
        byte[] bt = null;
        try {
            bt = encrypt(data.getBytes(CHARSET), key.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new String(Base64.encode(bt));
    }

    /**
     * Description 根据键值进行解密
     * 
     * @param data
     * @param key 加密键byte数组
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String decodeString(String data, String key) {
        if (data == null)
            return null;
        byte[] bt = null;
        try {
            byte[] buf = Base64.decode(data.getBytes());
            bt = decrypt(buf, key.getBytes());
            return new String(bt, CHARSET);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Description 根据键值进行加密
     * 
     * @param data
     * @param key 加密键byte数组
     * @return
     * @throws Exception
     */
    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        // 从原始**数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);
        // 创建一个**工厂,然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(CBC);
        IvParameterSpec iv2 = new IvParameterSpec(IV.getBytes(CHARSET));
        cipher.init(Cipher.ENCRYPT_MODE, securekey, iv2);
        return cipher.doFinal(data);
    }

    /**
     * Description 根据键值进行解密
     * 
     * @param data
     * @param key 加密键byte数组
     * @return
     * @throws Exception
     */
    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        // 从原始**数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);
        // 创建一个**工厂,然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(CBC);
        IvParameterSpec iv2 = new IvParameterSpec(IV.getBytes(CHARSET));
        cipher.init(Cipher.DECRYPT_MODE, securekey, iv2);
        return cipher.doFinal(data);
    }

    public static void main(String[] args) {
        System.out.println(DesUtil.encodeString("test123", DesUtil.KEY));
        System.out.println(DesUtil.decodeString("ajFLisVReGs=", DesUtil.KEY));
    }

}

输出结果:

ajFLisVReGs=
test123