java 3des双倍长 有参考的代码给提供下吗?
答案:1 悬赏:80 手机版
解决时间 2021-02-27 12:50
- 提问者网友:几叶到寒
- 2021-02-26 15:26
java 3des双倍长 有参考的代码给提供下吗?
最佳答案
- 五星知识达人网友:三千妖杀
- 2021-02-26 16:03
虽然不知道你说的双倍长是什么意思,这是我自己用的3des工具类
密文是把byte数组转化成16进制字符串,一个byte对应两个数字,不知道是不是你说的双倍
public class DESede {
private static final String Algorithm = "DESede";
private static final char[] hex = "0123456789abcdef".toCharArray();
public static final String keyStr = StringUtil.DeviceKey;
private static byte[] key = strToBytes(keyStr);
public static String encrypt(String src) {
if(src == null){
return null;
}
try {
byte[] origin = src.getBytes("utf-8");
return bytesToStr(des3Init(Cipher.ENCRYPT_MODE, origin));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String decrypt(String cipher) {
if(cipher == null){
return null;
}
try {
byte[] origin = des3Init(Cipher.DECRYPT_MODE, strToBytes(cipher));
return new String(origin, "utf-8");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static byte[] des3Init(int mode, byte[] input) throws Exception{
// 根据【给定的字节数组key】和【 指定算法DESede(3des)】构造一个密钥
SecretKey secretKey = new SecretKeySpec(key, Algorithm);
// 加解密
Cipher cipher = Cipher.getInstance(Algorithm);
cipher.init(mode, secretKey);
return cipher.doFinal(input);
}
private static String bytesToStr(byte[] b){
StringBuilder s = new StringBuilder();
for (int i = 0; i < b.length; i++) {
s.append(hex[(b[i]>>>4)&0xf]);
s.append(hex[b[i]&0xf]);
}
return s.toString();
}
private static byte[] strToBytes(String src){
char[] c = src.toCharArray();
byte[] b = new byte[c.length/2];
for (int i = 0; i < b.length; i++) {
b[i] = (byte)((Character.digit((int)c[2*i], 16)<<4)
| Character.digit((int)c[2*i+1], 16));
}
return b;
}
}
密文是把byte数组转化成16进制字符串,一个byte对应两个数字,不知道是不是你说的双倍
public class DESede {
private static final String Algorithm = "DESede";
private static final char[] hex = "0123456789abcdef".toCharArray();
public static final String keyStr = StringUtil.DeviceKey;
private static byte[] key = strToBytes(keyStr);
public static String encrypt(String src) {
if(src == null){
return null;
}
try {
byte[] origin = src.getBytes("utf-8");
return bytesToStr(des3Init(Cipher.ENCRYPT_MODE, origin));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String decrypt(String cipher) {
if(cipher == null){
return null;
}
try {
byte[] origin = des3Init(Cipher.DECRYPT_MODE, strToBytes(cipher));
return new String(origin, "utf-8");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static byte[] des3Init(int mode, byte[] input) throws Exception{
// 根据【给定的字节数组key】和【 指定算法DESede(3des)】构造一个密钥
SecretKey secretKey = new SecretKeySpec(key, Algorithm);
// 加解密
Cipher cipher = Cipher.getInstance(Algorithm);
cipher.init(mode, secretKey);
return cipher.doFinal(input);
}
private static String bytesToStr(byte[] b){
StringBuilder s = new StringBuilder();
for (int i = 0; i < b.length; i++) {
s.append(hex[(b[i]>>>4)&0xf]);
s.append(hex[b[i]&0xf]);
}
return s.toString();
}
private static byte[] strToBytes(String src){
char[] c = src.toCharArray();
byte[] b = new byte[c.length/2];
for (int i = 0; i < b.length; i++) {
b[i] = (byte)((Character.digit((int)c[2*i], 16)<<4)
| Character.digit((int)c[2*i+1], 16));
}
return b;
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯