Commit b02d655d authored by yanzg's avatar yanzg

修改实例化关系

parent 38be1e4e
package com.yanzuoguang.util.helper;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.Key;
import java.security.SecureRandom;
/**
* DES加密
*
* @author 颜佐光
*/
public class DESHelper {
private static final String ALGORITHM_DES = "des";
/**
* DES加密
*
* @param key 秘钥key
* @param content 待加密内容
* @return byte[]
*/
public static String DESEncrypt(final String key, final String content) {
return RsaHelper.encodeBase64(processCipher(content.getBytes(), getSecretKey(key), Cipher.ENCRYPT_MODE, ALGORITHM_DES));
}
/**
* DES解密
*
* @param key 秘钥key
* @param encoderContent 已加密内容
* @return byte[]
*/
public static byte[] DESDecrypt(final String key, final String encoderContent) {
return processCipher(RsaHelper.decodeBase64(encoderContent), getSecretKey(key), Cipher.DECRYPT_MODE, ALGORITHM_DES);
}
/**
* 根据key生成秘钥
*
* @param key 给定key,要求key至少长度为8个字符
* @return SecretKey
*/
public static SecretKey getSecretKey(final String key) {
try {
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
SecretKeyFactory instance = SecretKeyFactory.getInstance(ALGORITHM_DES);
SecretKey secretKey = instance.generateSecret(desKeySpec);
return secretKey;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 加密/解密处理
*
* @param processData 待处理的数据
* @param key 提供的密钥
* @param opsMode 工作模式
* @param algorithm 使用的算法
* @return byte[]
*/
private static byte[] processCipher(final byte[] processData, final Key key,
final int opsMode, final String algorithm) {
try {
SecureRandom secureRandom = new SecureRandom();
Cipher cipher = Cipher.getInstance(algorithm);
//初始化
cipher.init(opsMode, key, secureRandom);
return cipher.doFinal(processData);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
...@@ -295,7 +295,7 @@ public final class RsaHelper { ...@@ -295,7 +295,7 @@ public final class RsaHelper {
* @return * @return
* @throws Exception * @throws Exception
*/ */
private static String encodeBase64(byte[] source) { public static String encodeBase64(byte[] source) {
try { try {
byte[] to = Base64Utils.encode(source); byte[] to = Base64Utils.encode(source);
return new String(to, SystemContants.UTF8); return new String(to, SystemContants.UTF8);
...@@ -311,7 +311,7 @@ public final class RsaHelper { ...@@ -311,7 +311,7 @@ public final class RsaHelper {
* @return * @return
* @throws Exception * @throws Exception
*/ */
private static byte[] decodeBase64(String target) { public static byte[] decodeBase64(String target) {
try { try {
byte[] from = target.getBytes(SystemContants.UTF8); byte[] from = target.getBytes(SystemContants.UTF8);
return Base64Utils.decode(from); return Base64Utils.decode(from);
......
package helper;
import com.yanzuoguang.util.helper.DESHelper;
import org.junit.Test;
public class TestDes {
String pwd = "tubida@yanzuoguang@good@boy@!@#^%$";
// String str = "{\"c\":\"64711099423332\",\"l\":1631524416,\"s\":\"671ec2998053b6de9b76bc8d09e00f1b\",\"t\":\"company-pangding-0000001\"}";
String str = "64711099423332,1631524416,company-pangding-0000001,671ec2998053b6de9b76bc8d09e00f1b";
@Test
public void test() {
System.out.println(str.length());
String des = DESHelper.DESEncrypt(pwd, str);
System.out.println(des.length());
System.out.println(des);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment