Java寫的加密解密算法及調用范例
1、.JAVA算法范例
package Common.JUtility;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
public class EncryptUtils {
///
/// 3des解碼
///
/// 待解密字符串
/// 原始密鑰字符串
///
public static String Decrypt3DES(String value, String key) throws Exception {
byte[] b = decryptMode(GetKeyBytes(key), Base64.decode(value));
return new String(b);
}
///
/// 3des加密
///
/// 待加密字符串
/// 原始密鑰字符串
///
public static String Encrypt3DES(String value, String key) throws Exception {
String str = byte2Base64(encryptMode(GetKeyBytes(key), value.getBytes()));
return str;
}
//計算24位長的密碼byte值,首先對原始密鑰做MD5算hash值,再用前8位數據對應補全后8位
public static byte[] GetKeyBytes(String strKey) throws Exception {
if (null == strKey || strKey.length() < 1)
throw new Exception("key is null or empty!");
java.security.MessageDigest alg = java.security.MessageDigest.getInstance("MD5");
alg.update(strKey.getBytes());
byte[] bkey = alg.digest();
System.out.println("md5key.length=" + bkey.length);
System.out.println("md5key=" + byte2hex(bkey));
int start = bkey.length;
byte[] bkey24 = new byte[24];
for (int i = 0; i < start; i++) {
bkey24[i] = bkey[i];
}
for (int i = start; i < 24; i++) {//為了與.net16位key兼容
bkey24[i] = bkey[i - start];
}
System.out.println("byte24key.length=" + bkey24.length);
System.out.println("byte24key=" + byte2hex(bkey24));
return bkey24;
}
private static final String Algorithm = "DESede"; //定義 加密算法,可用 DES,DESede,Blowfish
//keybyte為加密密鑰,長度為24字節
//src為被加密的數據緩沖區(源)
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字節
//src為加密后的緩沖區
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;
}
//轉換成base64編碼
public static String byte2Base64(byte[] b) {
return Base64.encode(b);
}
//轉換成十六進制字符串
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();
}
}
2、JAVA 算法調用范例
package test.com;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import Common.JUtility.EncryptUtils;//用原始密鑰經MD5轉換和補位后的新密鑰來做3DES加密解密
public class Test3DES {
public static void main(String[] args) {
String key = "abcd1234";
String password = "password";
System.out.println("key=" + key + ",password=" + password);
System.out.println();
System.out.println("----------示例開始:使用java寫的算法加密解密-----------");
try {
String encrypt = "";
String decrypt = "";
byte[] bkey = EncryptUtils.GetKeyBytes(key);
encrypt = EncryptUtils.byte2Base64(EncryptUtils.encryptMode(bkey, password.getBytes()));
System.out.println("用預轉換密鑰算加密結果=" + encrypt);
System.out.println("加密后base64表示=" + EncryptUtils.byte2hex(Base64.decode(encrypt)));
System.out.println("調用原始密鑰算加密結果=" + EncryptUtils.Encrypt3DES(password, key));
try {
decrypt = new String(EncryptUtils.decryptMode(bkey, Base64.decode(encrypt)));
System.out.println("用預轉換密鑰算解密結果=" + decrypt);
System.out.println("調用原始密鑰算解密結果=" + EncryptUtils.Decrypt3DES(encrypt, key));
} catch (Exception ex) {
System.out.println("Exception:" + ex.getMessage());
}
} catch (Exception ex) {
System.out.println("Exception:" + ex.getMessage());
}
System.out.println("----------示例結束:使用java寫的算法加密解密-----------");
}
}
3、(7)的運算結果
key=abcd1234,password=password
----------示例開始:使用java寫的算法加密解密-----------
md5key.length=16
md5key=E1:9D:5C:D5:AF:03:78:DA:05:F6:3F:89:1C:74:67:AF
byte24key.length=24
byte24key=E1:9D:5C:D5:AF:03:78:DA:05:F6:3F:89:1C:74:67:AF:E1:9D:5C:D5:AF:03:78:DA
用預轉換密鑰算加密結果=ftwPzxFH4WpzT4Orq8uSLQ==
加密后base64表示=7E:DC:0F:CF:11:47:E1:6A:73:4F:83:AB:AB:CB:92:2D
md5key.length=16
md5key=E1:9D:5C:D5:AF:03:78:DA:05:F6:3F:89:1C:74:67:AF
byte24key.length=24
byte24key=E1:9D:5C:D5:AF:03:78:DA:05:F6:3F:89:1C:74:67:AF:E1:9D:5C:D5:AF:03:78:DA
調用原始密鑰算加密結果=ftwPzxFH4WpzT4Orq8uSLQ==
用預轉換密鑰算解密結果=password
md5key.length=16
md5key=E1:9D:5C:D5:AF:03:78:DA:05:F6:3F:89:1C:74:67:AF
byte24key.length=24
byte24key=E1:9D:5C:D5:AF:03:78:DA:05:F6:3F:89:1C:74:67:AF:E1:9D:5C:D5:AF:03:78:DA
調用原始密鑰算解密結果=password
----------示例結束:使用java寫的算法加密解密-----------