Des加密解密例子

简介:

代码如下:

package com.yanek.util;


import java.security.Key;

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

import org.apache.commons.codec.binary.Base64;

/**
 * 运算模式CBC。<br>
 * 在CBC模式下使用key,向量iv;<br>
 * 对字符加密时,双方采用的都是UTF-8编码
 * @version 1.0.0
 */
public class DesUtil {

	// 向量
	private static final byte[] keyiv = { 1, 2, 3, 4, 5, 6, 7, 8 };

	/**
	 * CBC解密
	 * 
	 * @param key
	 *            密钥
	 * @param data
	 *            Base64编码的密文
	 * @return 明文
	 * @throws Exception
	 */
	public static byte[] desDecodeCBC(byte[] key, byte[] data) throws Exception {
		Key deskey = null;
		DESKeySpec spec = new DESKeySpec(key);
		SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DES");
		deskey = keyfactory.generateSecret(spec);
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		IvParameterSpec ips = new IvParameterSpec(keyiv);
		cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
		byte[] bOut = cipher.doFinal(data);
		return bOut;
	}

	/**
	 * CBC解密
	 * 
	 * @param key
	 *            密钥
	 * @param data
	 *            Base64加密后的密文
	 * @return 明文
	 * @throws Exception
	 */
	public static String desDecodeCBC(String key, String data) throws Exception {
		byte[] _data = Base64.decodeBase64(data);
		byte[] _key = key.getBytes("UTF-8");
		byte[] bOut = desDecodeCBC(_key, _data);

		return new String(bOut, "UTF-8");
	}

	/**
	 * CBC加密
	 * 
	 * @param key
	 *            密钥
	 * @param data
	 *            明文
	 * @return 密文
	 * @throws Exception
	 */
	public static byte[] desEncodeCBC(byte[] key, byte[] data) throws Exception {
		Key deskey = null;
		DESKeySpec spec = new DESKeySpec(key);
		SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DES");
		deskey = keyfactory.generateSecret(spec);
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); // 加密方法/运算模式/填充模式
		IvParameterSpec ips = new IvParameterSpec(keyiv);
		cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
		byte[] bOut = cipher.doFinal(data);
		return bOut;
	}

	/**
	 * CBC加密
	 * 
	 * @param key
	 *            密钥
	 * @param data
	 *            明文
	 * @return Base64加密后的密文
	 * @throws Exception
	 */
	public static String desEncodeCBC(String key, String data) throws Exception {
		byte[] _data = data.getBytes("UTF-8");
		byte[] _key = key.getBytes("UTF-8");
		byte[] bOut = desEncodeCBC(_key, _data);

		return Base64.encodeBase64String(bOut); // Base64加密后的密文
	}

	public static void main(String[] args) throws Exception {

		String key = "hdhhdhdmZtmcOlmT2";
		String data = "学习测试";

		System.out.println("加密解密 测试 ");
		String str1 = desEncodeCBC(key, data);// 加密
		String str2 = desDecodeCBC(key, str1);// 解密
		System.out.println(str1);
		System.out.println(str2);

	}
}


 

目录
相关文章
|
2月前
|
Java 数据安全/隐私保护
des加密+base64编码,base64解码+des解密
des加密+base64编码,base64解码+des解密
46 0
|
2月前
|
算法 搜索推荐 Java
DES - 对称加密算法简要介绍与JAVA实现
DES - 对称加密算法简要介绍与JAVA实现
69 2
|
29天前
|
存储 算法 安全
Java中的DES和3DES加密算法详解
Java中的DES和3DES加密算法详解
|
1月前
|
Java C# 数据安全/隐私保护
|
24天前
|
C# 数据安全/隐私保护
Des加密和解密
Des加密和解密
21 0
|
2月前
|
存储 算法 安全
加密解密(DES)
加密解密(DES)
|
2月前
|
存储 算法 Java
加密解密(3DES)DES的加强版
加密解密(3DES)DES的加强版
|
2月前
|
算法 安全 数据安全/隐私保护
C/C++学习 -- 分组加密算法(DES算法)
C/C++学习 -- 分组加密算法(DES算法)
93 0
|
2月前
|
JavaScript 前端开发 算法
JavaScript学习 -- 对称加密算法DES
JavaScript学习 -- 对称加密算法DES
46 0
|
2月前
|
算法 安全 程序员
详解 DES加密技术 | 凯撒密码 | 栅栏密码
详解 DES加密技术 | 凯撒密码 | 栅栏密码
153 0