Commit dcfb0ed0 authored by matianhao's avatar matianhao

[资产资源接口] <add> DES解密

parent 7fe79a6f
......@@ -5,9 +5,12 @@ import org.apache.commons.lang.StringUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.Security;
import java.util.Base64;
......@@ -129,4 +132,55 @@ public class AESUtils {
}
}
/**
* 16进制字符串转字节数组
*
* @param hexStr
* @return
*/
public static byte[] hexStr2Byte(String hexStr) {
String str = "0123456789ABCDEF";
char[] hexs = hexStr.toCharArray();
byte[] bytes = new byte[hexStr.length() / 2];
int n;
for (int i = 0; i < bytes.length; i++) {
n = str.indexOf(hexs[2 * i]) * 16;
n += str.indexOf(hexs[2 * i + 1]);
bytes[i] = (byte) (n & 0xff);
}
return bytes;
}
public static void main(String[] args) throws Exception {
// 秘钥
String key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4";
// 待解密参数
String content = "6B2E012D451E446DA87BED8BB65F23879D27E80BAE10980BBC79BA0EC894392C7CC82E659370A366";
try {
// 算法/模式/补码方式
Cipher cipher = Cipher.getInstance("desede/ECB/NoPadding");
// key
DESedeKeySpec keySpec = new DESedeKeySpec(Base64.getDecoder().decode(key));
//获取DES秘钥生成器对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("desede");
// 生成秘钥:key的长度不能够小于8位字节
Key secretKey = keyFactory.generateSecret(keySpec);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] origin = cipher.doFinal(hexStr2Byte(content));
System.out.println(new String(origin));
} catch (Exception e) {
e.printStackTrace();
}
}
}
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