对称密码之DES

来源:互联网 发布:网络创世纪uo 音乐 编辑:程序博客网 时间:2024/06/08 11:17

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 0
原创粉丝点击