์ƒˆ์†Œ์‹

Languages/Python

[์•”ํ˜ธ ํ”„๋กœํ† ์ฝœ] Base64 ์ธ์ฝ”๋”ฉ ๊ตฌํ˜„ํ•˜๊ธฐ

  • -
๋ฐ˜์‘ํ˜•

https://blog.minamiland.com/465


์ธ์ฝ”๋”ฉ์ด๋ž€?

 Base64 ์ธ์ฝ”๋”ฉ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ธฐ์ „์— ๋จผ์ € ์ธ์ฝ”๋”ฉ์ด๋ž€๊ฒŒ ๋ฌด์—‡์ธ์ง€ ๊ฐ„๋žตํ•˜๊ฒŒ ์•Œ์•„๋ณด์ž. 

์ธ์ฝ”๋”ฉ(encoding)์€ ์ •๋ณด์˜ ํ˜•ํƒœ๋‚˜ ํ˜•์‹์„ ํ‘œ์ค€ํ™”, ๋ณด์•ˆ, ์ฒ˜๋ฆฌ ์†๋„ ํ–ฅ์ƒ, ์ €์žฅ ๊ณต๊ฐ„ ์ ˆ์•ฝ ๋“ฑ์„ ์œ„ํ•ด์„œ ๋‹ค๋ฅธ ํ˜•ํƒœ๋‚˜ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ฒ˜๋ฆฌ ํ˜น์€ ๊ทธ ์ฒ˜๋ฆฌ ๋ฐฉ์‹์„ ๋งํ•œ๋‹ค. ๋™์˜์ƒ์ด๋‚˜ ์ด๋ฏธ์ง€์˜์—ญ์—์„œ๋„ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ์šฉ์–ด์ง€๋งŒ ์šฐ๋ฆฌ๋Š” Binary Data๋ฅผ Text๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” Base64 ์ธ์ฝ”๋”ฉ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณผ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‚˜๋จธ์ง€๋Š” ์ƒ๋žตํ•˜๋„๋ก ํ•˜๊ฒ ๋‹ค.

 

๋‹ค์–‘ํ•œ ํŠน์ง•๋“ค์„ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ? ์•„๋ž˜ ๋ธ”๋กœ๊ทธ ์ฐธ๊ณ ํ•˜๊ธฐ.

lucete1230-cyberpolice.tistory.com/95 

 

์ธ์ฝ”๋”ฉ(ascii, html, url, base 64 ..)

IT์ธ์ด๋ผ๋ฉด ์–ด๋””์—์„œ๋“  ํ•œ๋ฒˆ์ฏค์€ ๋“ค์–ด๋ดค์„ ๋ฒ•ํ•œ "์ธ์ฝ”๋”ฉ!" ๊ฐ์ข… ์‚ฌ์ดํŠธ์—์„œ Web๋ฌธ์ œ๋ฅผ ํ’€๋•Œ๋„ ์ž์ฃผ ๋“ฑ์žฅํ•˜๋Š”๋ฐ์š”, ์˜ค๋Š˜์€ ์ œ๊ฐ€ ์ž์ฃผ ์“ฐ๋Š” ์ธ์ฝ”๋”ฉ ์ข…๋ฅ˜์™€, ์ธ์ฝ”๋”ฉ์ด ๋ฌด์—‡์ธ์ง€์— ๋Œ€ํ•ด ๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค

lucete1230-cyberpolice.tistory.com

 

 

 

Base 64 ์ธ์ฝ”๋”ฉ์ด๋ž€?
์ปดํ“จํ„ฐ ๋ถ„์•ผ์—์„œ ์“ฐ์ด๋Š” Base 64 (๋ฒ ์ด์Šค ์œก์‹ญ์‚ฌ)๋ž€ 8๋น„ํŠธ ์ด์ง„ ๋ฐ์ดํ„ฐ(์˜ˆ๋ฅผ ๋“ค์–ด ์‹คํ–‰ ํŒŒ์ผ์ด๋‚˜, ZIP ํŒŒ์ผ ๋“ฑ)๋ฅผ ๋ฌธ์ž ์ฝ”๋“œ์— ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋Š” ๊ณตํ†ต ASCII ์˜์—ญ์˜ ๋ฌธ์ž๋“ค๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ์ผ๋ จ์˜ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ๋Š” ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐœ๋…์ด๋‹ค.

์›๋ž˜ Base 64๋ฅผ ๊ธ€์ž ๊ทธ๋Œ€๋กœ ๋ฒˆ์—ญํ•˜์—ฌ ๋ณด๋ฉด 64์ง„๋ฒ•์ด๋ž€ ๋œป์ด๋‹ค. ํŠน๋ณ„ํžˆ 64์ง„๋ฒ•์ด ์ปดํ“จํ„ฐ์—์„œ ํฅ๋ฏธ๋กœ์šด ๊ฒƒ์€, 64๊ฐ€ 2์˜ ์ œ๊ณฑ์ˆ˜(64 = 26)์ด๋ฉฐ, 2์˜ ์ œ๊ณฑ์ˆ˜๋“ค์— ๊ธฐ๋ฐ˜ํ•œ ์ง„๋ฒ•๋“ค ์ค‘์—์„œ ํ™”๋ฉด์— ํ‘œ์‹œ๋˜๋Š” ASCII ๋ฌธ์ž๋“ค์„ ์จ์„œ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ง„๋ฒ•์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ฆ‰, ๋‹ค์Œ ์ œ๊ณฑ์ˆ˜์ธ 128์ง„๋ฒ•์—๋Š” 128๊ฐœ์˜ ๊ธฐํ˜ธ๊ฐ€ ํ•„์š”ํ•œ๋ฐ ํ™”๋ฉด์— ํ‘œ์‹œ๋˜๋Š” ASCII ๋ฌธ์ž๋“ค์€ 128๊ฐœ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค.

๊ทธ๋Ÿฐ ๊นŒ๋‹ญ์— ์ด ์ธ์ฝ”๋”ฉ์€ ์ „์ž ๋ฉ”์ผ์„ ํ†ตํ•œ ์ด์ง„ ๋ฐ์ดํ„ฐ ์ „์†ก ๋“ฑ์— ๋งŽ์ด ์“ฐ์ด๊ณ  ์žˆ๋‹ค. Base 64์—๋Š” ์–ด๋–ค ๋ฌธ์ž์™€ ๊ธฐํ˜ธ๋ฅผ ์“ฐ๋Š๋ƒ์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ ๋ณ€์ข…์ด ์žˆ์ง€๋งŒ, ์ž˜ ์•Œ๋ ค์ง„ ๊ฒƒ์€ ๋ชจ๋‘ ์ฒ˜์Œ 62๊ฐœ๋Š” ์•ŒํŒŒ๋ฒณ A-Z, a-z์™€ 0-9๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์œผ๋ฉฐ ๋งˆ์ง€๋ง‰ ๋‘ ๊ฐœ๋ฅผ ์–ด๋–ค ๊ธฐํ˜ธ๋ฅผ ์“ฐ๋Š๋ƒ์˜ ์ฐจ์ด๋งŒ ์žˆ๋‹ค.

 

 

 

์œ„ํ‚ค ๋ฐฑ๊ณผ์—์„œ base64๋ฅผ ๊ฒ€์ƒ‰ํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์˜ˆ์‹œ๊ฐ€ ๋‚˜์˜จ๋‹ค. ์ด๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ๋กœ ์‹ค์Šตํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.

 

 

100% ์™„๋ฒฝํ•˜์ง„ ์•Š์ง€๋งŒ, ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ๋‘์—ˆ๋‹ค.

 

# -*- coding: utf-8 -*-
"""
Created on Tue Mar 23 00:01:31 2021

@author: ์ดํ•˜์€
"""
import re
word = {'000000': 'A', '000001': 'B', '000010': 'C', '000011': 'D', 
       '000100': 'E', '000101': 'F', '000110': 'G', '000111': 'H', 
       '001000': 'I', '001001': 'J', '001010': 'K', '001011': 'L',
       '001100': 'M', '001101': 'N', '001110': 'O', '001111': 'P', 
       '010000': 'Q', '010001': 'R', '010010': 'S', '010011': 'T', 
       '010100': 'U', '010101': 'V', '010110': 'W', '010111': 'X', 
       '011000': 'Y', '011001': 'Z', '011010': 'a', '011011': 'b', 
       '011100': 'c', '011101': 'd', '011110': 'e', '011111': 'f',
       '100000': 'g', '100001': 'h', '100010': 'I', '100011': 'j',
       '100100': 'k', '100101': 'l', '100110': 'm', '100111': 'n',
       '101000': 'o', '101001': 'p', '101010': 'q', '101011': 'r',
       '101100': 's', '101101': 't', '101110': 'u', '101111': 'v',
       '110000': 'w', '110001': 'x', '110010': 'y', '110011': 'z',
       '110100': '0', '110101': '1', '110110': '2', '110111': '3',
       '111000': '4', '111001': '5', '111010': '6', '111011': '7',
       '111100': '8', '111101': '9', '111110': '+', '111111': '/'}

aaa = []
def base64_enc(msg):
    string = ''
    zz=[]
    result_enc =''    
    #๋ฌธ์ž๋ฅผ 8bit๋กœ ํ‘œํ˜„ํ•˜๊ธฐ.
    for i in range(len(msg)):
        a = bin(msg[i])
        #print(a)
        aaa.append(int(a,2))
        if(len(a)!=9):
            a = '0'+a
            string += a
        else:
            string+=a

    string = re.sub("b","",string)
    
    #๊ธธ์ด ๋‚˜๋ˆ„๋Š” ๋ถ€๋ถ„ > base64 encoding ์€ 6byte๋กœ ๋‚˜๋ˆ ์คŒ.
    length=6 
    for i in range(0, len(string),length):
        zz.append(string[i:i+length])
    
    
    
    #padding์ฒ˜๋ฆฌ
    for i in range(len(zz)):
        z = len(zz[i])
        # = ์€ ํ•œ๊ฐœ ํ˜น์€ 2๊ฐœ ๋ฐ–์— ๋‚˜์˜ฌ ์ˆ˜ ์—†์Œ.
        if(z == 4):
            zd = zz[i];zd +='00';zz[i] =zd;zz.append("=")
        if(z == 2):
            zd = zz[i];zd +='0000';zz[i] =zd;zz.append("==")
    
    # base64์ธ์ฝ”๋”ฉ ํ‘œ๋ฅผ ๊ฐ€์ ธ์™€์„œ, key์ถœ๋ ฅํ•˜๊ธฐ.
    for i in zz:  
        for key, value in word.items(): #mydict์— ์•„์ดํ…œ์„ ํ•˜๋‚˜์”ฉ ์ ‘๊ทผํ•ด์„œ, key, value๋ฅผ ๊ฐ๊ฐ name, age์— ์ €์žฅ
            if i == str(key):
                result_enc += word[key]
        if i == "=":
            result_enc += i
        elif i == "==":
            result_enc += i
            
    return result_enc   


def base64_dec():
    ans = ''
    for i in aaa:
        ans += chr(i)
    return ans
    

def main():
    msg = input("๋ณ€๊ฒฝํ•  ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜์‹œ์˜ค : ")
    print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
    enc = base64_enc(msg.encode()) #๊ฐ’์€ ์ธ์ฝ”๋”ฉํ•ด์„œ ๋ณด๋‚ด๊ธฐ~
    dec = base64_dec()
    print("base 64 Encoding : ",enc)
    print("---------------------------------------------------------")
    print("base 64 Deciding : ",dec)
    print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++")

main()



 

 

์ฐธ๊ณ ๋กœ base 64 ํ‘œ๋Š” ๊ฐ€์ ธ์˜ฌ ์ˆ˜๊ฐ€ ์—†์–ด์„œ ์ง์ ‘ ์ง€์ •ํ•ด์ฃผ์—ˆ๋‹ค.

 

์•„๋ž˜๋Š” ์•„์ง ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ๋๋งˆ์น˜์ง€ ์•Š์€ ์ฝ”๋“œ์ด์ง€๋งŒ, ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์˜ ์ฝ”๋“œ์ด๋‹ค. (decoding๋ถ€๋ถ„)

# -*- coding: utf-8 -*-
"""
Created on Mon Mar 22 23:56:37 2021

@author: ์ดํ•˜์€
"""


import re
        
word = {'000000': 'A', '000001': 'B', '000010': 'C', '000011': 'D', 
       '000100': 'E', '000101': 'F', '000110': 'G', '000111': 'H', 
       '001000': 'I', '001001': 'J', '001010': 'K', '001011': 'L',
       '001100': 'M', '001101': 'N', '001110': 'O', '001111': 'P', 
       '010000': 'Q', '010001': 'R', '010010': 'S', '010011': 'T', 
       '010100': 'U', '010101': 'V', '010110': 'W', '010111': 'X', 
       '011000': 'Y', '011001': 'Z', '011010': 'a', '011011': 'b', 
       '011100': 'c', '011101': 'd', '011110': 'e', '011111': 'f',
       '100000': 'g', '100001': 'h', '100010': 'I', '100011': 'j',
       '100100': 'k', '100101': 'l', '100110': 'm', '100111': 'n',
       '101000': 'o', '101001': 'p', '101010': 'q', '101011': 'r',
       '101100': 's', '101101': 't', '101110': 'u', '101111': 'v',
       '110000': 'w', '110001': 'x', '110010': 'y', '110011': 'z',
       '110100': '0', '110101': '1', '110110': '2', '110111': '3',
       '111000': '4', '111001': '5', '111010': '6', '111011': '7',
       '111100': '8', '111101': '9', '111110': '+', '111111': '/'}



def base64_enc(msg):
    string = ''
    zz=[]
    result_enc =''
    #๋ฌธ์ž๋ฅผ 8bit๋กœ ํ‘œํ˜„ํ•˜๊ธฐ.
    for i in range(len(msg)):
        a = bin(msg[i])
        #print(a)
        if(len(a)!=9):
            a = '0'+a   
            string += a
        else:
            string+=a
    string = re.sub("b","",string)
    
    #๊ธธ์ด ๋‚˜๋ˆ„๋Š” ๋ถ€๋ถ„ > base64 encoding ์€ 6byte๋กœ ๋‚˜๋ˆ ์คŒ.
    length=6 
    for i in range(0, len(string),length):
        zz.append(string[i:i+length])
                       
    #padding์ฒ˜๋ฆฌ
    for i in range(len(zz)):
        z = len(zz[i])
        # = ์€ ํ•œ๊ฐœ ํ˜น์€ 2๊ฐœ ๋ฐ–์— ๋‚˜์˜ฌ ์ˆ˜ ์—†์Œ.
        if(z == 4):
            zd = zz[i];zd +='00';zz[i] =zd;zz.append("=")
        if(z == 2):
            zd = zz[i];zd +='0000';zz[i] =zd;zz.append("==")
    
    # base64์ธ์ฝ”๋”ฉ ํ‘œ๋ฅผ ๊ฐ€์ ธ์™€์„œ, key์ถœ๋ ฅํ•˜๊ธฐ.
    for i in zz:  
        for key, value in word.items(): #mydict์— ์•„์ดํ…œ์„ ํ•˜๋‚˜์”ฉ ์ ‘๊ทผํ•ด์„œ, key, value๋ฅผ ๊ฐ๊ฐ name, age์— ์ €์žฅ
            if i == str(key):
                result_enc += word[key]
        if i == "=":
            result_enc += i
        elif i == "==":
            result_enc += i

    return result_enc


def base64_dec(enc):
    dic =''
    dic2 = {v:k for k,v in word.items()} #๊ฐ’ ๋’ค์ง‘๊ธฐ.
    for i in enc:
        if(i == '='):
            dic += i
            dic = dic.replace("00=", "")
        elif(i == "=="):
            dic += i
            dic = dic.replace("0000==", "")
        else :
            dic += dic2.get(i)

    box=[]
    length = 8 #๊ธธ์ด๋ฅผ ๋‹ค์‹œ 8๊ธ€์ž๋กœ ๋Š˜๋ ค์„œ ascii์ฝ”๋“œ๋กœ ๋ฐ”๊ฟ€ ์ค€๋น„.
    for i in range(0, len(dic),length):
        box.append("b"+dic[i:i+length])

    aes=[]
    result=''
    for i in range(len(box)):
        aes.append(box[i].replace("b0","0b"))
        result += chr(int(aes[i],2))

    return result
    
    
def main():
    msg = input("๋ณ€๊ฒฝํ•  ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜์‹œ์˜ค : ")
    enc = base64_enc(msg.encode()) #๊ฐ’์€ ์ธ์ฝ”๋”ฉํ•ด์„œ ๋ณด๋‚ด๊ธฐ~
    dec = base64_dec(enc)
    print("==================================================")
    print("base 64 Encoding : ",enc)
    print("base 64 Deciding : ",dec)
    print("==================================================")
main()


 

 

์ถœ๋ ฅ๊ฒฐ๊ณผ๋ฅผ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

๋ฌธ์ œ๊ฐ€ ์—†๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜ ์ธ์ฝ”๋”ฉ/๋””์ฝ”๋”ฉ ์‚ฌ์ดํŠธ๋ฅผ ์ด์šฉํ•ด์„œ ์ฒดํฌํ•ด๋ณด๋„๋ก ํ•˜์ž!

www.convertstring.com/ko/EncodeDecode/Base64Encode

 

Base64๋กœ ์ธ์ฝ”๋”ฉ - ์˜จ๋ผ์ธ Base64๋กœ ์ธ์ฝ”๋”

 

www.convertstring.com

 

๊ฐ’์ด ๋™์ผํ•˜๊ฒŒ ์ธ์ฝ”๋”ฉ ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์›๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฌธ์ž ํ•œ๊ฐœ (8bit)๋ฅผ ์ด์ง„์ˆ˜๋กœ ํ‘œํ˜„ํ•˜์—ฌ, ์ด๋ฅผ 6bit์”ฉ ์ชผ๊ฐœ๊ณ  ํ•ด๋‹นํ•˜๋Š” base64ํ‘œ์˜ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋ฉด

base64 ์ธ์ฝ”๋”ฉ์ด ์™„๋ฃŒ๋œ๋‹ค. ๋งˆ์ง€๋ง‰ ๋น„ํŠธ๊ฐ€ ๋นˆ๋‹ค๋ฉด ํŒจ๋”ฉ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๋Š”๋ฐ, ์ด๋•Œ ํŒจ๋”ฉ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์—ˆ๋˜ 0์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ

'=' ์„ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

์œ„์—์„œ ์œ„ํ‚ค๋ฐฑ๊ณผ์—์„œ ๋‚˜์™”๋˜ ์˜ˆ์‹œ๋„ ํ•œ๋ฒˆ ๋„ฃ์–ด๋ณด์•˜๋‹ค

 

๊ฒฐ๊ณผ๊ฐ€ ๋ฌธ์ œ์—†์ด ๋™์ผํ•˜๊ฒŒ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.