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

DES加密解密

程序员文章站 2022-05-15 15:00:26
...

DES加密

maven依赖:

 <!-- DES -->
  <dependency>
       <groupId>javax.xml.rpc</groupId>
       <artifactId>javax.xml.rpc-api</artifactId>
       <version>1.1.1</version>
   </dependency>

DESUtil类:

package common;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.NoSuchAlgorithmException;

/**
 * @program: Demo
 * @Date: 2019/2/20 20:59
 * @Author: LiJc
 * @Description:
 */
public class DESUtil {
    /**
     *  生成KEY
     */
    public static byte[] getKey(){
        KeyGenerator keyGenerator = null;
        try {
            keyGenerator = KeyGenerator.getInstance("DES");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            System.out.println("----------------KEY生成失败!");
            return null;
        }
        keyGenerator.init(56);
        SecretKey secretKey = keyGenerator.generateKey();
        byte[] byteKey = secretKey.getEncoded();
        return byteKey;
    }

    /**
     * DES加密
     *
     * @param context
     * @return
     */
    public static byte[] desEncrypt(String context, String key) {
        try {
            // KEY转换
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
            SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey conventSecretKey = secretKeyFactory.generateSecret(desKeySpec);

            // 加密
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, conventSecretKey, new IvParameterSpec(key.getBytes("UTF-8")));
            return cipher.doFinal(context.getBytes("UTF-8"));
        } catch (Throwable e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * DES解密
     *
     * @param context
     * @return
     */
    public static byte[] desDecrypt(byte[] context, String key) {
        try {
            // KEY转换
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
            SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey conventSecretKey = secretKeyFactory.generateSecret(desKeySpec);

            // 解密
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, conventSecretKey, new IvParameterSpec(key.getBytes("UTF-8")));
            return cipher.doFinal(context);
        } catch (Throwable e) {
            e.printStackTrace();
            return null;
        }
    }
}

单元测试:

package common;

import org.apache.commons.codec.binary.Base64;
import org.junit.Test;

/**
 * @program: Demo
 * @Date: 2019/2/20 21:00
 * @Author: LiJc
 * @Description:
 */
public class DESTest {

    @Test
    public void test1(){
        String deskey = "password";//des**//长度8位
        String content = "真香警告!"; //内容
        System.out.println("DES原始加密内容为:"+content);
        //加密
        byte[] encryptbyte =  DESUtil.desEncrypt(content, deskey); //加密后返回为字节数组
        System.out.println("DES加密后内容为:"+ Base64.encodeBase64String(encryptbyte));
        //解密
        byte[] decryptbyte = DESUtil.desDecrypt(encryptbyte, deskey);  //将加密后返回的字节数组进行解密
        System.out.println("DES解密后内容为:"+ new String(decryptbyte));
    }
}

测试结果:
DES加密解密