DES加密

DES是一种对称加密算法

英文名:Data Encrption Standard(数据加密标准)

DES算法的加密与解密都是使用同一个密钥,密钥的长度为8个字节

如下为加密算法:


import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.net.URLDecoder;
import java.net.URLEncoder;

/**
* 加密 * * @param inputContent 要加密的内容 * @param inputKey 加密用的KEY * @return * @throws Exception */ private static String encryptProcess(String inputContent, String inputKey) throws Exception { DESKeySpec desKeySpec = new DESKeySpec(inputKey.getBytes("UTF-8")); SecretKey secretKey = SecretKeyFactory.getInstance("DES").generateSecret(desKeySpec); IvParameterSpec iv = new IvParameterSpec(inputKey.getBytes("UTF-8")); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); byte[] encodedBytes = cipher.doFinal(URLEncoder.encode(inputContent, "UTF-8").getBytes("UTF-8")); StringBuffer buffer = new StringBuffer(); for (byte b : encodedBytes) { String plainText = Integer.toHexString(0xff & b); if (plainText.length() < 2) plainText = "0" + plainText; buffer.append(plainText); } return buffer.toString(); }

 

扩展:3DES (Triple DES: 三重数据加密算法),对每个数据块应用3次DES加密算法(使用3条56位的密钥对数据进行三次加密)

3DES是DES向AES过渡时期的一种算法,在Java代码中,算法的String是"DESede",如:

Cipher.getInstance("DESede");

 

posted @ 2017-06-15 11:04  XIAOSHUA  阅读(369)  评论(0编辑  收藏  举报