"함수 base64 decode()"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(사용자 2명의 중간 판 12개는 보이지 않습니다)
1번째 줄: 1번째 줄:
[[분류: Base64]]
[[분류: Base64]]
[[Category:Encoding]]
[[Category:Encoding]]
;base64_decode
==개요==
*Decodes string using MIME base64 algorithm
{{DISPLAYTITLE:함수 base64_decode()}}
;base64_decode()
* base64 알고리즘으로 문자열 디코딩


==Bash==
==Bash==
{{참고|리눅스 base64 인코딩, 디코딩}}
{{참고|리눅스 base64 인코딩, 디코딩}}
[[category: Bash]]
[[category: Bash]]
<source lang='bash'>
{{소스헤더|마지막 newline 미포함 ★}}
<syntaxhighlight lang='bash'>
echo -n hello world | base64
# aGVsbG8gd29ybGQ=
echo -n aGVsbG8gd29ybGQ= | base64 --decode
# hello world
</syntaxhighlight>
<syntaxhighlight lang='bash'>
echo -n ★A가あ中 | base64
# 4piFQeqwgOOBguS4rQ==
echo 4piFQeqwgOOBguS4rQ== | base64 --decode
# ★A가あ中
</syntaxhighlight>
 
{{소스헤더|마지막 newline 포함}}
<syntaxhighlight lang='bash'>
echo hello world | base64
# aGVsbG8gd29ybGQK
echo aGVsbG8gd29ybGQK | base64 --decode
echo aGVsbG8gd29ybGQK | base64 --decode
# hello world
# hello world
</source>
</syntaxhighlight>
<source lang='bash'>
<syntaxhighlight lang='bash'>
echo ★A가あ中 | base64
# 4piFQeqwgOOBguS4rQo=
echo 4piFQeqwgOOBguS4rQo= | base64 --decode
echo 4piFQeqwgOOBguS4rQo= | base64 --decode
# ★A가あ中
# ★A가あ中
</source>
</syntaxhighlight>
 
==Java==
[[분류: Java]]
{{참고|자바 base64_decode()}}
<syntaxhighlight lang='java'>
import java.util.Base64;
public class MyClass {
    public static void main(String args[]) {
        System.out.println( new String(Base64.getDecoder().decode("VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==")) );
        System.out.println( new String(Base64.getDecoder().decode("4piFQeqwgOOBguS4rQ==")) );
        // This is an encoded string
        // ★A가あ中
    }
}
</syntaxhighlight>


==JavaScript==
==JavaScript==
[[category: JavaScript]]
[[category: JavaScript]]
<source lang='javascript'>
{{참고|JavaScript base64_decode()}}
{{소스헤더|순수 자바스크립트}}
<syntaxhighlight lang='javascript'>
function utf8_decode( str ) { 
  var arr = [], i = ac = c1 = c2 = c3 = 0; 
  str += '';
  while ( i < str.length ) { 
    c1 = str.charCodeAt(i++); if (c1 < 128) { arr[ac++] = String.fromCharCode(c1); continue; }
    c2 = str.charCodeAt(i++); if (c1>191 && c1<224){ arr[ac++] = String.fromCharCode(((c1&31)<<6)|(c2&63)); continue; }
    c3 = str.charCodeAt(i++); arr[ac++] = String.fromCharCode(((c1&15)<<12)|((c2&63)<<6)|(c3&63)); 
  }
  return arr.join('');
function base64_decode(input) {
function base64_decode(input) {
   var keystr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
   var keystr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
   var output = "";
   var output = "";
   var chr1, chr2, chr3;
   var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  var enc1, enc2, enc3, enc4;
   var i = 0;
   var i = 0;
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
   if( /[^A-Za-z0-9\+\/\=]/g.exec(input) ) return null;
   while (i < input.length) {
   while(i < input.length) {
     enc1 = keystr.indexOf(input.charAt(i++));
     enc1 = keystr.indexOf(input.charAt(i++));
     enc2 = keystr.indexOf(input.charAt(i++));
     enc2 = keystr.indexOf(input.charAt(i++));
35번째 줄: 82번째 줄:
     chr3 = ((enc3 & 3) << 6) | enc4;
     chr3 = ((enc3 & 3) << 6) | enc4;
     output = output + String.fromCharCode(chr1);
     output = output + String.fromCharCode(chr1);
     if (enc3 != 64) output += String.fromCharCode(chr2);
     if(enc3 != 64) output += String.fromCharCode(chr2);
     if (enc4 != 64) output += String.fromCharCode(chr3);
     if(enc4 != 64) output += String.fromCharCode(chr3);
   }
   }
   return utf8_decode(output);
   return utf8_decode(output);
}
}
document.write(base64_decode('4piFQeqwgOOBguS4rQ==')); // ★A가あ中
console.log(base64_decode('안녕')); // (null)
</source>
console.log(base64_decode('aGVsbG8gd29ybGQ=')); // hello world
*Dependencies: [[utf8_decode]]
console.log(base64_decode('4piFQeqwgOOBguS4rQ==')); // ★A가あ中
</syntaxhighlight>
{{소스헤더|CryptoJS 활용}}
<syntaxhighlight lang='JavaScript'>
console.log( CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse('4piFQeqwgOOBguS4rQ==')) );
// ★A가あ中
</syntaxhighlight>


==Objective-C==
==Objective-C==
[[category: Objective-C]]
[[category: Objective-C]]
<source lang='objc'>
<syntaxhighlight lang='objc'>
#define CHAR(x) [NSString stringWithFormat:@"%C",x]
#define CHAR(x) [NSString stringWithFormat:@"%C",x]
#define CHAR_AT(str, n) [str substringWithRange:NSMakeRange(n,1)]
#define CHAR_AT(str, n) [str substringWithRange:NSMakeRange(n,1)]
80번째 줄: 133번째 줄:
}
}
NSLog(@"%@", [Util base64_decode:@"4piFQeqwgOOBguS4rQ=="]); // ★A가あ中
NSLog(@"%@", [Util base64_decode:@"4piFQeqwgOOBguS4rQ=="]); // ★A가あ中
</source>
</syntaxhighlight>
*Dependencies: [[CHAR]], [[CHAR_AT]], [[STRPOS]], [[replace_pattern]]
*Dependencies: [[CHAR]], [[CHAR_AT]], [[STRPOS]], [[replace_pattern]]


==PHP==
==PHP==
[[category: PHP]]
[[category: PHP]]
<source lang='php'>
<syntaxhighlight lang='php'>
$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo base64_decode($str);
echo base64_decode($str);
// This is an encoded string
// This is an encoded string
</source>
</syntaxhighlight>
<source lang='php'>
<syntaxhighlight lang='php'>
echo base64_decode('4piFQeqwgOOBguS4rQ==');
echo base64_decode('4piFQeqwgOOBguS4rQ==');
// ★A가あ中
// ★A가あ中
</source>
</syntaxhighlight>
 
==Perl==
[[category:Perl]]
<syntaxhighlight lang='perl'>
use MIME::Base64 qw( decode_base64 );
my $str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
print decode_base64($str);
# This is an encoded string
</syntaxhighlight>


==같이 보기==
==같이 보기==
*[[base64_encode]]
*[[함수 base64_encode()]]
*[[utf8_decode]]
*[[함수 utf8_decode()]]
*[[Base64]]
*[[Base64]]
*[[자바스크립트 window.atob()]]

2020년 11월 2일 (월) 02:36 기준 최신판

1 개요[ | ]

base64_decode()
  • base64 알고리즘으로 문자열 디코딩

2 Bash[ | ]

마지막 newline 미포함 ★
echo -n hello world | base64
# aGVsbG8gd29ybGQ=
echo -n aGVsbG8gd29ybGQ= | base64 --decode
# hello world
echo -n ★A가あ中 | base64
# 4piFQeqwgOOBguS4rQ==
echo 4piFQeqwgOOBguS4rQ== | base64 --decode
# ★A가あ中
마지막 newline 포함
echo hello world | base64
# aGVsbG8gd29ybGQK
echo aGVsbG8gd29ybGQK | base64 --decode
# hello world
echo ★A가あ中 | base64
# 4piFQeqwgOOBguS4rQo=
echo 4piFQeqwgOOBguS4rQo= | base64 --decode
# ★A가あ中

3 Java[ | ]

import java.util.Base64;
public class MyClass {
    public static void main(String args[]) {
        System.out.println( new String(Base64.getDecoder().decode("VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==")) );
        System.out.println( new String(Base64.getDecoder().decode("4piFQeqwgOOBguS4rQ==")) );
        // This is an encoded string
        // ★A가あ中
    }
}

4 JavaScript[ | ]

순수 자바스크립트
function utf8_decode( str ) {  
  var arr = [], i = ac = c1 = c2 = c3 = 0;  
  str += '';
  while ( i < str.length ) {  
    c1 = str.charCodeAt(i++); if (c1 < 128) { arr[ac++] = String.fromCharCode(c1); continue; }
    c2 = str.charCodeAt(i++); if (c1>191 && c1<224){ arr[ac++] = String.fromCharCode(((c1&31)<<6)|(c2&63)); continue; }
    c3 = str.charCodeAt(i++); arr[ac++] = String.fromCharCode(((c1&15)<<12)|((c2&63)<<6)|(c3&63));  
  }
  return arr.join('');
}  
function base64_decode(input) {
  var keystr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  var output = "";
  var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  var i = 0;
  if( /[^A-Za-z0-9\+\/\=]/g.exec(input) ) return null;
  while(i < input.length) {
    enc1 = keystr.indexOf(input.charAt(i++));
    enc2 = keystr.indexOf(input.charAt(i++));
    enc3 = keystr.indexOf(input.charAt(i++));
    enc4 = keystr.indexOf(input.charAt(i++));
    chr1 = (enc1 << 2) | (enc2 >> 4);
    chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
    chr3 = ((enc3 & 3) << 6) | enc4;
    output = output + String.fromCharCode(chr1);
    if(enc3 != 64) output += String.fromCharCode(chr2);
    if(enc4 != 64) output += String.fromCharCode(chr3);
  }
  return utf8_decode(output);
}
console.log(base64_decode('안녕')); // (null)
console.log(base64_decode('aGVsbG8gd29ybGQ=')); // hello world
console.log(base64_decode('4piFQeqwgOOBguS4rQ==')); // ★A가あ中
CryptoJS 활용
console.log( CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse('4piFQeqwgOOBguS4rQ==')) );
// ★A가あ中

5 Objective-C[ | ]

#define CHAR(x) [NSString stringWithFormat:@"%C",x]
#define CHAR_AT(str, n) [str substringWithRange:NSMakeRange(n,1)]
#define STRPOS(str, x) [Util strpos:str sub:x]
+(int) strpos:(NSString*)str sub:(NSString*)sub {
    int pos = [str rangeOfString:sub].location;
    return (pos == NSNotFound)? -1 : pos;
}
+(NSString*) replace_pattern:(NSString*)pattern with:(NSString*)template haystack:(NSString*)haystack {
    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
    return [regex stringByReplacingMatchesInString:haystack options:0 range:NSMakeRange(0, haystack.length) withTemplate:template];
}
+(NSString*) base64_decode:(NSString*)str {
    NSString* key = @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    NSString* output = @"";
    int e1, e2, e3, e4, c1, c2, c3;
    int i = 0;
    str = [Util replace_pattern:@"/[^A-Za-z0-9+/=]/g" with:@"" haystack:str];
    while (i < [str length]) {
        e1 = STRPOS(key, CHAR_AT(str, i++));
        e2 = STRPOS(key, CHAR_AT(str, i++));
        e3 = STRPOS(key, CHAR_AT(str, i++));
        e4 = STRPOS(key, CHAR_AT(str, i++));
        c1 = (e1 << 2) | (e2 >> 4);
        c2 = ((e2 & 15) << 4) | (e3 >> 2);
        c3 = ((e3 & 3) << 6) | e4;
        output = [output stringByAppendingString:CHAR(c1)];
        if (e3 != 64) output = [output stringByAppendingString:CHAR(c2)];
        if (e4 != 64) output = [output stringByAppendingString:CHAR(c3)];
    }
    return [Util utf8_decode:output];
}
NSLog(@"%@", [Util base64_decode:@"4piFQeqwgOOBguS4rQ=="]); // ★A가あ中

6 PHP[ | ]

$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo base64_decode($str);
// This is an encoded string
echo base64_decode('4piFQeqwgOOBguS4rQ==');
// ★A가あ中

7 Perl[ | ]

use MIME::Base64 qw( decode_base64 );
my $str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
print decode_base64($str);
# This is an encoded string

8 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}