Frage

I am building a simple iphone (SDK6.1) application that encrypts some user's notes, stores the into a database and when user enters a password (does not need to be encrypted) it will decrypt his notes and show them to him.

For the database i am using Core Data (.xcdatamodel). The encrypted text at the moment is declared as String in the data model and in the Notes.h fil,e it is declared as NSString.

For the Encryption i am using apple's sample code from CryptoExercise which works perfectly.

The problem is that when i try to save the encrypted text in the database and then decrypt it i am not getting the desired results.. basically i am getting an empty string back.

Obviously i am using the following code to convert from uint8_t to NSString so i can store it into the data model and i understand that this is my main problem.

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];

As i said before i understand that converting uint8_t is the main problem here and i would like to know which is the correct way to do this?

Is it possible with Data Model at all, or should i go to SQLite??

War es hilfreich?

Lösung 2

So just to answer the question so i can use code and everything..

I changed the EncryptedText in the Data Model to Binary Data and then the code would look like this:

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];

Andere Tipps

You cannot convert arbitrary bytes sequences to NSString and back like that. For example, if data contains the single byte 128 (hex 0x80), then

NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

creates a string with one Unicode character U+0080. When you convert this back to NSData with

NSData *d1 = [string dataUsingEncoding:NSUTF8StringEncoding];

then d1 will contain the bytes 0xC2 0x80 (which is the UTF-8 for U+0080). But

NSData *d2 = [string dataUsingEncoding:NSASCIIStringEncoding];

does also not work (d2 = nil), because the string cannot be converted to 7-bit ASCII.

So you should either

  • store the encrypted data as "Binary Data" in Core Data, or
  • store the encrypted data as String, but choose a different conversion strategy, for example Base64.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top