이 글은 기계 번역의 미러 문서이며, 원본 기사로 바로 이동하려면 여기를 클릭해 주세요.

보기: 10067|회답: 0

URL secure Base64 인코딩, 디코딩

[링크 복사]
게시됨 2019. 11. 15. 오후 2:10:48 | | |
Base64는 이진 문자를 HTTP 전송용 가시 문자로 트랜스코딩할 수 있지만, Transcoding 시 URL에 의해 "+", "/", "=" 등 특수 문자가 생성되어 두 측면 간에 불일치가 발생합니다.
"+", "/", "="를 전송 전에 트랜스코딩하지 않는 문자로 대체하고, 데이터를 받은 후 다시 해당 문자를 교체한 후 디코딩할 수 있습니다.


PHP:

    /**

     * URL base64 디코딩

     * '-' -> '+'

     * '_' -> '/'

     * 나머지 문자열 길이 %4, 보완 '='

     * @param 알려지지 않은 $string

     */

    공용 정적 함수 urlsafe_b64decode($string) {

        $data = str_replace(array('-','_'), array('+','/'),$string);

        $mod 4 = strlen($data) % 4;

        만약 ($mod 4) {

            $data .= substr('====', $mod 4);

        }

        base64_decode($data);

    }

   

    /**

     * URL base64 인코딩

     * '+' -> '-'

     * '/' -> '_'

     * '=' -> ''

     * @param 알려지지 않은 $string

     */

    함수 urlsafe_b64encode($string) {

        $data = base64_encode($string);

        $data = str_replace(array('+','/','='),array('-','_',''),$data);

        $data 반환;

    }




OC:



#pragma - saveBase64 인코딩에서 문자열 "-", "_"를 "+", "/", 그리고 문자열 길이의 4배로 비트 보완 "="로 변환합니다.

+(NSData*)safeUrlBase64Decode:(NSString*)safeUrlbase64Str

{

    // '-' -> '+'

    // '_' -> '/'

    길이가 4배 미만이라면 '='를 보완합니다.

    NSMutableString * base64Str = [[NSMutableString alloc]initWithString:safeUrlbase64Str];

    base64Str = (NSMutableString * )[base64Str stringByReplacingOccurrencesOfString:@"-" withString:@"+"];

    base64Str = (NSMutableString * )[base64Str stringByReplacingOccurrencesOfString:@"_" withString:@"/"];

    NSInteger mod4 = base64Str.length % 4;

    if(mod4 > 0)

        [base64Str appendString:[@"====" substringToIndex:(4-mod4)]];

    NSLog(@"Base64 원본: %@", base64Str);

    return [GTMBase64 decodeData:[base64Str dataUsingEncoding:NSUTF8StringEncoding]];

   

}



#pragma - Base64 인코딩에는 +, /, = 이러한 보안이 취약한 URL 문자열이 포함되어 있으니 문자를 변경하세요

+(NSString*)safeUrlBase64Encode:(NSData*)data

{

    // '+' -> '-'

    // '/' -> '_'

    // '=' -> ''

    NSString * base64Str = [GTMBase64 stringByEncodingData:data];

    NSMutableString * safeBase64Str = [[NSMutableString alloc]initWithString:base64Str];

    safeBase64Str = (NSMutableString * )[safeBase64Str stringByReplacingOccurrencesOfString:@"+" withString:@"-"];

    safeBase64Str = (NSMutableString * )[safeBase64Str stringByReplacingOccurrencesOfString:@"/" withString:@"_"];

    safeBase64Str = (NSMutableString * )[safeBase64Str stringByReplacingOccurrencesOfString:@"=" withString:@""];

    NSLog(@"safeBase64encoded:%@", safeBase64Str);

    안전기지64Str로 반환;

}



자바:

public static 문자열 safeUrlBase64Encode(byte[] data){

String encodeBase64 = new BASE64Encoder().encode(data);

string safeBase64Str = encodeBase64.replace('+', '-');

safeBase64Str = safeBase64Str.replace('/', '_');

safeBase64Str = safeBase64Str.replaceAll("=", "");

안전기지64Str로 반환;

}


public static byte[] safeUrlBase64Decode(마지막 문자열 safeBase64Str){

base64Str 문자열 = safeBase64Str.replace('-', '+');

base64Str = base64Str.replace('_', '/');

int mod4 = base64Str.length()%4;

IF(mod4 > 0){

base64Str = base64Str + "====".substring(mod4);

}

새로운 BASE64Decoder().decodeBuffer(base64Str);

}



안드로이드의 경우, android.utils.Base64에는 다음과 같은 기능이 포함되어 있습니다:

Base64.encodeToString(encrypted, Base64.URL_SAFE| Base64.NO_WRAP);

Base64.decode(data, Base64.URL_SAFE);




이전의:계정 잠금 보안 전략을 활용해 보세요
다음:C#은 Windows와 SQLserver 인증을 위해 두 가지 유형의 연결 문자열에 sqlserver를 연결합니다
면책 조항:
Code Farmer Network에서 발행하는 모든 소프트웨어, 프로그래밍 자료 또는 기사는 학습 및 연구 목적으로만 사용됩니다; 위 내용은 상업적 또는 불법적인 목적으로 사용되지 않으며, 그렇지 않으면 모든 책임이 사용자에게 부담됩니다. 이 사이트의 정보는 인터넷에서 가져온 것이며, 저작권 분쟁은 이 사이트와는 관련이 없습니다. 위 내용은 다운로드 후 24시간 이내에 컴퓨터에서 완전히 삭제해야 합니다. 프로그램이 마음에 드신다면, 진짜 소프트웨어를 지원하고, 등록을 구매하며, 더 나은 진짜 서비스를 받아주세요. 침해가 있을 경우 이메일로 연락해 주시기 바랍니다.

Mail To:help@itsvse.com