net.ymate.platform.commons.codec.DESCodec Maven / Gradle / Ivy
/*
* Copyright 2007-2107 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.ymate.platform.commons.codec;
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;
/**
*
* DESCodec
*
*
* DES编解码类;
*
*
* @author 刘镇([email protected])
* @version 0.0.0
*
*
* 版本号 动作 修改人 修改时间
*
*
*
* 0.0.0
* 创建类
* 刘镇
* 2011-6-14下午12:24:17
*
*
*/
public class DESCodec {
// private static final Log _LOG = LogFactory.getLog(DESCodec.class);
/**
* 密钥算法
*/
public static final String KEY_ALGORITHM = "DES";
/**
* 加密/解密算法/工作模式/填充方式
*/
public static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";
/**
*
* 生成密钥
*
* @return byte[] 二进制密钥
* */
public static byte[] initkey() throws Exception {
// 实例化密钥生成器
KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
// 初始化密钥生成器
kg.init(56);
// 生成密钥
SecretKey secretKey = kg.generateKey();
// 获取二进制密钥编码形式
return secretKey.getEncoded();
}
/**
* 转换密钥
*
* @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);
}
/**
* 解密数据
*
* @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);
}
}