[자바스크립트]Base64 Endode, Decode |
function encode_base64( what )
{
var base64_encodetable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var result = "";
var len = what.length;
var x, y;
var ptr = 0;
while( len-- > 0 )
{
x = what.charCodeAt( ptr++ );
result += base64_encodetable.charAt( ( x >> 2 ) & 63 );
if( len-- <= 0 )
{
result += base64_encodetable.charAt( ( x << 4 ) & 63 );
result += "==";
break;
}
y = what.charCodeAt( ptr++ );
result += base64_encodetable.charAt( ( ( x << 4 ) | ( ( y >> 4 ) & 15 ) ) & 63 );
if ( len-- <= 0 )
{
result += base64_encodetable.charAt( ( y << 2 ) & 63 );
result += "=";
break;
}
x = what.charCodeAt( ptr++ );
result += base64_encodetable.charAt( ( ( y << 2 ) | ( ( x >> 6 ) & 3 ) ) & 63 );
result += base64_encodetable.charAt( x & 63 );
}
return result;
}
function decode_base64( what )
{
var base64_decodetable = new Array (
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, 255, 255,
255, 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, 255, 255, 255, 255, 255,
255, 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, 255, 255, 255, 255, 255
);
var result = "";
var len = what.length;
var x, y;
var ptr = 0;
while( !isNaN( x = what.charCodeAt( ptr++ ) ) )
{
if( x == 13 || x == 10 )
continue;
if( ( x > 127 ) || (( x = base64_decodetable[x] ) == 255) )
return false;
if( ( isNaN( y = what.charCodeAt( ptr++ ) ) ) || (( y = base64_decodetable[y] ) == 255) )
return false;
result += String.fromCharCode( (x << 2) | (y >> 4) );
if( (x = what.charCodeAt( ptr++ )) == 61 )
{
if( (what.charCodeAt( ptr++ ) != 61) || (!isNaN(what.charCodeAt( ptr ) ) ) )
return false;
}
else
{
if( ( x > 127 ) || (( x = base64_decodetable[x] ) == 255) )
return false;
result += String.fromCharCode( (y << 4) | (x >> 2) );
if( (y = what.charCodeAt( ptr++ )) == 61 )
{
if( !isNaN(what.charCodeAt( ptr ) ) )
return false;
}
else
{
if( (y > 127) || ((y = base64_decodetable[y]) == 255) )
return false;
result += String.fromCharCode( (x << 6) | y );
}
}
}
return result;
}
'IT-개발,DB' 카테고리의 다른 글
[개발] [웹개발] 자바스크립트 개체의 속성과 메소드 리스트 (0) | 2010.10.26 |
---|---|
[개발] ASP.NET base64로 문자열 암호화 하기 (0) | 2010.10.26 |
[ASP.NET] asp.net에서 웹 애플리케이션 예외처리 (퍼온글) (0) | 2010.10.25 |
[ASP.NET] 로그인한 국가별 시간대별 DateTime 리턴 (0) | 2010.10.25 |
[ASP.NET] GridView의 스크롤기능을 추가했을경우 사용되는 Div 태그의 Width 값 해상도에 맞게 설정해 주기 (0) | 2010.10.25 |
댓글