ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Objectivc-C] SHA-256 / AES-256 ์•”ํ˜ธํ™”
    ๐ŸŽ iOS/๊ฐœ๋ฐœ 2023. 2. 6. 16:14

    ๋ฏผ๊ฐํ•œ ๊ฐœ์ธ์ •๋ณด๋ฅผ API์— ํƒœ์›Œ ๋ณด๋‚ด์•ผ ํ•  ๋•Œ ์ฃผ๋กœ ํด๋ผ์ด์–ธํŠธ๋‹จ์—์„œ ์•”ํ˜ธํ™” ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์„œ ๋ณด๋‚ด๊ณค ํ•ฉ๋‹ˆ๋‹ค. ๋ณธ ํฌ์ŠคํŒ…์—์„œ๋Š” Objective-C๋กœ SHA256๊ณผ AES256 ์•”ํ˜ธํ™”๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค.

     

     


     

    ๐Ÿ“Œ SHA-256

    SHA๋Š” ๋‹จ๋ฐฉํ–ฅ ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ฏ€๋กœ ๋ณตํ˜ธํ™”๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋ณตํ˜ธํ™”๋ฅผ ํ•˜์ง€ ์•Š์•„๋„ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋น ๋ฅธ ์†๋„๊ฐ€ ํŠน์ง•์ž…๋‹ˆ๋‹ค. ๋น„๋ฐ€๋ฒˆํ˜ธ ์ผ์น˜์—ฌ๋ถ€ ํ™•์ธ ๋“ฑ ๋ฐ์ดํ„ฐ์˜ ๋ฌด๊ฒฐ์„ฑ ๊ฒ€์ฆ์— ์ฃผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

     

    SHA-256์€ ์–ด๋–ค ๊ธธ์ด์˜ ๊ฐ’์„ ์ž…๋ ฅํ•˜๋”๋ผ๋„ 256bit์˜ ๊ณ ์ •๋œ ํฌ๊ธฐ์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ๊ฐ’์ด ์กฐ๊ธˆ๋งŒ ๋ณ€๋™๋˜๋”๋ผ๋„ ๋ฐ˜ํ™˜๊ฐ’์ด ์™„์ „ํžˆ ๋‹ฌ๋ผ์ง€๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜ํ™˜๊ฐ’์„ ํ† ๋Œ€๋กœ ์ž…๋ ฅ๊ฐ’์„ ์œ ์ถ”ํ•˜๋Š” ๊ฒƒ์ด ๊ฑฐ์˜ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

     

    #import <CommonCrypto/CommonDigest.h>
    
    - (NSString*)SHA256Hash:(NSString*)text {
        const char* utf8chars = [text UTF8String];
        unsigned char result[CC_SHA256_DIGEST_LENGTH];
        CC_SHA256(utf8chars, (CC_LONG)strlen(utf8chars), result);
    
        NSMutableString *ret = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH*2];
        for(int i = 0; i<CC_SHA256_DIGEST_LENGTH; i++) {
            [ret appendFormat:@"%02x",result[i]];
        }
    
        return ret;
    }
    NSString *testText = [self SHA256Hash:@"testText"];

     

     

    ์˜จ๋ผ์ธ SHA-256 Hash ์ƒ์„ฑ๊ธฐ์— ๋™์ผ ๋ฉ”์‹œ์ง€๋ฅผ ์ž…๋ ฅํ–ˆ์„ ๋•Œ, ๋™์ผํ•œ ์ถœ๋ ฅ์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    SHA256 ํ•ด์‹œ - ์˜จ๋ผ์ธ SHA256 ํ•ด์‹œ ์ƒ์„ฑ๊ธฐ

     

    www.convertstring.com

     

     


     

    ๐Ÿ“Œ AES-256

    AES๋Š” ์•”ํ˜ธํ™” ๋ฐ ๋ณตํ˜ธํ™” ์‹œ ๋™์ผํ•œ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์นญํ‚ค ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. AES์˜ ์ข…๋ฅ˜์—๋Š” AES-128, AES-192, AES-256์ด ์žˆ๋Š”๋ฐ ๋’ค์— ๋ถ™์€ ์ˆซ์ž๊ฐ€ ๊ฐ๊ฐ ํ‚ค์˜ ๊ธธ์ด(256bit=32byte)๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. 

     

    AES๋Š” 128bit(=16byte)์˜ ๊ณ ์ •๋œ ๋ธ”๋ก ๋‹จ์œ„๋กœ ์•”ํ˜ธํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š”๋ฐ, 128bit๋ณด๋‹ค ์ž‘์€ ๋ธ”๋ก์ด ์ƒ๊ธธ ๊ฒฝ์šฐ ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์„ ํŠน์ • ๊ฐ’์œผ๋กœ ์ฑ„์›Œ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ž‘์—…์„ ํŒจ๋”ฉ์ด๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ, ๋Œ€ํ‘œ์ ์œผ๋กœ PKCS5, PKCS7 ๋ฐฉ์‹์ด ์žˆ์Šต๋‹ˆ๋‹ค.

     

    ์•”ํ˜ธํ™”๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ Block Cipher Mode๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋Œ€ํ‘œ์ ์œผ๋กœ CBC, ECB ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ๋กœ CBC ๋ฐฉ์‹์ด ๊ถŒ์žฅ๋˜๋Š”๋ฐ, ๋ธ”๋ก์„ ๊ทธ๋Œ€๋กœ ์•”ํ˜ธํ™”ํ•˜์ง€ ์•Š๊ณ  ์ด์ „์— ์•”ํ˜ธํ™”ํ–ˆ๋˜ ๋ธ”๋ก๊ณผ์˜ XOR ์—ฐ์‚ฐ์„ ์„ ํ–‰ํ•œ ๋’ค ์•”ํ˜ธํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋•Œ, ์ฒซ ๋ฒˆ์งธ ๋ธ”๋ก์€ ์ด์ „ ์•”ํ˜ธํ™” ๋ธ”๋ก์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ์—ญํ• ์„ ํ•ด ์ค„ IV(Initialization Vector)๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. IV๋Š” ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ 128bit(=16byte) ํฌ๊ธฐ์—ฌ์•ผ ํ•˜๊ณ , IV๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์ฒซ ๋ฒˆ์งธ ๋ธ”๋ก์„ ์•”ํ˜ธํ™”ํ•ฉ๋‹ˆ๋‹ค.

     

     

     

    NSData ํด๋ž˜์Šค์— AES256 ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด ์•”ํ˜ธํ™” ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

     

    #import <CommonCrypto/CommonCryptor.h>
    
    #define key @"YOUR_KEY"
    
    @implementation NSData (AES256)
    
    - (NSData *)cryptOperation:(CCOperation)operation {
        char keys[kCCKeySizeAES256 + 1];
        bzero(keys, sizeof(keys));
        
        [key getCString:keys maxLength:sizeof(keys) encoding:NSUTF8StringEncoding]; 
        
        NSUInteger dataLength = [self length];
        size_t bufferSize = dataLength + kCCBlockSizeAES128;
        void *buffer = malloc(bufferSize);
    
        size_t numBytes = 0;
        
        // ์ž„์˜๋กœ ์„ค์ •ํ•œ IV
        const char *ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
        
        CCCryptorStatus status = CCCrypt(operation, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                         keys, kCCKeySizeAES256,
                                         ivBytes,
                                         [self bytes], dataLength, /* input */
                                         buffer, bufferSize, /* output */
                                         &numBytes);
        if (status == kCCSuccess) {
            return [NSData dataWithBytesNoCopy:buffer length:numBytes];
        }
        free(buffer);
        return nil;
    }
    
    - (NSData *)AES256Encrypt {
        return [self cryptOperation:kCCEncrypt];
    }
    
    - (NSData *)AES256Decrypt {
        return [self cryptOperation:kCCDecrypt];
    }
    
    @end

     

    ์ด ๋ฉ”์„œ๋“œ๋“ค์„ ์ด์šฉํ•ด ๋ฌธ์ž์—ด์„ ์•”ํ˜ธํ™”ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ค๋‹ˆ๋‹ค.

    - (NSString *)AES256Encrypt:(NSString *)string {
        NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
        NSData *dataEncrypted = [data AES256Encrypt];
        NSString *strEncrypted = [dataEncrypted base64EncodedStringWithOptions:0];
        return strEncrypted;
    }
    
    - (NSString *)AES256Decrypt:(NSString *)string {
        if([string containsString:@"\n"] || [string containsString:@"\t"]) {
            string = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@""];
            string = [string stringByReplacingOccurrencesOfString:@"\t" withString:@""];
        }
        NSData *keyData = [[NSData alloc] initWithBase64EncodedString:string options:0];
        NSData *dataDecrypted = [keyData AES256Decrypt];
        NSString *receivedDataDecryptString = [[NSString alloc]initWithData:dataDecrypted encoding:NSUTF8StringEncoding];
        return receivedDataDecryptString;
    }

     

    ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

    NSString *encryptText = [self AES256Encrypt:@"testText"];
    NSString *decryptText = [self AES256Encrypt:@"testText"];

     

     

     

    ์ฐธ๊ณ 

    https://stackoverflow.com/questions/45832991/how-to-encrypt-with-aes-256-cbc-in-objective-c

     

    ๋Œ“๊ธ€

Designed by Tistory.