MD5加密
MD5加密代码
package com.hsx.util;
import java.security.MessageDigest;
import sun.misc.*;
/**
* 对指定的数据经行MD5加密
* @author hsx
*
*/
public class MD5Encoder {
@SuppressWarnings("restriction")
public static String encode(String string) {
String result = null;
try {
MessageDigest md = MessageDigest.getInstance("md5");
byte[] b = md.digest(string.getBytes());
BASE64Encoder encoder = new BASE64Encoder();
result = encoder.encode(b);
} catch (Exception e) {
throw new RuntimeException(e);
}
return result;
}
}
使用BASE64Encoder出现的问题的解决方案
问题: 在MyEclipse中编写Java代码时,用到了BASE64Decoder,import sun.misc.BASE64Decoder;可是Eclipse提示:
Access restriction: The type BASE64Decoder is not accessible due to restriction on required library C:\Program files\java\jre6\lib\rt.jar Access restriction : The constructor BASE64Decoder() is not accessible due to restriction on required library C:\Program files\java\jre6\lib\rt.jar解决方案:
1.(推荐使用) 只需要在project build path中先移除JRE System Library,再添加库JRE System Library,重新编译后就一切正常了。- Windows -> Preferences -> Java -> Compiler -> Errors/Warnings -> Deprecated and trstricted API -> Forbidden reference (access rules): -> change to warning
BASE64编码
- BASE64编码原理:把原来的3个字节变为4个字节