在java中調用sun公司提供的
jce
security/US_export_policy
security/local_policy
ext/sunjce_provider
Java運行時會自動加載這些包
以下是java中調用sun公司提供的
加密解密代碼
import java
import javax
import javax
import javax
/*字符串 DESede(
public class ThreeDes {
/**
* @param args在java中調用sun公司提供的
* 用到$JAVA_HOME/jre/lib/目錄下如下的
*jce
*security/US_export_policy
*security/local_policy
*ext/sunjce_provider
*/
private static final String Algorithm =
//keybyte為加密密鑰
//src為被加密的數據緩沖區(源)
public static byte[] encryptMode(byte[] keybyte
try {
//生成密鑰
SecretKey deskey = new SecretKeySpec(keybyte
//加密
Cipher c
c
return c
} catch (java
// TODO: handle exception
e
}catch(javax
e
}catch(java
e
}
return null;
}
//keybyte為加密密鑰
//src為加密後的緩沖區
public static byte[] decryptMode(byte[] keybyte
try {
//生成密鑰
SecretKey deskey = new SecretKeySpec(keybyte
//解密
Cipher c
c
return c
} catch (java
// TODO: handle exception
e
}catch(javax
e
}catch(java
e
}
return null;
}
//轉換成十六進制字符串
public static String byte
String hs=
String stmp=
for(int n=
stmp = (java
if(stmp
hs = hs +
}else{
hs = hs + stmp;
}
if(n<b
}
return hs
}
public static void main(String[] args) {
// TODO Auto
//添加新安全算法
Security
final byte[] keyBytes = {
(byte)
(byte)
(byte)
(byte)
(byte)
}; //
String szSrc =
System
byte[] encoded = encryptMode(keyBytes
System
byte[] srcBytes = decryptMode(keyBytes
System
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26954.html