본문 바로가기
#포트폴리오 [Portfolio]

[🔒 Security] Base64

by cy_mos 2020. 7. 27.
반응형

 

알고 나면 아무것도 아닌 Base64의 정체 - 취미로 해킹

 

[네이버 지식백과] 베이스 64 [base 64] (IT용어사전, 한국정보통신기술협회)

2진 데이터를 아스키 텍스트로 변환하거나 그 반대로 변환하는 인코딩 방법입니다. MIME에 의해 사용되는 방법으로, 4개의 7비트 아스키 문자로 표현되도록 데이터를 3바이트씩 4개의 6비트 단위로 나누어 표현합니다. 메일에서 이미지, 오디오 파일을 보낼 때 이용하는 코딩으로 모든 플랫폼에서 안보이거나 깨지는 일이 생기지 않도록 공통으로 64개 아스키 코드를 이용하여 2진 데이터를 변환하기 위해 베이스 64를 이용하며 베이스 64로 인코딩하면 크기가 33% 커집니다.

 

✏️ Encoding and Decoding Base64 Table

 

Base64를 이용하여 아래의 문구를 Encode 작업을 수행하게 되면 아래와 같은 결과물을 얻을 수 있습니다.

 

Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.

 

아래의 결과물은 위의 문구를 Base64 Encode 작업을 수행하여 얻은 결과물입니다.=

 

TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0
aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1
c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0
aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdl
LCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=

🗂 Base64 Encode/Decode by Swift

/*
 * Copyright (c) 2024 Universal-SystemKit. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import Foundation

// MARK: - Public String Extension
public extension String {
    
    var base64EncodedString: String? { 
        
        return self.data(using: .utf8)?.base64EncodedString()
    }
    
    var base64EncodedData: Data? {
        
        return self.data(using: .utf8)?.base64EncodedData()
    }
    
    var base64DecodedString: String? {
        
        guard let rawData = Data(base64Encoded: self) else { return nil }
        
        return String(data: rawData, encoding: .utf8)
    }
    
    var base64DecodedData: Data? {
        
        return Data(base64Encoded: self)
    }
}

// MARK: - Public Extension Data
public extension Data {
    
    var base64DecodedString: String? {
        
        return String(data: self, encoding: .utf8)?.base64DecodedString
    }
    
    var base64DecodedData: Data? {
        
        return String(data: self, encoding: .utf8)?.base64DecodedData
    }
}

🚀 REFERENCE

반응형

댓글