uint8_t []数组转换为NSString得到(null)

时间:2013-07-13 03:05:42

标签: objective-c nsstring nsdate uint8t

我不知道为什么(char *)uint8_t数组转换为NSString不起作用。 ivString总是(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字节

cipherString NSString * 0x00000000

initialisationVector uint8_t [16]

ivString NSString * 0x00000000

密钥NSString * 0x000d28c4 @“1234567890123456”

plain NSData * 0x1ed80760 9 bytes

P.S。

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

这也行不通。仍然得到(null)

我打电话的方法是:

- (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];
}

1 个答案:

答案 0 :(得分:0)

最后,我得到了答案! 我只需要将uint8_t []数组视为char []数组。然后,通过下面的行将char []数组转换为NSString:

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

结果是: ivString NSString * 0x1d8489c0 @“¹U¡hÌTÖÆáÎÍÍ”