21xrx.com
2024-09-17 03:55:52 Tuesday
登录
文章检索 我的文章 写文章
Java实现DES加密算法的核心代码
2023-06-15 17:51:12 深夜i     --     --
Java DES加密 密钥

DES加密是一种广泛应用于网络通信安全中的加密算法,本文将介绍如何使用Java实现DES加密算法的核心代码,并提供了示例代码供参考。

DES加密算法采用对称密钥体制,即加密和解密使用同一密钥,因此需要确保密钥的安全性。实现DES加密算法的步骤如下:

1.生成密钥

使用Java中的KeyGenerator类和SecureRandom类生成指定长度的DES密钥。

2.创建Cipher对象

使用Java中的Cipher类创建一个DES加密对象,可选择CBC模式或ECB模式。

3.初始化Cipher对象

使用Cipher类的init方法初始化创建的Cipher对象,设置加密模式和密钥。

4.加密明文

使用Cipher类的doFinal方法对明文进行加密,返回加密后的密文。

下面是示例代码实现DES加密算法:


import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

import java.security.Key;

import java.security.SecureRandom;

public class DesUtil {

  /**

   * DES加密

   * @param content 明文

   * @param keyWord 密钥

   * @return 密文

   */

  public static String encrypt(String content, String keyWord) {

    try {

      // 生成密钥

      KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");

      keyGenerator.init(new SecureRandom(keyWord.getBytes()));

      SecretKey secretKey = keyGenerator.generateKey();

      byte[] byteKey = secretKey.getEncoded();

      // 转换密钥

      Key key = new SecretKeySpec(byteKey, "DES");

      // 创建Cipher对象

      Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

      cipher.init(Cipher.ENCRYPT_MODE, key);

      // 加密

      byte[] byteContent = content.getBytes("utf-8");

      byte[] result = cipher.doFinal(byteContent);

      return parseByte2HexStr(result);

    } catch (Exception e) {

      e.printStackTrace();

    }

    return null;

  }

  /**

   * DES解密

   * @param content 密文

   * @param keyWord 密钥

   * @return 明文

   */

  public static String decrypt(String content, String keyWord) {

    try {

      // 生成密钥

      KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");

      keyGenerator.init(new SecureRandom(keyWord.getBytes()));

      SecretKey secretKey = keyGenerator.generateKey();

      byte[] byteKey = secretKey.getEncoded();

      // 转换密钥

      Key key = new SecretKeySpec(byteKey, "DES");

      // 创建Cipher对象

      Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

      cipher.init(Cipher.DECRYPT_MODE, key);

      // 解密

      byte[] byteContent = parseHexStr2Byte(content);

      byte[] result = cipher.doFinal(byteContent);

      return new String(result, "utf-8");

    } catch (Exception e) {

      e.printStackTrace();

    }

    return null;

  }

  /**

   * 将byte数组转换为16进制字符串

   * @param buf

   * @return

   */

  private static String parseByte2HexStr(byte[] buf) {

    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < buf.length; i++) {

      String hex = Integer.toHexString(buf[i] & 0xFF);

      if (hex.length() == 1) {

        sb.append('0');

      }

      sb.append(hex.toUpperCase());

    }

    return sb.toString();

  }

  /**

   * 将16进制字符串转换为byte数组

   * @param hexStr

   * @return

   */

  private static byte[] parseHexStr2Byte(String hexStr) {

    if (hexStr.length() < 1) {

      return null;

    }

    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;

  }

}

经过以上步骤,我们就实现了DES加密算法的核心代码。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复