我在用base64做密码加密解密的时候在windows上加密解密是没有问题的,但是在linux系统下加密解密的时候发现每次相同的密码加密后密文不同,导致我把项目部署到linux系统下登录系统的时候老是提示我密码不对,以下是我解决问题的总结,希望对大家有帮助,在两种情况下对密码加密的解决一种是cas单点登录下的密码加密,另一种是普通的登录加密解决。废话不多说直入主题,代码:
一:在cas单点登录下密码加密的解决:
package org.nci.platform.utils;
import java.io.PrintStream;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.jasig.cas.authentication.handler.PasswordEncoder;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Base64Util implements PasswordEncoder
{
private static final int LENGTH = 128;
private static final String ENCODE = "UTF-8";
private static final String defaultKey = "Mi_mAWo.La0+nAnlE";
private static final String defaultPrefix = "==";
public static String encrypt(String content)
{
String value = "";
try {
if (!isEmpty(content))
value = "==" + base64Encode(aesEncryptToBytes(content));
}
catch (Exception e) {
System.out.println("EncryptAndDecrypt(加密错误)");
e.printStackTrace();
}
return value;
}
public static String decrypt(String encryptStr)
{
String value = "";
try {
int length = "==".length();
if (encryptStr.length() > length) {
String val = encryptStr.substring(0, length);
if (val.equals("=="))
value = aesDecryptByBytes(base64Decode(encryptStr.substring(length)));
else
value = encryptStr;
}
else {
value = encryptStr;
}
} catch (Exception e) {
System.out.println("EncryptAndDecrypt(解密错误)");
e.printStackTrace();
}
return value;
}
public static byte[] aesEncryptToBytes(String content)
throws Exception
{
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed("Mi_mAWo.La0+nAnlE".getBytes());
kgen.init(128, secureRandom);
Cipher cipher = Cipher.getInstance("AES");
SecretKeySpec sks = new SecretKeySpec(kgen.generateKey().getEncoded(), "AES");
cipher.init(1, sks);
return cipher.doFinal(content.getBytes("UTF-8"));
}
public static String aesDecryptByBytes(byte[] encryptBytes)
throws Exception
{
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed("Mi_mAWo.La0+nAnlE".getBytes());
kgen.init(128, secureRandom);
Cipher cipher = Cipher.getInstance("AES");
SecretKeySpec sks = new SecretKeySpec(kgen.generateKey().getEncoded(), "AES");
cipher.init(2, sks);
byte[] decryptBytes = cipher.doFinal(encryptBytes);
return new String(decryptBytes);
}
public static String base64Encode(byte[] bytes)
{
return new BASE64Encoder().encode(bytes);
}
public static byte[] base64Decode(String base64Code)
throws Exception
{
return isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);
}
public static boolean isEmpty(String str) {
return (str == null) || ("".equals(str.trim()));
}
public String encode(String password) {
String end = encrypt(password);
return end;
}
}
实现PasswordEncoder接口需要把cas-server-core-4.0.0.jar(该jar包可以去我的csdn下载)放入cas的lib下把上述的代码块打成jar包放入cas的lib目录下如下图:
同时需要在deployerConfigContext.xml文件配置,如下图:
二:在普通登录下密码加密的解决:
还是和上面的方式一样,只是不需要实现PasswordEncoder接口。
结果:
总结:
注意:在openjdk1.7下是不可以的,在jdk1.7下和openjdk1.8(包括openjdk1.8)以上是可以的
第一次遇到这样的问题,最后解决了。在开发中会遇到各种各样的问题,有问题不怕,我们还是有一个去解决问题的心。希望对各位有所帮助。