使用AES加密的时候(encryptString:(NSString*)string withKey:(NSString*)key) 出现结果是nil
2016-12-15 11:57
351 查看
//问题代码
+(NSString*)encryptString:(NSString*)string withKey:(NSString*)key{
NSString *result =
nil;
NSData *data = [string
dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",[data AES256EncryptWithKey:key]);
result = [[NSString
alloc] initWithData:[data
AES256EncryptWithKey:key]
encoding:NSUTF8StringEncoding];
return result;
}
//解决方式
+(NSString*)encryptString:(NSString*)string withKey:(NSString*)key{
NSString *result =
nil;
NSData *data = [string
dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",[data
AES256EncryptWithKey:key]);
result = [[NSString
alloc] initWithData:[GTMBase64
encodeData:[data AES256EncryptWithKey:key]]
encoding:NSUTF8StringEncoding];
return result;
}
解决方案来自:http://stackoverflow.com/questions/29503546/initwithdata-returns-null
activeoldestvotes
+(NSString*)encryptString:(NSString*)string withKey:(NSString*)key{
NSString *result =
nil;
NSData *data = [string
dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",[data AES256EncryptWithKey:key]);
result = [[NSString
alloc] initWithData:[data
AES256EncryptWithKey:key]
encoding:NSUTF8StringEncoding];
return result;
}
//解决方式
+(NSString*)encryptString:(NSString*)string withKey:(NSString*)key{
NSString *result =
nil;
NSData *data = [string
dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",[data
AES256EncryptWithKey:key]);
result = [[NSString
alloc] initWithData:[GTMBase64
encodeData:[data AES256EncryptWithKey:key]]
encoding:NSUTF8StringEncoding];
return result;
}
解决方案来自:http://stackoverflow.com/questions/29503546/initwithdata-returns-null
initWithData
returns (null)
up vote0down votefavorite | Well, I have a code that selects a text, and converts it to NSData then encrypts it with AES-256 and then converts this NSData to a NSString to be displayed the message encrypted, I'm doing this as follows: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); The output of my NSLog is: Text normal -> this is a simple text Text with AES -> (null) Text With AES decrypted -> this is a simple text So the problem is with the code: NSString *stringCrypt = [[NSString alloc] initWithData:dataEnc encoding:NSUTF8StringEncoding]; That return a null string, and in this case I need that string that will be inserted into a text file, how to solve this problem? why this returning a null value? EDIT The function to convert it to AES256 is: - (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 As the user Zaph says, I need to use the -base64EncodedDataWithOptions, in my case I do as follows: 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); In this case I only need to convert it to a base64 string, and now I can store this values inside a text value, and other device can get this text and decrypt. Thanks for all. ios objective-c nsstring nsdata
| ||||||||||||||||||||
a comment |
2 Answers
activeoldestvotesup vote1down voteaccepted | Random bytes, and that is what encrypted looks like and can not be distinguished from, rarely is convertible to a string. This is why encrypted data is often Base64 encoded to produce printable characters. Use - base64EncodedStringWithOptions:to encode the encrypted data to a character string.
| ||||
a comment |
up vote2down vote | I expect your initWithDatais failing because dataEncmost certainly does not contain UTF8 bytes. Assuming your AES256 encryption code is working correctly, it is going to return binary data that looks essentially random. It will not be realistically convertible to a string unless you do something like go through it byte by byte and output hex values. You can write the encrypted NSData to a file directly using one of its writeToFilemethods.
| ||||||||||||||||
|
相关文章推荐
- db2存储过程在关联使用left join 时候出现的不是自己想要的结果可能性,以及解决办法
- 使用SudzC生成ARC版Obj-C代码出现返回结果为nil的疑问与暂时解决办法
- 使用openssl_encrypt方法替代mcrypt_encrypt做AES加密
- 在测试aes加密的时候,出现一个关于密钥的长度问题
- 解决使用nvelocity时候template文件里面包含中文输出结果时候出现乱码的问题
- 在Java中使用AES加密,并且加密的Key长度在16位以上
- AES加密出现InvalidKeyException: Illegal key size解决方案
- 使用MPMediaPlayerController播放视频提示[NSURL initFileURLWithPath:]: nil string parameter错误的解决办法
- 使用AES加密解密在windows上使用没有问题,在Linux(Centos6.5)上出现解密失败的解决方法
- 使用openssl 生成AES加密并且对加密结果进行base64加密
- 问题: 在eclipse里使用ctr+shift+t打开一个类的时候,出现错误:Class file name must end with .class
- 关于使用JSON库的时候,出现[NSCFString JSONValue]: unrecognized selector sent to instance的问题
- Use AES Encrypt And Decrypt With Custom Key( Expand AesCryptoServiceProvider Class)
- 使用AES时出现 Illegal key size or default parameters 时的解决办法
- Laravel 出现"RuntimeException inEncrypter.php line 43: The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths."问题的解决办法
- NSURL使用urlWithString得到的对象为nil的问题原因以及解决
- 使用xdebug调试出现Waiting for incoming connection with ide key XX
- 使用友盟分享的时候出现 data parameter is nil
- 关于使用JSON库的时候,出现[NSCFString JSONValue]: unrecognized selector sent to instance的问题
- 关于使用JSON库的时候,出现[NSCFString JSONValue]: unrecognized selector sent to instance的问题