han108
2016-10-10 09:08:42
java写的DES加密解密的小例子
运行代码:
package com.DESUtil;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import com.sun.crypto.provider.SunJCE;
/**
* Description 处理字符串加密解密的公用类
* @author Administrator
*
*/
public class DESEDE {
private static final String Algorithm = "DESede";
public DESEDE() {
}
private static byte[] encryptMode(byte keybyte[], byte src[]) {
try {
javax.crypto.SecretKey deskey = new SecretKeySpec(keybyte, "DESede");
Cipher c1 = Cipher.getInstance("DESede");
c1.init(1, deskey);
return c1.doFinal(src);
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (Exception e3) {
e3.printStackTrace();
}
return null;
}
private static byte[] decryptMode(byte keybyte[], byte src[]) {
try {
javax.crypto.SecretKey deskey = new SecretKeySpec(keybyte, "DESede");
Cipher c1 = Cipher.getInstance("DESede");
c1.init(2, deskey);
return c1.doFinal(src);
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (Exception e3) {
e3.printStackTrace();
}
return null;
}
private static String byte2hex(byte b[]) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0xff);
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase();
}
public static byte[] hex2byte(String hex) throws IllegalArgumentException {
if (hex.length() % 2 != 0)
throw new IllegalArgumentException();
char arr[] = hex.toCharArray();
byte b[] = new byte[hex.length() / 2];
int i = 0;
int j = 0;
for (int l = hex.length(); i < l;) {
String swap = "" + arr[i++] + arr[i];
int byteint = Integer.parseInt(swap, 16) & 0xff;
b[j] = (new Integer(byteint)).byteValue();
i++;
j++;
}
return b;
}
/**
* 字符串加密
* @param sourceCode 要加密的字符串
* +
* @return
*/
public static String encryptIt(String sourceCode) {
Security.addProvider(new SunJCE());
byte keyBytes[] = { 17, 34, 79, 88, -120, 16, 64, 56, 40, 37, 121, 81,
-53, -35, 85, 102, 119, 41, 116, -104, 48, 64, 54, -30 };
byte encoded[] = encryptMode(keyBytes, sourceCode.getBytes());
return byte2hex(encoded);
}
/**
* 字符串解密
* @param encodedCode 要解密的字符串
* @return
*/
public static String decryptIt(String encodedCode) {
Security.addProvider(new SunJCE());
byte keyBytes[] = { 17, 34, 79, 88, -120, 16, 64, 56, 40, 37, 121, 81,
-53, -35, 85, 102, 119, 41, 116, -104, 48, 64, 54, -30 };
byte encoded[] = decryptMode(keyBytes, hex2byte(encodedCode));
return new String(encoded);
}
public static void main(String args[]) {
String str="1234";
String str1=encryptIt(str);
System.out.println("加密1234:"+str1);
String str2=decryptIt("D4F7DDF6D2B9A0E7");
//System.out.println("str2="+str2);
if(str.equals(str2)){
System.out.println("加密解密成功了");
}
System.out.println("解密9F7798E2D49AC78C7C78DE1DA63CD426:"+decryptIt("9F7798E2D49AC78C7C78DE1DA63CD426"));
}
}
运行截图:
猜你喜欢
请下载代码后再发表评论
相关代码
最近下载
最近浏览
except I LV2
2023年11月5日
wwfl02 LV3
2022年12月16日
yymmdm LV6
2022年8月10日
四季夏目 LV7
2022年6月19日
3199625134 LV10
2022年4月23日
许文欣 LV2
2022年3月26日
y1214435276 LV9
2022年3月24日
微信网友_5865371899187200
2022年3月9日
暂无贡献等级
微信网友_5853454548717568
2022年3月1日
暂无贡献等级
rookandfuze
2022年2月18日
暂无贡献等级




