0

I have no idea why the (char *)uint8_t array transform to NSString doesn't work. ivString always is (null)

- (IBAction)encryptPacketBtnTouchDown:(id)sender {
    NSString *key = @"1234567890123456";
    NSData *plain = [plainMessage.text dataUsingEncoding:NSUTF8StringEncoding];
    uint8_t initialisationVector[16];
    arc4random_buf(initialisationVector, 16);

    NSString* ivString =  [NSString stringWithUTF8String:(char *)initialisationVector];

    NSData *cipherData = [plain AES128EncryptWithKey:key iv:ivString];
    [asyncSocket writeData:cipherData withTimeout:-1.0 tag:0];
    NSString* cipherString = [[NSString alloc] initWithBytes:[cipherData bytes] 
                                                      length:[cipherData length] 
                                                    encoding:NSASCIIStringEncoding];
}

cipherData NSData * 0x1edaf3a0 16 bytes

cipherString NSString * 0x00000000

initialisationVector uint8_t [16]

ivString NSString * 0x00000000

key NSString * 0x000d28c4 @"1234567890123456"

plain NSData * 0x1ed80760 9 bytes

P.S.

NSString* ivString =  [[NSString alloc] initWithBytes:initialisationVector
                                               length:16
                                             encoding:NSUTF8StringEncoding];

This also won't works. Still got (null)

The method I am calling is :

- (NSData *)AES128Operation:(CCOperation)operation key:(NSString *)key iv:(NSString *)iv
{
    char keyPtr[kCCKeySizeAES128 + 1];
    memset(keyPtr, 0, sizeof(keyPtr));
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    char ivPtr[kCCBlockSizeAES128 + 1];
    memset(ivPtr, 0, sizeof(ivPtr));

    [iv getCString:ivPtr
         maxLength:sizeof(ivPtr)
          encoding:NSUTF8StringEncoding];

    NSUInteger dataLength = [self length];
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);

    size_t numBytesCrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(operation,
                                          kCCAlgorithmAES128,
                                          kCCOptionPKCS7Padding,
                                          keyPtr,
                                          kCCBlockSizeAES128,
                                          ivPtr,
                                          [self bytes],
                                          dataLength,
                                          buffer,
                                          bufferSize,
                                          &numBytesCrypted);
    if (cryptStatus == kCCSuccess) {
        return [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted];
    }
    free(buffer);
    return nil;
}

- (NSData *)AES128EncryptWithKey:(NSString *)key iv:(NSString *)iv
{
    return [self AES128Operation:kCCEncrypt key:key iv:iv];
}
Yi Jiang
  • 3,938
  • 6
  • 30
  • 62
  • `cipherData` and `initialisationVector` don't represent strings. They are arrays of bytes. Why are you trying to convert it to a string? – rmaddy Jul 13 '13 at 03:10
  • I need display the encryption result on the screen. But, that is not my question. My question is I can't convert Initialisation Vector uint8_t[] to NSString. Because the lib I am using required NSString as formate of IV. ivString == (null) is my problem. – Yi Jiang Jul 13 '13 at 03:13
  • But `initialisationVector` is a sequence of random bytes. Those bytes don't represent a UTF-8 encoded (or any other encoding) string. Where is this `AES128EncryptWithKey:iv:` method from? – rmaddy Jul 13 '13 at 03:16
  • IV is random bytes. You are right. But, I need input into the method as a parameter. The Parameter formate is NSString. So, I need transform the bytes array into NSString. – Yi Jiang Jul 13 '13 at 03:25
  • 1
    I understand you need a random string for the IV but you can't create an `NSString` from random bytes. I find it odd that this method uses `NSString` for the `iv` parameter. I suggest you update the method to take a `uint8_t` array instead of the `NSString`. Then you can pass in the random bytes directly and avoid the needless conversions to/from `NSString`. – rmaddy Jul 13 '13 at 03:34

1 Answers1

0

Finally, I got the answer!
I just need treat the uint8_t[] array as a char[] array. Then, transform the char[] array to NSString by below line:

NSString* ivString = [NSString stringWithCString:initialisationVector encoding:NSASCIIStringEncoding];

Result is: ivString NSString * 0x1d8489c0 @"¹U ¡hÌTÖÆáβÍ"

Yi Jiang
  • 3,938
  • 6
  • 30
  • 62