ios - initWithData 返回(空)

标签 ios objective-c nsstring nsdata

好吧,我有一个选择文本的代码,并将其转换为 NSData,然后使用 AES-256 对其进行加密,然后将此 NSData 转换为 NSString 以显示加密的消息,我按如下方式执行此操作:

 NSString *text = @"This is a simple text";

    NSData* data = [text dataUsingEncoding:NSUTF8StringEncoding];

    NSData *dataEnc = [data AES256EncryptWithKey:@"12556"];

    NSString *stringCrypt = [[NSString alloc] initWithData:dataEnc encoding:NSUTF8StringEncoding];

    NSData *dataDesc = [dataEnc AES256DecryptWithKey:@"12556"];

    NSString *string = [[NSString alloc] initWithData:dataDesc encoding:NSUTF8StringEncoding];

    NSLog(@"Text normal -> %@",text);

    NSLog(@"Text with AES -> %@",stringCrypt);

    NSLog(@"Text with AES decrypted -> %@",string);

我的 NSLog 的输出是:

Text normal -> this is a simple text 
Text with AES -> (null) 
Text With AES decrypted -> this is a simple text

所以问题出在代码上:

NSString *stringCrypt = [[NSString alloc] initWithData:dataEnc encoding:NSUTF8StringEncoding];

返回一个空字符串,在这种情况下我需要将该字符串插入到文本文件中,如何解决这个问题?为什么这会返回空值?

EDIT

将其转换为AES256的函数是:

- (NSData *)AES256EncryptWithKey:(NSString *)key {
    // 'key' should be 32 bytes for AES256, will be null-padded otherwise
    char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)

    // fetch key data
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    NSUInteger dataLength = [self length];

    //See the doc: For block ciphers, the output size will always be less than or
    //equal to the input size plus the size of one block.
    //That's why we need to add the size of one block here
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);

    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                          keyPtr, kCCKeySizeAES256,
                                          NULL /* initialization vector (optional) */,
                                          [self bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        //the returned NSData takes ownership of the buffer and will free it on deallocation
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }

    free(buffer); //free the buffer;
    return nil;
}

Solved

正如用户 Zaph 所说,我需要使用 -base64EncodedDataWithOptions,在我的例子中,我这样做:

NSString *text = @"This is a simple text";

    NSData* data = [text dataUsingEncoding:NSUTF8StringEncoding];

    NSData *dataEnc = [data AES256EncryptWithKey:@"12556"];

    NSData *daes = [dataEnc base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];

    NSString *stringCrypt = [[NSString alloc] initWithData:daes encoding:NSUTF8StringEncoding];

    NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:stringCrypt options:0];

    NSData *dataDesc = [decodedData AES256DecryptWithKey:@"12556"];

    NSString *string = [[NSString alloc] initWithData:dataDesc encoding:NSUTF8StringEncoding];

    NSLog(@"Text Normal -> %@",text);

    NSLog(@"Text with AES (BASE64) -> %@",stringCrypt);

    NSLog(@"Text with AES decrypted -> %@",string);

在这种情况下,我只需要将它转换为 base64 字符串,现在我可以将这些值存储在文本值中,其他设备可以获取该文本并进行解密。谢谢大家。

最佳答案

我预计您的 initWithData 会失败,因为 dataEnc 肯定不包含 UTF8 字节。假设您的 AES256 加密代码工作正常,它将返回看起来基本上是随机的二进制数据。它实际上无法转换为字符串,除非您执行类似逐字节遍历它并输出十六进制值的操作。

您可以使用其 writeToFile 之一将加密的 NSData 直接写入文件方法。

关于ios - initWithData 返回(空),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29503546/

相关文章:

ios - 如何通过菜单按钮打开新的 View Controller ?

ios - 如何在 iOS 中从 NIB 加载 NIB

ios - 创建 session UUID Swift

ios - 在单元格中添加和修改多个约束?

ios - UIBezierPath 剪辑 UIIimage

ios - 将 NSString 转换为其他 NSString

ios - 在等待上一个执行完成时循环遍历 block

ios - 在我的 Storyboard View 上方显示 Nib

javascript - 在 Canvas 上绘制 html5 视频 - 在 iPad 上

iphone - 将省略号添加到 NSString