initWithData返回(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];
这返回一个空字符串,在这种情况下,我需要将插入到文本文件的字符串,如何解决这个问题? 为什么这返回一个空值?
编辑
将其转换为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;
}
解决了
正如用户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字符串,现在我可以将这些值存储在文本值中,其他设备可以获取此文本并进行解密。 感谢所有。
随机字节,这就是加密的样子,无法区分,很少可以转换为字符串。 这就是为什么加密数据经常被Base64编码以产生可打印字符的原因。
使用- base64EncodedStringWithOptions:
将加密数据编码为字符串。
我期望你的initWithData
失败,因为dataEnc
肯定不包含UTF8字节。 假设您的AES256加密代码正常工作,它将返回看起来基本上是随机的二进制数据。 它不会真实地转换为字符串,除非您按字节逐字地输入并输出十六进制值。
您可以使用其writeToFile
方法之一直接将加密的NSData写入文件。