BASE64
package base64;import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;public class BASE {/*** BASE解密* * @param key* @return* @throws Exception*/public static byte[] decryptBASE(String key) throws Exception {return (new BASE64Decoder()).decodeBuffer(key);}/*** BASE加密* * @param key* @return* @throws Exception*/public static String encryptBASE(byte[] key) throws Exception {return (new BASE64Encoder()).encodeBuffer(key);}public static void main(String[] args) {String str = "Hello world";try {String result = BASE.encryptBASE(str.getBytes());System.out.println("result-->加密数据:" + result);byte result1[] = BASE.decryptBASE(result);String str1 = new String(result1);System.out.println("str-->解密数据:" + str1);} catch (Exception e) {e.printStackTrace();}}
}
MD5
package md5;import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;public class MD5Tool {public final static String MD5(String str) {char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','A', 'B', 'C', 'D', 'E', 'F', };byte[] btInput = str.getBytes();try {// 获得MD5摘要算法的MessageDigest对象,提供信息摘要功能MessageDigest mdInst = MessageDigest.getInstance("MD5");// 使用输入的字节更新摘要mdInst.update(btInput);// 获得密文byte[] code = mdInst.digest();// 把密文转换成十六进制的字符串形式char strChar[] = new char[code.length * 2];int k = 0;for (int i = 0; i < code.length; i++) {byte byte1 = code[i];strChar[k++] = hexDigits[byte1 >>> 4 & 0xf];strChar[k++] = hexDigits[byte1 & 0xf];}return new String(strChar);} catch (NoSuchAlgorithmException e) {e.printStackTrace();}return null;}public static void main(String[] args) {System.out.println(MD5Tool.MD5("加密"));System.out.println(MD5Tool.MD5("加密"));System.out.println(MD5Tool.MD5("加密1"));System.out.println("---------------");System.out.println(MD5Tool.MD5("abcdefg"));System.out.println(MD5Tool.MD5("abcdefg"));System.out.println(MD5Tool.MD5("abcdefh"));}
}
SHA1
package sha;import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;public class SHA1 {public static String getSha1(String str){if (null == str || 0 == str.length()){return null;}char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};try {//得到SHA1的MessageDigest对象MessageDigest mdTemp = MessageDigest.getInstance("SHA1");// 使用输入的字节更新摘要mdTemp.update(str.getBytes("UTF-8"));//获得加密信息 byte[] md = mdTemp.digest();int j = md.length;char[] buf = new char[j * 2];int k = 0;//将字节转化为16进制for (int i = 0; i < j; i++) {byte byte0 = md[i];// hexDigits[byte0 >>> 4 & 0xf] ,byte0无符右移4位,取与获得高四位(<=15),buf[k++] = hexDigits[byte0 >>> 4 & 0xf]; //hexDigits[byte0 & 0xf]获取低四位,这也是char str[] = new char[j * 2];的原因,buf[k++] = hexDigits[byte0 & 0xf];}return new String(buf);} catch (NoSuchAlgorithmException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();}return null;}public static void main(String[] args) {String string = "hello world";String enString = SHA1.getSha1(string);System.out.println(string+"加密->"+enString.toUpperCase());}
}
来自网络,方便以后查看