initWithData returns(空)
initWithData returns (null)
好吧,我有一个选择文本的代码,并将其转换为 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];
那 return 一个空字符串,在这种情况下我需要将那个字符串插入到文本文件中,如何解决这个问题?为什么这个 return 是空值?
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 加密代码工作正常,它将 return 看起来基本上是随机的二进制数据。它实际上不能转换为字符串,除非您执行类似逐字节遍历它并输出十六进制值的操作。
您可以使用其中一种 writeToFile
方法将加密的 NSData 直接写入文件。
随机字节,也就是加密的样子,无法区分,很少能转换成字符串。这就是加密数据通常采用 Base64 编码以生成可打印字符的原因。
使用- base64EncodedStringWithOptions:
将加密数据编码为字符串。
好吧,我有一个选择文本的代码,并将其转换为 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];
那 return 一个空字符串,在这种情况下我需要将那个字符串插入到文本文件中,如何解决这个问题?为什么这个 return 是空值?
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 加密代码工作正常,它将 return 看起来基本上是随机的二进制数据。它实际上不能转换为字符串,除非您执行类似逐字节遍历它并输出十六进制值的操作。
您可以使用其中一种 writeToFile
方法将加密的 NSData 直接写入文件。
随机字节,也就是加密的样子,无法区分,很少能转换成字符串。这就是加密数据通常采用 Base64 编码以生成可打印字符的原因。
使用- base64EncodedStringWithOptions:
将加密数据编码为字符串。