Java密码加密与解密
java中对代码进行加密与解密,其中用MD5方式的是不可逆的。
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
@SuppressWarnings("unused")
public class PasswordUtils {
//加密
@SuppressWarnings({})
public static String encrypt(String plainText) {
String result = "";
try {
DESKeySpec keySpec = new DESKeySpec("CampusHelper".getBytes("UTF8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(keySpec);
sun.misc.BASE64Encoder base64encoder = new sun.misc.BASE64Encoder();
sun.misc.BASE64Decoder base64decoder = new sun.misc.BASE64Decoder();
// ENCODE plainTextPassword String
byte[] cleartext = plainText.getBytes("UTF8");
Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread
// safe
cipher.init(Cipher.ENCRYPT_MODE, key);
result = base64encoder.encode(cipher.doFinal(cleartext));
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
//解密
@SuppressWarnings({})
public static String decrypt(String encryptedPwd)
throws InvalidKeyException, UnsupportedEncodingException {
String result = "";
try {
DESKeySpec keySpec = new DESKeySpec("CampusHelper".getBytes("UTF8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(keySpec);
sun.misc.BASE64Encoder base64encoder = new sun.misc.BASE64Encoder();
sun.misc.BASE64Decoder base64decoder = new sun.misc.BASE64Decoder();
// DECODE encryptedPwd String
byte[] encrypedPwdBytes = base64decoder.decodeBuffer(encryptedPwd);
Cipher cipher = Cipher.getInstance("DES");// cipher is not thread
// safe
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] plainTextPwdBytes = (cipher.doFinal(encrypedPwdBytes));
result = new String(plainTextPwdBytes);
} catch (Exception e) {
System.out.println("解密失败:"+encryptedPwd);
result=againDecrypt(encryptedPwd);
}
return result;
}
public static String againDecrypt(String encryptedPwd)
throws InvalidKeyException, UnsupportedEncodingException {
String result = "";
try {
encryptedPwd=URLDecoder.decode(encryptedPwd,"utf-8");
DESKeySpec keySpec = new DESKeySpec("CampusHelper".getBytes("UTF8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(keySpec);
sun.misc.BASE64Encoder base64encoder = new sun.misc.BASE64Encoder();
sun.misc.BASE64Decoder base64decoder = new sun.misc.BASE64Decoder();
// DECODE encryptedPwd String
byte[] encrypedPwdBytes = base64decoder.decodeBuffer(encryptedPwd);
Cipher cipher = Cipher.getInstance("DES");// cipher is not thread
// safe
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] file=cipher.doFinal(encrypedPwdBytes);
byte[] plainTextPwdBytes = (file);
result = new String(plainTextPwdBytes);
} catch (Exception e) {
//e.printStackTrace();
System.out.println("重新解密失败:"+encryptedPwd);
}
return result;
}
public static String generateMD5(String plainText) {
String result = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
byte[] digest = md.digest();
StringBuffer sb = new StringBuffer();
for (byte b : digest) {
sb.append(Integer.toHexString(b & 0xff));
}
result = sb.toString();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
try {
//System.out.println(decrypt(URLDecoder.decode("BrhT6ggt4%252BI%253D","utf-8")));
//System.out.println(decrypt(URLDecoder.decode(URLDecoder.decode("BrhT6ggt4%252BI%253D","utf-8"))));
//System.out.println(URLEncoder.encode(encrypt("41"), "utf-8"));
//System.out.println(encrypt("41"));
// long now = System.currentTimeMillis();
// System.out.println(now);
// now = now - now / 10000000000L * 10000000000L;
// System.out.println(now);
System.out.println(decrypt("DAD0dZU0S/w/pJcrfPW4Fw=="));
//System.out.println(encrypt("321088199511214887"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
例2
/**
* 利用MD5加密
*
* @param source
* @return
*/
public static String getMD5(byte[] source) {
String s = null;
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };// 用来将字节转换成16进制表示的字符
try {
java.security.MessageDigest md = java.security.MessageDigest
.getInstance("MD5");
md.update(source);
byte tmp[] = md.digest();// MD5 的计算结果是一个 128 位的长整数,
// 用字节表示就是 16 个字节
char str[] = new char[16 * 2];// 每个字节用 16 进制表示的话,使用两个字符, 所以表示成 16
// 进制需要 32 个字符
int k = 0;// 表示转换结果中对应的字符位置
for (int i = 0; i < 16; i++) {// 从第一个字节开始,对 MD5 的每一个字节// 转换成 16
// 进制字符的转换
byte byte0 = tmp[i];// 取第 i 个字节
str[k++] = hexDigits[byte0 >>> 4 & 0xf];// 取字节中高 4 位的数字转换,// >>>
// 为逻辑右移,将符号位一起右移
str[k++] = hexDigits[byte0 & 0xf];// 取字节中低 4 位的数字转换
}
s = new String(str);// 换后的结果转换为字符串
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return s;
}