import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.nio.charset.CharsetEncoder; import org.apache.commons.codec.StringEncoder; import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.digest.DigestUtils; public class EncodingUtil { private static char[] m = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; /** * * enc(ISO-8859-1)转UTF-8 * @param s * @param enc:ISO-8859-1 * @return */ public static String toUTF8(String s,String enc){ if (s==null) { return null; } Charset charset = Charset.forName(enc); CharsetEncoder ce = charset.newEncoder(); if (ce.canEncode(s)) { try { String t = new String(s.getBytes(enc),"UTF-8"); return t; } catch (UnsupportedEncodingException e) { e.printStackTrace(); return s; } }else{ return s; } } /** * base64加密 * @param text * @return */ public static String encodeBase64(String text) { byte[] b = text.getBytes(); Base64 base64 = new Base64(); String s = new String(base64.encode(b)); return s; } /** * base64解密 * @param text * @return */ public static String decodeBase64(String encodeStr) { byte[] b = encodeStr.getBytes(); Base64 base64 = new Base64(); String s = new String(base64.decode(b)); return s; } /** * MD5加密 * @param text * @return */ public static String toMD5(String text){ return DigestUtils.md5Hex(text); } /** * SHA加密 * @param text * @return */ public static String toSHA(String text){ // DigestUtils.sha256Hex(text); // DigestUtils.sha384Hex(text); // DigestUtils.sha512Hex(text); return DigestUtils.shaHex(text); } /** * 位移加密char * @param c * @param move * @return */ private static int encodeMove(char c,int move){ int index = -1; int i = c; if (i>47&&i<58) {//0-9 index = i-48; // System.out.println(c+"--"+m[i-48]); }else if (i>64&&i<91) {//A-Z index = i-55; // System.out.println(c+"--"+m[i-55]); }else if (i>96&&i<123) {//a-z index = i-61; // System.out.println(c+"--"+m[i-61]); }else { return -1; } if (index>-1) {//肯定执行 int a = index+move; int b = a%62; return b; } return -1; } /** * 位移解密char * @param c * @param move * @return */ private static int decodeMove(char c,int move){ int index = -1; int i = c; if (i>47&&i<58) {//0-9 index = i-48; // System.out.println(c+"--"+m[i-48]); }else if (i>64&&i<91) {//A-Z index = i-55; // System.out.println(c+"--"+m[i-55]); }else if (i>96&&i<123) {//a-z index = i-61; // System.out.println(c+"--"+m[i-61]); }else { return -1; } if (index>-1) {//肯定执行 int a = 62+index-move; int b = a%62; return b; } return -1; } /** * 位移加密 * @param text * @param count:位移距离 */ public static String encode(String text,int count){ char[]cs = text.toCharArray(); StringBuffer sb = new StringBuffer(); for (char c : cs) { int i = encodeMove(c, count); char a = m[i]; sb.append(a); } return sb.toString(); } /** * 位移解密 * @param text * @param count * @return */ public static String decode(String text,int count){ char[]cs = text.toCharArray(); StringBuffer sb = new StringBuffer(); for (char c : cs) { int i = decodeMove(c, count); char a = m[i]; sb.append(a); } return sb.toString(); } public static void main(String[] args) { // System.out.println(toUTF8("æ²¹", "iso-8859-1")); // System.out.println(decodeBase64(encodeBase64("中国"))); // System.out.println(new String(DigestUtils.md5Hex("123456"))); System.out.println((int)'0');//48-48=0 System.out.println((int)'A');//65-10=55 System.out.println((int)'Z');// System.out.println((int)'a');//97-36=61 System.out.println((int)'z');// String t = "adfs345678190AZGHJHafsffz"; char[]cs = t.toCharArray(); StringBuffer sb = new StringBuffer(); for (char c : cs) { // int i = c; // if (i>47&&i<58) { // System.out.println(c+"--"+m[i-48]); // }else if (i>64&&i<91) { // System.out.println(c+"--"+m[i-55]); // }else if (i>96&&i<123) { // System.out.println(c+"--"+m[i-61]); // } // System.out.println(c+"--"+m[moveEncode(c, 1)]+"--"+m[moveDecode(m[moveEncode(c, 1)], 1)]); } System.out.println(decode(encode("shiu76582hrhuhHFDHKJS", 2),2)); // System.out.println(-2%10); } }
加密解密类
猜你喜欢
转载自itace.iteye.com/blog/2229402
今日推荐
周排行