Iphone 将uint8_t密文保存到数据模型中
我正在构建一个简单的iphone(SDK6.1)应用程序,该应用程序对一些用户的笔记进行加密,并将其存储到数据库中,当用户输入密码(不需要加密)时,它将解密他的笔记并将其显示给他 对于我正在使用的数据库(.xcdatamodel)。此时加密的文本在数据模型中声明为字符串,在Notes.h fil,e中声明为NSString 对于加密,我使用的是来自CryptoExercise的apple示例代码,该代码非常有效 问题是,当我试图将加密文本保存在数据库中,然后对其进行解密时,我没有得到想要的结果。。基本上我得到了一个空字符串回来 显然,我正在使用以下代码将uint8_t转换为NSString,以便将其存储到数据模型中,我知道这是我的主要问题Iphone 将uint8_t密文保存到数据模型中,iphone,rsa,public-key-encryption,xcdatamodel,Iphone,Rsa,Public Key Encryption,Xcdatamodel,我正在构建一个简单的iphone(SDK6.1)应用程序,该应用程序对一些用户的笔记进行加密,并将其存储到数据库中,当用户输入密码(不需要加密)时,它将解密他的笔记并将其显示给他 对于我正在使用的数据库(.xcdatamodel)。此时加密的文本在数据模型中声明为字符串,在Notes.h fil,e中声明为NSString 对于加密,我使用的是来自CryptoExercise的apple示例代码,该代码非常有效 问题是,当我试图将加密文本保存在数据库中,然后对其进行解密时,我没有得到想要的结果。
uint8_t *cipherBuffer = NULL;
SecKey *encrypt = [[SecKey alloc]init];
NSString *et = [[NSString alloc]init];
[encrypt generateKeyPairRSA];
// Encrypt the plain text
cipherBuffer = [encrypt Encryption:plainTextField.text];
// Convert uint8_t to NSString
NSMutableData *data = [[NSMutableData alloc]init];
[data appendBytes:cipherBuffer length:strlen((char*)cipherBuffer)+1];
NSString *string = [[NSString alloc]initWithData:data encoding: NSASCIIStringEncoding];
// Save to Data Model
[self.currentNote setEncryptedText:string];
[self.delegate addNewNoteViewControllerDidSave];
// Retrieve encrypted text from database
et = [self.currentNote encryptedText];
// Convert back to uint_8
NSData *someData = [et dataUsingEncoding:NSUTF8StringEncoding];
const void *bytes = [someData bytes];
uint8_t *crypto_data = (uint8_t*)bytes;
// Decrypt Data
[encrypt Decryption:crypto_data];
正如我之前所说,我理解转换uint8_t是这里的主要问题,我想知道哪种方法是正确的
使用数据模型是否可能,或者我是否应该转到SQLite???您不能将任意字节序列转换为
NSString
并像那样返回。例如
如果data
包含单个字节128
(十六进制0x80
),则
创建包含一个Unicode字符U+0080的字符串。当您使用将其转换回NSData
时
NSData *d1 = [string dataUsingEncoding:NSUTF8StringEncoding];
然后d1
将包含字节0xC2 0x80
(这是U+0080的UTF-8)。但是
也不起作用(d2=nil
),因为字符串无法转换为7位
ASCII码
所以你要么
- 将加密数据作为“二进制数据”存储在核心数据中,或
- 将加密数据存储为字符串,但选择不同的转换策略, 例如Base64
uint8_t *cipherBuffer = NULL;
SecKey *encrypt = [[SecKey alloc]init];
[encrypt generateKeyPairRSA];
cipherBuffer = [encrypt Encryption:plainTextField.text];
NSMutableData *data = [[NSMutableData alloc]init];
[data appendBytes:cipherBuffer length:strlen((char*)cipherBuffer)+1];
// Save cipher into the Data Model
[self.currentNote setEncryptedText:data];
[self.delegate addNewNoteViewControllerDidSave];
// Retrieve cipher back from Data Model
NSData *etData = [[NSData alloc]init];
etData = [self.currentNote encryptedText];
// Convert back to uint8_t
const void *bytes = [etData bytes];
uint8_t *crypto_data = (uint8_t*)bytes;
// De cypher
[encrypt Decryption:crypto_data];
非常感谢,我知道转换是错误的,但由于我是iphone新手,我不知道如何修复它。我用两种方法都解决了这个问题,但我想第一种方法(存储为二进制数据)最适合这种情况。@GeorgeNikolaides:是的,存储为二进制数据是更好的方法,这就是为什么我把它放在可能的解决方案列表的第一位。
NSData *d2 = [string dataUsingEncoding:NSASCIIStringEncoding];
uint8_t *cipherBuffer = NULL;
SecKey *encrypt = [[SecKey alloc]init];
[encrypt generateKeyPairRSA];
cipherBuffer = [encrypt Encryption:plainTextField.text];
NSMutableData *data = [[NSMutableData alloc]init];
[data appendBytes:cipherBuffer length:strlen((char*)cipherBuffer)+1];
// Save cipher into the Data Model
[self.currentNote setEncryptedText:data];
[self.delegate addNewNoteViewControllerDidSave];
// Retrieve cipher back from Data Model
NSData *etData = [[NSData alloc]init];
etData = [self.currentNote encryptedText];
// Convert back to uint8_t
const void *bytes = [etData bytes];
uint8_t *crypto_data = (uint8_t*)bytes;
// De cypher
[encrypt Decryption:crypto_data];