对称密码之DES

1、对称密码的概念

      1.加密密钥和解密密钥相同,对于大多数对称密码算法,加解密过程互逆

      2.加解密通信模型

          

      3.特点:算法公开、计算量小、加密速度快、加密效率高

      4.弱点:双方都使用同样密钥,安全性得不到保证

2、常见的对称加密方式

      (1)DES   (Data Encryption Standard)
      (2)3DES (Triple DES、DESede)
      (3)AES    (Advanced Encryption Standard)      

3、DES算法基本概念

      1.DES:数据加密标准,是对称加密算法领域中的典型算法
      2.特点:密钥偏短(56位)、生命周期短(因为秘钥只有56位,能暴力破解。所以要定期更换。故生命周期短)
      3.JDK实现

          

4、DES算法编程步骤

      4.1  生成秘钥

    

      4.2  加/解密

              

5、DES算法的实现

DESUtil.java

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class DESUtil {

	/**
	 * 生成密钥
	 * 
	 * @return
	 */
	public static byte[] initKey() {
		try {
			// 秘钥生成器
			KeyGenerator keyGen = KeyGenerator.getInstance("DES");
			// 初始化秘钥生成器
			keyGen.init(56);
			// 生成密钥
			SecretKey secretKey = keyGen.generateKey();
			return secretKey.getEncoded();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

	/**
	 * 使用DES算法,对数据进行加密
	 * 
	 * @param data
	 *            要加密的数据
	 * @param key
	 *            加密数据的秘钥
	 * @return
	 */
	public static byte[] encrypt(byte[] data, byte[] key) {
		try {
			// 恢复密钥
			SecretKey secretKey = new SecretKeySpec(key, "DES");
			// cipher完成加密或者解密
			Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
			// 根基密钥,对Cipher进行初始化ENCRYPT_MODE(加密),DECRYPT_MODE(解密)
			cipher.init(Cipher.ENCRYPT_MODE, secretKey);
			// 解密/加密
			byte[] cipherBytes = cipher.doFinal(data);
			return cipherBytes;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

	/**
	 * 使用DES数据解密
	 * 
	 * @param data
	 *            要解密的数据
	 * @param key
	 *            解密数据用到的秘钥
	 * @return
	 */
	public static byte[] decrypt(byte[] data, byte[] key) {
		try {
			// 恢复密钥
			SecretKey secretKey = new SecretKeySpec(key, "DES");
			// cipher完成加密或者解密
			Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
			// 根基密钥,对Cipher进行初始化ENCRYPT_MODE(加密),DECRYPT_MODE(解密)
			cipher.init(Cipher.DECRYPT_MODE, secretKey);
			// 解密/加密
			byte[] plainBytes = cipher.doFinal(data);
			return plainBytes;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}
}

字节数组到16进制的转换

public class BytesToHex {

	public static String fromBytesToHex(byte[] resultBytes) {
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < resultBytes.length; i++) {
			if (Integer.toHexString(0xFF & resultBytes[i]).length() == 1) {
				builder.append("0").append(
						Integer.toHexString(0xFF & resultBytes[i]));
			} else {
				builder.append(Integer.toHexString(0xFF & resultBytes[i]));
			}
		}
		return builder.toString();
	}

}

 测试代码

public class Test {
	// 待加密的明文
	public static final String DATA = "test";

	public static void main(String[] args) {
		/* Test DES */
		byte[] desKey = DESUtil.initKey();
		System.out.println("DES KEY : " + BytesToHex.fromBytesToHex(desKey));
		byte[] desResult = DESUtil.encrypt(DATA.getBytes(), desKey);
		System.out.println(DATA + ">>>DES 加密>>>"
				+ BytesToHex.fromBytesToHex(desResult));

		byte[] desPlain = DESUtil.decrypt(desResult, desKey);
		System.out.println(BytesToHex.fromBytesToHex(desResult)
				+ ">>>DES 解密>>>" + new String(desPlain));
	}
}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值