zoukankan      html  css  js  c++  java
  • Base64编码加密

    package liferay;
    
    
    
    public class Base64 {
    
    	public static final char EQUAL = '=';
    	public static final char PLUS = '+';
    	public static final char QUESTION = '?';
    	public static final char FORWARD_SLASH = '/';
    	public static final char SLASH = FORWARD_SLASH;
    
    	public static String encode(byte raw[], int offset, int length) {
    		int lastIndex = Math.min(raw.length, offset + length);
    
    		StringBuilder sb = new StringBuilder(((lastIndex - offset) / 3 + 1) * 4);
    
    		for (int i = offset; i < lastIndex; i += 3) {
    			sb.append(encodeBlock(raw, i, lastIndex));
    		}
    
    		return sb.toString();
    	}
    	
    	public static String encode(byte raw[]) {
    		return encode(raw, 0, raw.length);
    	}
    
    	protected static char[] encodeBlock(byte raw[], int offset, int lastIndex) {
    		int block = 0;
    		int slack = lastIndex - offset - 1;
    		int end = slack < 2 ? slack : 2;
    
    		for (int i = 0; i <= end; i++) {
    			byte b = raw[offset + i];
    
    			int neuter = b >= 0 ? ((int) (b)) : b + 256;
    			block += neuter << 8 * (2 - i);
    		}
    
    		char base64[] = new char[4];
    
    		for (int i = 0; i < 4; i++) {
    			int sixbit = block >>> 6 * (3 - i) & 0x3f;
    			base64[i] = getChar(sixbit);
    		}
    
    		if (slack < 1) {
    			base64[2] = EQUAL;
    		}
    
    		if (slack < 2) {
    			base64[3] = EQUAL;
    		}
    
    		return base64;
    	}
    
    	protected static char getChar(int sixbit) {
    		if ((sixbit >= 0) && (sixbit <= 25)) {
    			return (char) (65 + sixbit);
    		}
    
    		if ((sixbit >= 26) && (sixbit <= 51)) {
    			return (char) (97 + (sixbit - 26));
    		}
    
    		if ((sixbit >= 52) && (sixbit <= 61)) {
    			return (char) (48 + (sixbit - 52));
    		}
    
    		if (sixbit == 62) {
    			return PLUS;
    		}
    
    		return sixbit != 63 ? QUESTION : SLASH;
    	}
    	
    	
    	public static void main(String[] args) {
    		
    		//Base64加密
    		
    		
    		
    		
    		System.out.println("===========================================================");
    		byte[] raw = new byte[]{'A','B','C','D','E','F','G'};
    		char [] c = encodeBlock(raw,3,4);
    		for (int i=0;i<c.length;i++) {
    			System.out.print(c[i]);
    		}
    		
    		
    		System.out.println("==================================");
    		String str = encode(raw);
    		
    		System.out.println(str);
    		
    		
    		
    		
    		
    	}
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    }
    

      

  • 相关阅读:
    BNU 51002 BQG's Complexity Analysis
    BNU OJ 51003 BQG's Confusing Sequence
    BNU OJ 51000 BQG's Random String
    BNU OJ 50999 BQG's Approaching Deadline
    BNU OJ 50998 BQG's Messy Code
    BNU OJ 50997 BQG's Programming Contest
    CodeForces 609D Gadgets for dollars and pounds
    CodeForces 609C Load Balancing
    CodeForces 609B The Best Gift
    CodeForces 609A USB Flash Drives
  • 原文地址:https://www.cnblogs.com/airycode/p/4807333.html
Copyright © 2011-2022 走看看