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.