好吧,我有一个选择文本的代码,然后将其转换为NSData,然后使用AES-256对其进行加密,然后将此NSData转换为NSString以显示加密的消息,我这样做如下: / p>
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字符串,现在我可以将这些值存储在文本值中,其他设备可以获取此文本并解密。谢谢大家。
答案 0 :(得分:2)
我希望您的initWithData
失败,因为dataEnc
肯定不包含UTF8字节。假设您的AES256加密代码工作正常,它将返回看起来基本上随机的二进制数据。它不会实际转换为字符串,除非您执行类似的操作,逐字节输出并输出十六进制值。
您可以使用其中一种writeToFile
方法直接将加密的NSData写入文件。
答案 1 :(得分:1)
随机字节,即加密的外观和无法区分的内容,很少可转换为字符串。这就是为什么加密数据通常是Base64编码以产生可打印字符的原因。
使用- base64EncodedStringWithOptions:
将加密数据编码为字符串。