All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.javaclub.configcenter.client.util.DESCoder Maven / Gradle / Ivy

The newest version!
package com.github.javaclub.configcenter.client.util;

import java.security.Key;

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.Base64;

public class DESCoder {
	/**
	 * 密钥算法 java支持56位密钥,bouncycastle支持64位
	 * */
	public static final String KEY_ALGORITHM = "DES";

	/**
	 * 加密/解密算法/工作模式/填充方式
	 * */
	public static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";

	/**
	 * 
	 * 生成密钥,java6只支持56位密钥,bouncycastle支持64位密钥
	 * 
	 * @return byte[] 二进制密钥
	 * */
	public static byte[] initkey() throws Exception {

		// 实例化密钥生成器
		KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
		// 初始化密钥生成器
		kg.init(56);
		// 生成密钥
		SecretKey secretKey = kg.generateKey();
		// 获取二进制密钥编码形式
		return secretKey.getEncoded();
	}

	public static String getBase64Key() throws Exception {
		byte[] key = DESCoder.initkey();
		return Base64.encodeBase64String(key);
	}

	/**
	 * 转换密钥
	 * 
	 * @param key 二进制密钥
	 * @return Key 密钥
	 * */
	public static Key toKey(byte[] key) throws Exception {
		// 实例化Des密钥
		DESKeySpec dks = new DESKeySpec(key);
		// 实例化密钥工厂
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
		// 生成密钥
		SecretKey secretKey = keyFactory.generateSecret(dks);
		return secretKey;
	}

	/**
	 * 加密数据
	 * 
	 * @param data 待加密数据
	 * @param key 密钥
	 * @return byte[] 加密后的数据
	 * */
	public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
		// 还原密钥
		Key k = toKey(key);
		// 实例化
		Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
		// 初始化,设置为加密模式
		cipher.init(Cipher.ENCRYPT_MODE, k);
		// 执行操作
		return cipher.doFinal(data);
	}

	public static String encrypt(String str, String base64Key) throws Exception {
		byte[] data = DESCoder.encrypt(str.getBytes("utf-8"), Base64.decodeBase64(base64Key));
		return Base64.encodeBase64String(data);
	}

	/**
	 * 解密数据
	 * 
	 * @param data 待解密数据
	 * @param key 密钥
	 * @return byte[] 解密后的数据
	 * */
	public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
		// 欢迎密钥
		Key k = toKey(key);
		// 实例化
		Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
		// 初始化,设置为解密模式
		cipher.init(Cipher.DECRYPT_MODE, k);
		// 执行操作
		return cipher.doFinal(data);
	}

	public static String decrpty(String base64Str, String base64Key) throws Exception {
		return new String(DESCoder.decrypt(Base64.decodeBase64(base64Str), Base64.decodeBase64(base64Key)), "utf-8");
	}

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		String msg = "sfsdf哇恶搞";
		String key = getBase64Key();
		System.out.println("key:" + key);
		String emsg = encrypt(msg, key);
		System.out.println("encrypt: "+ emsg);
		System.out.println("decrpty: "+decrpty(emsg,key));
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy