【Java加密解密】DES加密算法

对称加密算法

加密密钥和解密密钥是同一个。

【Java加密解密】DES加密算法


常见的包括:DES,3DES,PBE,IDEA……


DES算法,IBM提交的解决方案。后被破解,3DES是其改进版本。

DES(Data Encryption Standard)数据加密标准

DES默认密钥长度56位,JDK实现长度56,BC实现长度64

3DES是DES改进版本,当然还有2DES和4DES,常用是3DES,长度为168或者112.


注明:

BounCycastle: ouncy Castle 是一种用于 Java 平台的开放源码的轻量级密码术包。它支持大量的密码术算法,并提供 JCE 1.2.1 的实现。因为 Bouncy Castle 被设计成轻量级的,所以从 J2SE 1.4 到 J2ME(包括 MIDP)平台,它都可以运行。它是在 MIDP 上运行的唯一完整的密码术包。


DES和3DES的JDK实现和BC实现算法实现如下:

package com.ufgov;

import java.security.Key;
import java.security.Security;

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

import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class DesSercurty {

	public static void main(String[] args) {
		jdkDES();
		bcDES();
		jdk3DES();
		bc3DES();
	}

	// JDK DES算法实现
	private static void jdkDES() {
		try {
			// 生成密钥
			KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
			keyGenerator.getProvider();
			System.out.println(keyGenerator.getProvider());
			keyGenerator.init(56);
			SecretKey secretKey = keyGenerator.generateKey();
			byte[] bytesKey = secretKey.getEncoded();

			// key转换
			DESKeySpec desKeySpec = new DESKeySpec(bytesKey);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			Key convertsecretKey = keyFactory.generateSecret(desKeySpec);

			// 加密
			Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, convertsecretKey);
			String src = "加密的字符串";
			byte[] result = cipher.doFinal(src.getBytes());
			System.out.println("result des encrypt:"
					+ Hex.encodeHexString(result));

			// 解密
			cipher.init(Cipher.DECRYPT_MODE, convertsecretKey);
			result = cipher.doFinal(result);
			System.out.println("result des decrypt:" + new String(result));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void bcDES() {
		try {

			Security.addProvider(new BouncyCastleProvider());

			// 生成密钥
			KeyGenerator keyGenerator = KeyGenerator.getInstance("DES", "BC");
			keyGenerator.getProvider();
			System.out.println(keyGenerator.getProvider());
			keyGenerator.init(56);
			SecretKey secretKey = keyGenerator.generateKey();
			byte[] bytesKey = secretKey.getEncoded();

			// key转换
			DESKeySpec desKeySpec = new DESKeySpec(bytesKey);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			Key convertsecretKey = keyFactory.generateSecret(desKeySpec);

			// 加密
			Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, convertsecretKey);
			String src = "加密的字符串";
			byte[] result = cipher.doFinal(src.getBytes());
			System.out.println("result des encrypt:"
					+ Hex.encodeHexString(result));

			// 解密
			cipher.init(Cipher.DECRYPT_MODE, convertsecretKey);
			result = cipher.doFinal(result);
			System.out.println("result des decrypt:" + new String(result));
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// JDK DES算法实现
	private static void jdk3DES() {
		try {
			// 生成密钥
			KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");
			keyGenerator.getProvider();
			System.out.println(keyGenerator.getProvider());
			keyGenerator.init(168);
			SecretKey secretKey = keyGenerator.generateKey();
			byte[] bytesKey = secretKey.getEncoded();

			// key转换
			DESKeySpec desKeySpec = new DESKeySpec(bytesKey);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			Key convertsecretKey = keyFactory.generateSecret(desKeySpec);

			// 加密
			Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, convertsecretKey);
			String src = "加密的字符串";
			byte[] result = cipher.doFinal(src.getBytes());
			System.out.println("result des encrypt:"
					+ Hex.encodeHexString(result));

			// 解密
			cipher.init(Cipher.DECRYPT_MODE, convertsecretKey);
			result = cipher.doFinal(result);
			System.out.println("result des decrypt:" + new String(result));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void bc3DES() {
		try {

			Security.addProvider(new BouncyCastleProvider());

			// 生成密钥
			KeyGenerator keyGenerator = KeyGenerator
					.getInstance("DESede", "BC");
			keyGenerator.getProvider();
			System.out.println(keyGenerator.getProvider());
			keyGenerator.init(168);
			SecretKey secretKey = keyGenerator.generateKey();
			byte[] bytesKey = secretKey.getEncoded();

			// key转换
			DESKeySpec desKeySpec = new DESKeySpec(bytesKey);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			Key convertsecretKey = keyFactory.generateSecret(desKeySpec);

			// 加密
			Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, convertsecretKey);
			String src = "加密的字符串";
			byte[] result = cipher.doFinal(src.getBytes());
			System.out.println("result des encrypt:"
					+ Hex.encodeHexString(result));

			// 解密
			cipher.init(Cipher.DECRYPT_MODE, convertsecretKey);
			result = cipher.doFinal(result);
			System.out.println("result des decrypt:" + new String(result));
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}


你可能感兴趣的:(【Java加密解密】DES加密算法)