加密解密

//第一种加密方式
public static String encodePassword(String password, String algorithm) {
if (algorithm == null) {
return password;
}
byte[] unencodedPassword = password.getBytes();
MessageDigest md = null;
try {
md = MessageDigest.getInstance(algorithm);
} catch (Exception e) {
MyLog.error(e);
log.error("Exception: " + e);
return password;
}
md.reset();
md.update(unencodedPassword);
byte[] encodedPassword = md.digest();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < encodedPassword.length; i++) {
if ((encodedPassword[i] & 0xff) < 0x10) {
buf.append("0");
}
buf.append(Long.toString(encodedPassword[i] & 0xff, 16));
}
return buf.toString();
}

public static String getEncryptPassword(String password) {
return encodePassword(password, "MD5");
}


//第二种加密方式
public static String encodeString(String str) {
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
return encoder.encodeBuffer(str.getBytes()).trim();
}


//解密
public static String decodeString(String str) {
sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder();
try {
return new String(dec.decodeBuffer(str));
} catch (IOException e) {
MyLog.error(e);
throw new RuntimeException(e.getMessage(), e.getCause());
}
}

你可能感兴趣的:(加密,解密,password)