编码ASCII字符_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 编码ASCII字符

编码ASCII字符

 2011/10/14 9:38:49  Crusader  http://crusader.iteye.com  我要评论(0)
  • 摘要:类似Base64的功能,即对目标字符串编码为不易识别的编码串,同时提供解码。目前暂时只支持对基于ASCII的字串编解码。源文件下载:http://aobama.googlecode.com/files/Obama.java·相比base64编码后的字符/节串,其长度更精短,base64编码后的字串会比原串长1/3左右,该方法为1/6·编码/解码速度更快(相比apachecommons中的base64实现),对700字节的字符串编/解码100万次(单位:nanoTime):·base64
  • 标签:

类似Base64的功能,即对目标字符串编码为不易识别的编码串,同时提供解码。目前暂时只支持对基于ASCII的字串编解码。
源文件下载:http://aobama.googlecode.com/files/Obama.java

    ·相比base64编码后的字符/节串,其长度更精短,base64编码后的字串会比原串长1/3左右,该方法为1/6

    ·编码/解码速度更快(相比apache commons中的base64实现),对700字节的字符串编/解码100万次(单位:nanoTime):

        ·base64:32951698610

        ·aobama:18900134515

    ·提供对同一源字串BLUFF编码功能:

        ·例如,源字符串为:http://code.google.com/hosting/createProject01

        ·通过打开BLUFF开关,对该原串多次encode调用,每次将产生不同的编码串,但对这些不同的编码结果都能解码回源串,例如:

                ·第一次:Kk2_Aunjjxh1YGoPYW1T20SWh1Wo1PxZAjjhexhKqE4PEmKJW0lV_ZZA

                ·第二次:rkmgfeBNjz6XsMcPslXRmDSl6XlcXPzPfNN6ez6rbCQPC2rIlDlSgPPf

                ·第三次:jkrXlcbPjwigd9ePdfgOr8SfigfegPwNlPPiewimBv5PvKm7f8lpXNNl

                ·第四次:Wk-HjxhAjun4ktFPkZ4L-USZn4ZF4PuWjAAneunJ3a1Pa7JKZUlyHWWj

               ·...

        ·对上述四个不同的编码结果调用decode方法都将得到源串http://code.google.com/hosting/createProject01

/** 
 *  类似Base64的功能, 目前暂时只支持对基于ASCII的字串编解码
 * 	1) 编码指定字节/符(暂时只能编码ASCII码), 每6个字节/符前用一个字节/符来
 * 	         标识这4个字节/符mod64的值(0或1);
 *  2) 指定BLUFF为TRUE, 则对相同字节/符每次产生一定程度上随机的编码结果(1/52)。如果
 * 	         指定为FALSE(默认),则对相同字节/符每次编码的结果是固定的
 * 
 *  @author Crusader
 *  @version 1.0
 *  Date: 2011-10-13
 * */
public class Obama {
	
	private static final int MASK_64 = 0x3F;
	private static final byte DEF_SECRET = 0x43;
	
//	static final byte[] CHUNK_SEPARATOR = { 13, 10 };
	
	public static boolean BLUFF = false;
	
//	public static boolean CHUNK = false;//76
//	public static final int CHUNK_LENGTH = 76;
	
	/** 编码基表 */
	private static final char base_encode[] = {
		// 默认序列
	    'P', 'e', 'r', 'Q', 'f', 'w', '7', 'g', 'i', 'p', 	/*  0- 9 */
	    '8', '9', 'B', 'd', 'O', 'v', '6', 'S', 'D', 'M', 	/* 10-19 */
	    'b', 's', 'R', 'C', 'N', 'c', 'm', '5', 'l', 'z', 	/* 20-29 */
	    'I', 'X', 'o', 'j', 'H', '2', 'x', 'W', '1', 'J', 	/* 30-39 */
	    'V', 'h', 'G', '0', 'Y', 'q', 'E', 'T', 'k', '3', 	/* 40-49 */
	    'a', 'L', 'y', 'n', 't', 'U', 'u', 'Z', '4', 'K', 	/* 50-59 */
	    'F', 'A', '_', '-'									/* 60-63 */ //95,45, 
	};
	
	/** 解码参照表 */
	private static int base_decode[] = {
		// 参照编码表默认序列的值序
		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  			/*  0- 9 */
		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  			/*  10- 19 */
		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  			/*  20- 29 */
		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  			/*  30- 39 */
		-1, -1, -1, -1, -1, 63, -1, -1, 43, 38,  			/*  40- 49 */
		35, 49, 58, 27, 16, 6, 10, 11, -1, -1,  			/*  50- 59 */
		-1, -1, -1, -1, -1, 61, 12, 23, 18, 46,  			/*  60- 69 */
		60, 42, 34, 30, 39, 59, 51, 19, 24, 14,  			/*  70- 79 */
		0, 3, 22, 17, 47, 55, 40, 37, 31, 44,  				/*  80- 89 */
		57, -1, -1, -1, -1, 62, 0, 50, 20, 25,  			/*  90- 99 */
		13, 1, 4, 7, 41, 8, 33, 48, 28, 26,  				/*  100- 109 */
		53, 32, 9, 45, 2, 21, 54, 56, 15, 5,  				/*  110- 119 */
		36, 52, 29, -1, -1, -1, -1, -1, -1		 			/*  120- 128 */	
	};
	
	/**
	 * 重排序编码基表和对应的解码表
	 */
	public static void init(){
		
		// 重排序编码基表
		ArrayList<Character> list = new ArrayList<Character>();
		for(char c : base_encode)
			list.add(c);
		Collections.shuffle(list);
		for(int i=0; i<list.size(); i++)
			base_encode[i] = list.get(i);
		
		// 根据重排序后的编码基表初始化解码表
		initDecode();
	}
	
	private static void initDecode(){
		Arrays.fill(base_decode, -1);
		for(int i=0; i<base_encode.length; i++){
			base_decode[base_encode[i]] = i;
		}
	}
	
	public static void setDecode(int[] decode){
		base_decode = null;
		base_decode = decode;
		
		for(int i=0; i<base_decode.length; i++){
			if(base_decode[i] < 0)
				continue;
			else
				base_encode[base_decode[i]] = (char)i;
		}
	}
	
	
	/** 
	 * 	编码
	 * */
	public static byte[] encodeBytes(byte[] content){
		
		if(content==null || content.length==0)
			return null;
		
		int len = content.length;
		
		byte[] cArray = new byte[len + (int)Math.ceil(len/6.0d) + 1];
		
		if(BLUFF){
			int s = 0;
			int r = new Random().nextInt(26);
			boolean u = ((r ^ len) & 1) == 1;
			s = u ? 65 : 97;
			cArray[0] = (byte)(r + s);
		}else{
			cArray[0] = DEF_SECRET;
		}
		
		
		byte c = 0;
		int n = 0;
		int mark = 0;
		int pos = 0;
		int segs = 0;
		for(int i=0; i<len; i+=6){
			mark = 1+i+segs;
			for(int k=0; (k<6) && (pos<len); k++){
				c = content[pos];
				n = c ^ cArray[0]; 
				n ^= k;
				cArray[mark] |= ((n>>>6)<<k); 
				
				cArray[mark+k+1] = (byte)base_encode[n & MASK_64];
				pos++;
			}
			segs++;
			cArray[mark] = (byte)base_encode[cArray[mark]];
		}
		
		return cArray;
		
	}
	
	public static byte[] encodeBytes(String content){
		
		if(content==null || content.length()==0)
			return null;
		
		int len = content.length();
		
		byte[] cArray = new byte[len + (int)Math.ceil(len/6.0d) + 1];
		
		if(BLUFF){
			int s = 0;
			int r = new Random().nextInt(26);
			boolean u = ((r ^ len) & 1) == 1;
			s = u ? 65 : 97;
			cArray[0] = (byte)(r + s);
		}else{
			cArray[0] = DEF_SECRET;
		}
		
		char c = '0';
		int n = 0;
		int mark = 0;
		int pos = 0;
		int segs = 0;
		
		for(int i=0; i<len; i+=6){
			mark = 1+i+segs;
			for(int k=0; (k<6) && (pos<len); k++){
				c = content.charAt(pos);
				n = c ^ cArray[0]; 
				n ^= k;
				cArray[mark] |= ((n>>>6)<<k); 
				
				cArray[mark+k+1] = (byte)base_encode[n & MASK_64];
				pos++;
			}
			segs++;
			cArray[mark] = (byte)base_encode[cArray[mark]];
		}
		
		return cArray;
		
	}
	
	public static String encodeString(String content){
		
		if(content==null || content.length()==0)
			return null;
		
		int len = content.length();
		
		char[] cArray = new char[len + (int)Math.ceil(len/6.0d) + 1];
		
		if(BLUFF){
			int s = 0;
			int r = new Random().nextInt(26);
			boolean u = ((r ^ len) & 1) == 1;
			s = u ? 65 : 97;
			cArray[0] = (char)(r + s);
		}else{
			cArray[0] = (char)DEF_SECRET;
		}
		
		char c = '0';
		int n = 0;
		int mark = 0;
		int pos = 0;
		int segs = 0;
		for(int i=0; i<len; i+=6){
			mark = 1+i+segs;
			for(int k=0; (k<6) && (pos<len); k++){
				c = content.charAt(pos);
				n = c ^ cArray[0]; 
				n ^= k;
				cArray[mark] |= ((n>>>6)<<k); 
				
				cArray[mark+k+1] = base_encode[n & MASK_64];
				pos++;
			}
			segs++;
			cArray[mark] = base_encode[cArray[mark]];
		}
		
		return new String(cArray);
		
	}
	
	public static String encodeString(byte[] content){
		
		if(content==null || content.length==0)
			return null;
		
		int len = content.length;
		
		char[] cArray = new char[len + (int)Math.ceil(len/6.0d) + 1];
		
		if(BLUFF){
			int s = 0;
			int r = new Random().nextInt(26);
			boolean u = ((r ^ len) & 1) == 1;
			s = u ? 65 : 97;
			cArray[0] = (char)(r + s);
		}else{
			cArray[0] = (char)DEF_SECRET;
		}
		
		byte c = 0;
		int n = 0;
		int mark = 0;
		int pos = 0;
		int segs = 0;
		for(int i=0; i<len; i+=6){
			mark = 1+i+segs;
			for(int k=0; (k<6) && (pos<len); k++){
				c = content[pos];
				n = c ^ cArray[0]; 
				n ^= k;
				cArray[mark] |= ((n>>>6)<<k); 
				
				cArray[mark+k+1] = base_encode[n & MASK_64];
				pos++;
			}
			segs++;
			cArray[mark] = base_encode[cArray[mark]];
		}
		
		return new String(cArray);
		
	}
	
	/** 解码 */
	public static String decodeString(String content){
		
		if(content==null || content.length()==0)
			return null;
		
		int len = content.length();
		
		char[] cArray = new char[len - 1 - (int)Math.ceil((len-1)/7.0)];
		
		char secret = content.charAt(0);
		char c = '0';
		int mark = 0;
		int index = 0;
		for(int i=1; i<len; i+=7){
			mark = base_decode[content.charAt(i)];
			for(int k=0, pos=i + k + 1; k<6 && pos<len; k++, pos++){
				c = content.charAt(pos);
//				cArray[index] = (char)((base_decode[c] + 64*((mark>>>k)&1))^ k ^secret);
				cArray[index] = (char)((base_decode[c] + (((mark>>>k)&1)<<6))^ k ^secret);
				index++;
			}
		}
		
		return new String(cArray);
	}
	
	public static String decodeString(byte[] content){
		
		if(content==null || content.length==0)
			return null;
		
		int len = content.length;
		
		char[] cArray = new char[len - 1 - (int)Math.ceil((len-1)/7.0)];
		
		byte secret = content[0];
		byte c = 0;
		int mark = 0;
		int index = 0;
		for(int i=1; i<len; i+=7){
			mark = base_decode[content[i]];
			for(int k=0, pos=i + k + 1; k<6 && pos<len; k++, pos++){
				c = content[pos];
				cArray[index] = (char)((base_decode[c] + (((mark>>>k)&1)<<6))^ k ^secret);
				index++;
			}
		}
		
		return new String(cArray);
	}
	
	public static byte[] decodeBytes(byte[] content){
		
		if(content==null || content.length==0)
			return null;
		
		int len = content.length;
		
		byte[] cArray = new byte[len - 1 - (int)Math.ceil((len-1)/7.0)];
		
		byte secret = content[0];
		byte c = 0;
		int mark = 0;
		int index = 0;
		for(int i=1; i<len; i+=7){
			mark = base_decode[content[i]];
			for(int k=0, pos=i + k + 1; k<6 && pos<len; k++, pos++){
				c = content[pos];
				cArray[index] = (byte)((base_decode[c] + (((mark>>>k)&1)<<6))^ k ^secret);
				index++;
			}
		}
		
		return cArray;
	}
	
	public static byte[] decodeBytes(String content){
		
		if(content==null || content.length()==0)
			return null;
		
		int len = content.length();
		
		byte[] cArray = new byte[len - 1 - (int)Math.ceil((len-1)/7.0)];
		
		byte secret = (byte)content.charAt(0);
		byte c = 0;
		int mark = 0;
		int index = 0;
		for(int i=1; i<len; i+=7){
			mark = base_decode[content.charAt(i)];
			for(int k=0, pos=i + k + 1; k<6 && pos<len; k++, pos++){
				c = (byte)content.charAt(pos);
				cArray[index] = (byte)((base_decode[c] + (((mark>>>k)&1)<<6))^ k ^secret);
				index++;
			}
		}
		
		return cArray;
	}
	
	public static void printBaseEncode(){
		int loop =0;
		for(char n : base_encode){
			System.out.print("'"+n+"', ");
			loop++;
			if(loop%10==0)
				System.out.println("\t/* " + (loop-10) + " - " + (loop-1) + " */" );
		}
	}
	
	public static void printBaseDecode(){
		int loop =0;
		for(int n : base_decode){
			System.out.print((n<10&&n>-1?" ":"") + n + ", ");
			loop++;
			if(loop%10==0)
				System.out.println("\t/* " + (loop-10) + " - " + (loop-1) + " */");
		}
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		
//		init();
		
		printBaseEncode();
		System.out.println();
		printBaseDecode();
		System.out.println();
		
		String content = "!@#$%%^&%^&*()-=|\t\\/?<>[]{}';\"\";:\nStickies is a PC utility I wrote to try to cut down on the number of yellow notes I was leaving stuck to my monitor. It is a computerised version of those notes."+
		"The design goal behind Stickies is that the program is small and simple. Stickies will not mess with your system files, or write to the registry. Stickies stores information in a single text-based ini file."+
		"Stickies will never support animated dancing figures, or play \"Greensleeves\". They are instead yellow rectangular windows onto which you can put some text notes. Once created, they will stay on screen until you take them away. Just like a real sticky piece of paper. ";
		/*
		 * 	http://www.youku.com/v_olist/c_97_a_韩国_s__g_军事_r__lg__im__st__mt__d_1_et_0_fv_0_fl__fc__fe__o_7.html
			http://www.youku.com/v_olist/c_97_a_韩国_s__g_军事_r__lg__im__st__mt__d_1_et_0_fv_0_fl__fc__fe__o_7.html
			http://www.youku.com/v_olist/c_97_a_韩国_s__g_军事_r__lg__im__st__mt__d_1_et_0_fv_0_fl__fc__fe__o_7.html
			http://www.youku.com/v_olist/c_97_a_韩国_s__g_军事_r__lg__im__st__mt__d_1_et_0_fv_0_fl__fc__fe__o_7.html
		 */
		content = "http://www.youku.com/v_olist/c_97_a_%E9%9F%A9%E5%9B%BD_s__g_%E5%86%9B%E4%BA%8B_r__lg__im__st__mt__d_1_et_0_fv_0_fl__fc__fe__o_7.html";
		
		content = "123";
		
		byte[] contents = content.getBytes();
		long start = System.currentTimeMillis();
		Obama.BLUFF = true;
		try{
			for(int i=0; i<1; i++){
				byte[] b = Base64.encodeBase64(contents);
//				Base64.decodeBase64(b);
				
				String de1 = encodeString(content);
//				decodeBytes(de1);
				
//				byte[] b = encodeBytes(contents);
//				decodeBytes(b);
				
				String de2 = encodeString(content.getBytes());
				byte[] de3 = encodeBytes(content.getBytes());
				byte[] de4 = encodeBytes(content);
				System.out.println(de1);
				System.out.println(de2);
				System.out.println(new String(de3));
				System.out.println(new String(de4));
//				
//				System.out.println(URLDecoder.decode(decodeString(de1),"utf-8"));
//				System.out.println(URLDecoder.decode(decodeString(de2),"utf-8"));
//				System.out.println(URLDecoder.decode(decodeString(new String(de3)),"utf-8"));
//				System.out.println(URLDecoder.decode(decodeString(new String(de4)),"utf-8"));
	//			
	//			
				System.out.println(new String(decodeBytes(de1)));
				System.out.println(new String(decodeBytes(de2)));
				System.out.println(new String(decodeBytes(new String(de3))));
				System.out.println(new String(decodeBytes(new String(de4))));
	//			System.out.println(new String(Base64.encodeBase64URLSafe(content.getBytes())));
			}
		}catch(ArrayIndexOutOfBoundsException e){
			e.printStackTrace();
		}
		
		System.out.println(content.length());
		
//		start = System.nanoTime();
//		for(int i=0; i<1000000; i++){
//			byte[] b1 = Base64.encodeBase64(contents);
//			byte[] r1 = Base64.decodeBase64(b1);
//		}
//		System.out.println(System.nanoTime()-start);
//		
//		start = System.nanoTime();
//		for(int i=0; i<1000000; i++){
//			String de1 = encodeString(content);
//			String re = decodeString(de1);
//		}
//		System.out.println(System.nanoTime()-start);
//		
//		start = System.nanoTime();
//		for(int i=0; i<1000000; i++){
//			byte[] b2 = encodeBytes(contents);
//			byte[] r2 = decodeBytes(b2);
//		}
//		
//		System.out.println(System.nanoTime()-start);
		
	}
//32951698610
//18900134515

}
  • 大小: 35.1 KB
  • 查看图片附件
  • 相关文章
发表评论
用户名: 匿名