๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํŒŒ์ด์ฌ Base64 ์ธ์ฝ”๋”ฉ/๋””์ฝ”๋”ฉ ์ƒ˜ํ”Œ์ฝ”๋“œ

๐ŸŽฎinspirer9 2023. 7. 12. 09:47
728x90
๋ฐ˜์‘ํ˜•

ํŒŒ์ด์ฌ์—์„œ๋Š” `base64` ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ Base64 ์ธ์ฝ”๋”ฉ ๋ฐ ๋””์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์— ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ ์ƒ˜ํ”Œ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค:

์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ํŒŒ์ด์ฌ์˜ `base64` ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด๊ณผ ๋ฐ”์ดํŠธ ๋ฐฐ์—ด์˜ Base64 ์ธ์ฝ”๋”ฉ ๋ฐ ๋””์ฝ”๋”ฉ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด ์˜ˆ์‹œ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. `base64_encode_string`, `base64_decode_string`, `base64_encode_bytes`, `base64_decode_bytes` ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ธ์ฝ”๋”ฉํ•˜๊ฑฐ๋‚˜ ๋””์ฝ”๋”ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

python
import base64

# ๋ฌธ์ž์—ด์„ Base64๋กœ ์ธ์ฝ”๋”ฉํ•˜๋Š” ํ•จ์ˆ˜
def base64_encode_string(input_string):
    encoded_bytes = base64.b64encode(input_string.encode('utf-8'))
    encoded_string = encoded_bytes.decode('utf-8')
    return encoded_string

# Base64๋กœ ์ธ์ฝ”๋”ฉ๋œ ๋ฌธ์ž์—ด์„ ๋””์ฝ”๋”ฉํ•˜๋Š” ํ•จ์ˆ˜
def base64_decode_string(encoded_string):
    decoded_bytes = base64.b64decode(encoded_string.encode('utf-8'))
    decoded_string = decoded_bytes.decode('utf-8')
    return decoded_string

# ๋ฐ”์ดํŠธ ๋ฐฐ์—ด์„ Base64๋กœ ์ธ์ฝ”๋”ฉํ•˜๋Š” ํ•จ์ˆ˜
def base64_encode_bytes(input_bytes):
    encoded_bytes = base64.b64encode(input_bytes)
    return encoded_bytes

# Base64๋กœ ์ธ์ฝ”๋”ฉ๋œ ๋ฐ”์ดํŠธ ๋ฐฐ์—ด์„ ๋””์ฝ”๋”ฉํ•˜๋Š” ํ•จ์ˆ˜
def base64_decode_bytes(encoded_bytes):
    decoded_bytes = base64.b64decode(encoded_bytes)
    return decoded_bytes

# ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ์˜ˆ์‹œ
input_string = "Hello, World!"
encoded_string = base64_encode_string(input_string)
decoded_string = base64_decode_string(encoded_string)
print("Encoded String:", encoded_string)
print("Decoded String:", decoded_string)

# ๋ฐ”์ดํŠธ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์˜ˆ์‹œ
input_bytes = b'\x48\x65\x6c\x6c\x6f\x2c\x20\x57\x6f\x72\x6c\x64\x21'
encoded_bytes = base64_encode_bytes(input_bytes)
decoded_bytes = base64_decode_bytes(encoded_bytes)
print("Encoded Bytes:", encoded_bytes)
print("Decoded Bytes:", decoded_bytes)
728x90
๋ฐ˜์‘ํ˜•