简体   繁体   English

RNCryptor“ encryptData”返回空

[英]RNCryptor “encryptData” returning null

New to iOS dev, trying to work on encoding data for secure data storage in db. iOS开发人员的新手,尝试对数据进行编码以将数据安全存储在db中。

I found the current example here: https://github.com/RNCryptor/RNCryptor-objc 我在这里找到了当前示例: https : //github.com/RNCryptor/RNCryptor-objc

This is my code. 这是我的代码。

NSString * aPassword =@"tempkey";

NSData *data = [@"Data" dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSData *encryptedData = [RNEncryptor encryptData:data
                                    withSettings:kRNCryptorAES256Settings
                                        password:aPassword
                                           error:&error];


NSLog(@"Data: %@", [[NSString alloc] initWithData:encryptedData encoding:NSUTF8StringEncoding]);

My log 我的日志

2016-10-20 11:41:52.662 BlueBoard[57245:10027277] Data: (null)

Am I missing a step in this process? 我在这个过程中缺少步骤吗? I've confirmed that it's null because it the db its being stored as null as well. 我已经确认它为null,因为它的数据库也被存储为null。

Your issue isn't that encryptedData is nil , it's that you are attempted to create an NSString from data that doesn't represent a string. 您的问题不是不是encryptedDatanil ,而是您试图从不代表字符串的数据中创建NSString

If you wish to convert encryptedData into a string for storage or other purposes, you should convert the data into a Base 64 encoded representation. 如果希望将encryptedData转换为字符串以用于存储或其他目的,则应将数据转换为以Base 64编码的表示形式。 Do this with the base64EncodedStringWithOptions: method. 使用base64EncodedStringWithOptions:方法执行此操作。

NSString *base64String = [encryptedData base64EncodedStringWithOptions:0];

Of course when you want to decrypt the string later, you will need to convert the Base 64 encoded string back into NSData , and then decrypt that data. 当然,当您以后想要解密字符串时,需要将Base 64编码的字符串转换回NSData ,然后解密该数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM