투케이2K

78. (Objective-C/objc) CommonCrypto 사용해 AES 256 데이터 복호화 디코딩 (decode) 수행 실시 본문

Objective-C

78. (Objective-C/objc) CommonCrypto 사용해 AES 256 데이터 복호화 디코딩 (decode) 수행 실시

투케이2K 2022. 9. 21. 16:18

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : OBJECTIVE-C

 

[소스 코드]

- (NSString *) aes256_Decode:(NSString *)secretKey :(NSString *)iv :(NSString *)inputString {
    printf("\n");
    printf("==================================== \n");
    printf("[ViewController >> aes256_Decode :: AES 256 복호화 디코딩 수행 실시] \n");
    printf("[secretKey :: %s] \n", secretKey.description.UTF8String);
    printf("[iv :: %s] \n", iv.description.UTF8String);
    printf("[inputString :: %s] \n", inputString.description.UTF8String);
    printf("==================================== \n");
    printf("\n");
    
    
    /*
     ------------------------------------
     [요약 설명]
     ------------------------------------
     1. AES256 : 비밀키를 사용해 데이터 인코딩 및 디코딩을 수행합니다
     ------------------------------------
     2. AES256 에 사용되는 비밀 키 값은 32 바이트 값 입니다
     ------------------------------------
     3. 필요 import :
     
     #import <CommonCrypto/CommonDigest.h>
     #import <CommonCrypto/CommonCryptor.h>
     ------------------------------------
     4. 호출 방법 :
     
     [self aes256_Decode:
      @"0123456789abcdef0123456789abcdef" : // [secretKey]
      @"0123456789abcdef" : // [iv]
      @"UQdw44JDqzsxYpkSCwXDIA==" // [data]
     ];
     ------------------------------------
     */
    
    
    // [리턴 변수 선언 실시]
    NSData *returnData = nil;
    NSString *returnString = @"";
    
    
    // [로직 처리 실시]
    @try {
        
        // [비밀 키 사이즈 및 데이터 널 체크 수행 실시]
        if (secretKey != nil && secretKey.length == 32 && iv != nil && iv.length == 16 && inputString != nil && inputString.length>0 && ![inputString  isEqual: @""]){
            
            // [AES256 32 bytes key]
            char keyPtr[kCCKeySizeAES256 + 1];
            bzero(keyPtr, sizeof(keyPtr));
            
            // [fetch key]
            [secretKey getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
            
            // [padding]
            CCOptions padding = kCCOptionPKCS7Padding;
            
            // [inputString to Data]
            NSData *inputData = [[NSData alloc] initWithBase64EncodedString:inputString options:0];
            
            // [data length]
            NSUInteger dataLength = [inputData length];
            
            // [block size]
            size_t bufferSize = dataLength + kCCBlockSizeAES128;
            void *buffer = malloc(bufferSize);
            
            // [decode]
            size_t bytesDecrypted = 0;
            
            CCCryptorStatus cryptStatus = CCCrypt(
                                                  kCCDecrypt,
                                                  
                                                  kCCAlgorithmAES128,
                                                  
                                                  padding,
                                                  
                                                  keyPtr,
                                                  
                                                  kCCKeySizeAES256,
                                                  
                                                  [iv UTF8String],
                                                  
                                                  [inputData bytes],
                                                  
                                                  dataLength,
                                                  
                                                  buffer,
                                                  
                                                  bufferSize,
                                                  
                                                  &bytesDecrypted);
            if (cryptStatus == kCCSuccess) {
                
                returnData = [NSData dataWithBytesNoCopy:buffer length:bytesDecrypted];
            }
            //free(buffer);
            
            
            // [base 64 값으로 변환 실시]
            if (returnData != nil && returnData.length>0){
                returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
            }
        }
        else {
            printf("\n");
            printf("==================================== \n");
            printf("[ViewController >> aes256_Decode :: try :: input data is null] \n");
            printf("==================================== \n");
            printf("\n");
        }
        
    }
    @catch (NSException *exception) {
        printf("\n");
        printf("==================================== \n");
        printf("[ViewController >> aes256_Decode :: catch :: 예외 상황 확인] \n");
        printf("[name :: %s] \n", exception.name.description.UTF8String);
        printf("[reason :: %s] \n", exception.reason.description.UTF8String);
        printf("==================================== \n");
        printf("\n");
    }
    
    
    // [로그 출력 실시]
    printf("\n");
    printf("==================================== \n");
    printf("[ViewController >> aes256_Decode :: AES 256 복호화 디코딩 수행 완료] \n");
    printf("[returnString :: %s] \n", returnString.description.UTF8String);
    printf("==================================== \n");
    printf("\n");
    
    
    // [리턴 반환 실시]
    return returnString;
}
 

[결과 출력]

 

 

반응형
Comments