使用AES加密的时候(encryptString:(NSString*)string withKey:(NSString*)key) 出现结果是nil
来源:互联网 发布:java编程框架 编辑:程序博客网 时间:2024/06/01 22:24
//问题代码
+(NSString*)encryptString:(NSString*)string withKey:(NSString*)key{
NSString *result =nil;
NSData *data = [stringdataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",[data AES256EncryptWithKey:key]);
result = [[NSStringalloc] initWithData:[dataAES256EncryptWithKey:key] encoding:NSUTF8StringEncoding];
return result;
}
//解决方式
+(NSString*)encryptString:(NSString*)string withKey:(NSString*)key{
NSString *result =nil;
NSData *data = [stringdataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",[dataAES256EncryptWithKey:key]);
result = [[NSStringalloc] initWithData:[GTMBase64encodeData:[data AES256EncryptWithKey:key]]encoding:NSUTF8StringEncoding];
return result;
}
解决方案来自:http://stackoverflow.com/questions/29503546/initwithdata-returns-null
initWithData returns (null)
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.
2 Answers
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.
I expect your initWithData
is failing because dataEnc
most 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 writeToFile
methods.
- 使用AES加密的时候(encryptString:(NSString*)string withKey:(NSString*)key) 出现结果是nil
- NSString 转NSURL的时候会出现NSURL为空
- NSString中让人费解的nil和NSNull
- NSString的使用
- NSString的使用
- Nsstring 的 简单 使用
- NSString的使用
- NSString 的stringByTrimmingCharactersInSet使用
- NSString的简单使用
- NSString的使用
- NSString的简单使用
- NSString字符串的使用
- NSString使用的延伸
- NSString的使用
- 使用Testflight发现的NSString和String转换的问题
- String与NSString的区别,以及各自的使用场景
- NSString跟Swift String的区别和使用场景
- 解决NSData转NSString返回nil的问题
- 多线程编程
- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- CCF模拟题-1~5
- Codeforces Round #384 (Div. 2) A. Vladik and flights【思维】水题
- Android笔记:通过代码设置系统壁纸
- 使用AES加密的时候(encryptString:(NSString*)string withKey:(NSString*)key) 出现结果是nil
- codeforces #384 Vladik and fractions (构造算法)
- apk重新签名
- 把ArrayList转化为json对象
- dubbo学习笔记 一 源码编译
- 初级音乐播放器——播放存储卡上的一首音乐,能够显示歌曲名和播放进度,并且能够播放和暂停(没有SD卡会报错)
- jenkins集成html report 不显示
- SPRD Android 6.0 安装美图美颜相机录像时,提示没有mic权限的问题。
- iOS 8 WkWebView 网页的配置和前进,后退,js 交互和进度条的加载