des vue 双倍长 解密_3DES双倍长加密 - osc_ojx9hm4t的个人空间 - OSCHINA - 中文开源技术交流社区...
importjava.security.SecureRandom;importjavax.crypto.Cipher;importjavax.crypto.SecretKey;importjavax.crypto.SecretKeyFactory;importjavax.crypto.spec.DESKeySpec;
//結果與DES算法工具一致public classDES{public static voidmain(String[] args) {
String key= "F2C04AD9F598EE61C424C9C7D39BA75F";
String data= "06321643FF8B67EB";
String des=encryptECB3Des(key,data);
System.out.println(des);
}public staticString encryptECB3Des(String key, String src) {
System.out.println("encryptECB3Des->" + "key:" +key);
System.out.println("encryptECB3Des->" + "src:" +src);int len =key.length();if (key == null || src == null) {return null;
}if (src.length() % 16 != 0) {return null;
}if (len == 32) {
String outData= "";
String str= "";for (int i = 0; i < src.length() / 16; i++) {
str= src.substring(i * 16, (i + 1) * 16);
outData+=encECB3Des(key, str);
}returnoutData;
}return null;
}public staticString encECB3Des(String key, String src) {byte[] temp = null;byte[] temp1 = null;
temp1= encryptDes(hexStringToBytes(key.substring(0, 16)), hexStringToBytes(src));
temp= decryptDes(hexStringToBytes(key.substring(16, 32)), temp1);
temp1= encryptDes(hexStringToBytes(key.substring(0, 16)), temp);returnbytesToHexString(temp1);
}public staticString decECB3Des(String key, String src) {byte[] temp2 = decryptDes(hexStringToBytes(key.substring(0, 16)), hexStringToBytes(src));byte[] temp1 = encryptDes(hexStringToBytes(key.substring(16, 32)), temp2);byte[] dest = decryptDes(hexStringToBytes(key.substring(0, 16)), temp1);returnbytesToHexString(dest);
}public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder= new StringBuilder("");if (src == null || src.length <= 0) {return null;
}for (int i = 0; i < src.length; i++) {int v = src[i] & 0xFF;
String hv=Integer.toHexString(v);if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}returnstringBuilder.toString();
}public static byte[] hexStringToBytes(String hexString) {if (hexString == null || hexString.equals("")) {return null;
}
hexString=hexString.toUpperCase();int length = hexString.length() / 2;char[] hexChars =hexString.toCharArray();byte[] d = new byte[length];for (int i = 0; i < length; i++) {int pos = i * 2;
d[i]= (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}returnd;
}private static byte charToByte(charc) {return (byte) "0123456789ABCDEF".indexOf(c);
}/*** 3DES(雙倍長) 解密
*
*@paramkeybyte
*@paramsrc
*@return
*/
public staticString decryptECB3Des(String key, String src) {if (key == null || src == null) {return null;
}if (src.length() % 16 != 0) {return null;
}if (key.length() == 32) {
String outData= "";
String str= "";for (int i = 0; i < src.length() / 16; i++) {
str= src.substring(i * 16, (i + 1) * 16);
outData+=decECB3Des(key, str);
}returnoutData;
}return null;
}/*** DES加密
**/
public static byte[] encryptDes(byte[] key, byte[] src) {try{//創建一個DESKeySpec對象
DESKeySpec desKey = newDESKeySpec(key);//創建一個密匙工廠
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");//將DESKeySpec對象轉換成SecretKey對象
SecretKey secretKey =keyFactory.generateSecret(desKey);//Cipher對象實際完成解密操作
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");//用密匙初始化Cipher對象
cipher.init(Cipher.ENCRYPT_MODE, secretKey);//現在,獲取數據并加密//正式執行加密操作
returncipher.doFinal(src);
}catch(Exception e) {
e.printStackTrace();
}return null;
}/*** des解密
*
*@paramkey
*@paramsrc
*@return
*/
public static byte[] decryptDes(byte[] key, byte[] src) {try{//DES算法要求有一個可信任的隨機數源
SecureRandom random = newSecureRandom();//創建一個DESKeySpec對象
DESKeySpec desKey = newDESKeySpec(key);//創建一個密匙工廠
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");//將DESKeySpec對象轉換成SecretKey對象
SecretKey secretKey =keyFactory.generateSecret(desKey);//Cipher對象實際完成解密操作
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");//用密匙初始化Cipher對象
cipher.init(Cipher.DECRYPT_MODE, secretKey, random);//現在,獲取數據并加密//正式執行加密操作
returncipher.doFinal(src);
}catch(Exception e) {
e.printStackTrace();
}return null;
}
}
總結
以上是生活随笔為你收集整理的des vue 双倍长 解密_3DES双倍长加密 - osc_ojx9hm4t的个人空间 - OSCHINA - 中文开源技术交流社区...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ios xib 四等分_ios Xib的
- 下一篇: layui 表格内容写temple函数_