?? mydes.java
字號(hào):
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
public class MyDes {
private static final String Algorithm = "DESede"; //定義 加密算法,可用 DES,DESede,Blowfish
//keybyte為加密密鑰,長度為24字節(jié)
//src為被加密的數(shù)據(jù)緩沖區(qū)(源)
public static byte[] encryptMode(byte[] keybyte, byte[] src) {
try {
//生成密鑰
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
//加密
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
return c1.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (java.lang.Exception e3) {
e3.printStackTrace();
}
return null;
}
//keybyte為加密密鑰,長度為24字節(jié)
//src為加密后的緩沖區(qū)
public static byte[] decryptMode(byte[] keybyte, byte[] src) {
try {
//生成密鑰
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
//解密
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE, deskey);
return c1.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (java.lang.Exception e3) {
e3.printStackTrace();
}
return null;
}
//轉(zhuǎn)換成十六進(jìn)制字符串
public static String byte2hex(byte[] b) {
String hs="";
String stmp="";
for (int n=0;n<b.length;n++) {
stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length()==1) hs=hs+"0"+stmp;
else hs=hs+stmp;
if (n<b.length-1) hs=hs+":";
}
return hs.toUpperCase();
}
public static void main(String[] args)
{
//添加新安全算法,如果用JCE就要把它添加進(jìn)去
Security.addProvider(new com.sun.crypto.provider.SunJCE());
final byte[] keyBytes = {0x11, 0x11, 0x11, 0x11, (byte)0x11, 0x11, 0x11, 0x11
, 0x11, 0x11, 0x11, 0x11, (byte)0x11, (byte)0x11, 0x11, 0x11
, 0x11, 0x11, 0x11, (byte)0x11, 0x11, 0x11, 0x11, (byte)0x11}; //24字節(jié)的密鑰
String szSrc = "aaa aaa aaa.";
System.out.println("加密前的字符串:" + szSrc);
byte[] encoded = encryptMode(keyBytes, szSrc.getBytes());
System.out.println("加密后的字符串:" + new String(encoded));
byte[] srcBytes = decryptMode(keyBytes, encoded);
System.out.println("解密后的字符串:" + (new String(srcBytes)));
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -