Java实现的对称加密算法3DES定义与用法示例-创新互联

本文实例讲述了Java实现的对称加密算法3DES定义与用法。分享给大家供大家参考,具体如下:

创新互联建站是一家专注于成都做网站、成都网站建设与策划设计,易门网站建设哪家好?创新互联建站做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:易门等地区。易门做网站价格咨询:18982081108

一 为什么出现3DES

1、返回柯克霍夫原则
2、存在安全问题

二 3DES(Triple DES或DESede)好处

1、密钥长度增强
2、迭代次数提高

三 3DES相关参数

Java实现的对称加密算法3DES定义与用法示例

四 3DES代码实现

package com.imooc.security.des;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import org.apache.commons.codec.binary.Base64;
public class Imooc3DES {
 private static String src = "cakin24 security 3des";
 public static void main(String[] args) {
  jdk3DES();
 }
 public static void jdk3DES() {
  try {
   //生成KEY
   KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");
   //keyGenerator.init(168);
   keyGenerator.init(new SecureRandom());//默认长度
   SecretKey secretKey = keyGenerator.generateKey();
   byte[] bytesKey = secretKey.getEncoded();
   //KEY转换
   DESedeKeySpec desedeKeySpec = new DESedeKeySpec(bytesKey);
   SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
   Key convertSecretKey = factory.generateSecret(desedeKeySpec);
   //加密
   Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
   cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
   byte[] result = cipher.doFinal(src.getBytes());
   System.out.println("jdk 3des encrypt : " + Base64.encodeBase64String(result));
   //解密
   cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
   result = cipher.doFinal(result);
   System.out.println("jdk 3des decrypt : " + new String(result));
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}


网页名称:Java实现的对称加密算法3DES定义与用法示例-创新互联
分享地址:http://ybzwz.com/article/jggsj.html