Base 64 인 코딩 원리
Base64 64 。
exe、jpg、pdf , , , , , 。Base64 。
Base64 , , 64 :
['A', 'B', 'C', ... 'a', 'b', 'c', ... '0', '1', ... '+', '/']
, , 3 , 3x8=24bit, 4 , 6 bit:
base64-encode
4 , , 4 , 。
,Base64 3 4 , 33%, 、 。
3 , 1 2 ?Base64 \x00 , 1 2 = , , , 。
Python base64 base64 :
>>> import base64
>>> base64.b64encode('binary\x00string')
'YmluYXJ5AHN0cmluZw=='
>>> base64.b64decode('YmluYXJ5AHN0cmluZw==')
'binary\x00string'
Base64 + /, URL , "url safe" base64 , + / - _:
>>> base64.b64encode('i\xb7\x1d\xfb\xef\xff')
'abcd++//'
>>> base64.urlsafe_b64encode('i\xb7\x1d\xfb\xef\xff')
'abcd--__'
>>> base64.urlsafe_b64decode('abcd--__')
'i\xb7\x1d\xfb\xef\xff'
64 , Base64 , , 。
Base64 , , 。
Base64 , 、Cookie 。
= Base64 , = URL、Cookie , , Base64 = :
# Base64:
'abcd' -> 'YWJjZA=='
# =:
'abcd' -> 'YWJjZA'
= ? Base64 3 4 , ,Base64 4 , , = Base64 4 , 。
= base64 :
>>> base64.b64decode('YWJjZA==')
'abcd'
>>> base64.b64decode('YWJjZA')
Traceback (most recent call last):
...
TypeError: Incorrect padding
>>> safe_b64decode('YWJjZA')
'abcd'
구현 코드
/**
* BASE64
*
*/
public class Base64Util {
private static final char intToBase64[] = {'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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
private static final byte base64ToInt[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
-1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 };
/**
* byte BASE64
*
* @param data byte
* @return <b>String</b> BASE64 <b><br/>null</b>
*/
public static String byteArrayToBase64(byte[] data) {
if(data==null || data.length==0){
return null;
}
int len = data.length;
int groups = len/3;
int nogroups = len-3*groups;
int resultLen = (len+2)/3*4;
StringBuffer result = new StringBuffer(resultLen);
int cursor = 0;
for(int i=0;i<groups;i++){
int byte0 = data[cursor++]&0xff;
int byte1 = data[cursor++]&0xff;
int byte2 = data[cursor++]&0xff;
result.append(intToBase64[byte0>>2]);
result.append(intToBase64[(byte0<<4)&0x3f|(byte1>>4)]);
result.append(intToBase64[(byte1<<2)&0x3f|(byte2>>6)]);
result.append(intToBase64[byte2&0x3f]);
}
if(nogroups!=0){
int byte0 = data[cursor++]&0xff;
result.append(intToBase64[byte0>>2]);
if(nogroups==1){
result.append(intToBase64[(byte0<<4)&0x3f]);
result.append("==");
}else{
int byte1 = data[cursor++]&0xff;
result.append(intToBase64[(byte0<<4)&0x3f|(byte1>>4)]);
result.append(intToBase64[(byte1<<2)&0x3f]);
result.append('=');
}
}
return result.toString();
}
/**
* BASE64 byte
*
* @param data BASE64
* @return <b>String</b> byte <b><br/>null</b>
*/
public static byte[] base64ToByteArray(String data) {
if(StringUtil.isEmpty(data)){
return null;
}
int len = data.length();
int groups = len/4;
if(groups*4!=len){
return null;
}
int nogroups = 0;
int fullGroups = groups;
if(len!=0){
if(data.charAt(len-1)=='='){
nogroups++;
fullGroups--;
}
if(data.charAt(len-2)=='='){
nogroups++;
}
}
byte[] result = new byte[groups*3-nogroups];
int inCursor = 0;
int outCursor = 0;
try {
for(int i=0;i<fullGroups;i++){
int ch0 = base64toInt(data.charAt(inCursor++));
int ch1 = base64toInt(data.charAt(inCursor++));
int ch2 = base64toInt(data.charAt(inCursor++));
int ch3 = base64toInt(data.charAt(inCursor++));
result[outCursor++] = (byte) ((ch0<<2)|(ch1>>4));
result[outCursor++] = (byte) ((ch1<<4)|(ch2>>2));
result[outCursor++] = (byte) ((ch2<<6)|ch3);
}
if(nogroups!=0){
int ch0 = base64toInt(data.charAt(inCursor++));
int ch1 = base64toInt(data.charAt(inCursor++));
result[outCursor++] = (byte) ((ch0<<2)|(ch1>>4));
if(nogroups==1){
int ch2 = base64toInt(data.charAt(inCursor++));
result[outCursor++] = (byte) ((ch1<<4)|(ch2>>2));
}
}
} catch (Exception e) {
return null;
}
return result;
}
private static int base64toInt(char c) {
int result = base64ToInt[c];
if(result<0){
throw new RuntimeException();
}
return result;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
이제 리더 코드를 읽었다면~ I부~"patte 보기"를 통해 알 수 있는 코드입니다. 함수/변수 이름에 명확한 정보를 삽입하여 필요 없는 주석을 줄이고 전체 코드를 간소화한다. 제1장: 목적 '이해하기 쉬운 코드=읽기 쉬운 코드=타인이 가장 짧은 시...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.