함수 base64_decode()

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 }}