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??
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.
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];
Related
I need to undergo a process of creating a signature from the following process in Objective C:
ASCII encode an NSString
Hash the results with MD5
Perform bitwise operation on the resulting bytes (&127)
Convert to Base64 string
I am stuck on where to start. I am able to complete this task in C# but am at a loss in Objective C or even ANSI C.
I have got as far as getting the UTF8String from the initial NSString using:
[NSString UTF8String]
Hopefully you can help.
Read String Conversions
Dowload NSString MD5 Category by H2CO3
Take your pick of Base64 encoders
You mentioned you are having problems with String conversions, this is what you want to convert to ASCII:
NSString* src = #"";
NSData* data = [src dataUsingEncoding: NSASCIIStringEncoding allowLossyConversion: YES];
NSString* ascii = [[NSString alloc] initWithData: data encoding: NSASCIIStringEncoding];
To perform the bitwise operations is a little complicated. You will want to do this in straight C, like so:
NSString* src = <MD5 hashed result>
NSMutableString* dst = [[NSMutableString alloc] initWithCapacity: src.length];
for(NSUInteger i=0;i<src.length;i++) {
unichar c = [src characterAtIndex: i];
[dst appendFormat: #"%d", (c & 127)];
}
//Base64 encode dst
The 3rd party should tell you how to do it if they require it.
How can i pass NSData object over Wifi network? can any one provide me the code to send and receive the NSData over Wifi.or any sample code/app reference .
Assuming that you know how to send data in general, here is the code:
uint8_t *bytes = (uint8_t)[myData bytes];
size_t length = [myData length];
sendBytesWithLength(bytes, length);
On the receiver side you regenerate your NSData object like this:
uint8_t *bytes = ; // Get the bytes from somewhere...
size_t length = ; // And the length
NSData *data = [[NSData alloc] initWithBytes:(const void *)bytes length:length];
Have you tried looking at the Bonjour references in the first place to set up the connection? That should lead you on to the other options for network communication.
I need convert NSData to Binary data for send to php server
You don't need to convert NSData. Just set the post body of the NSMutableURLRequest.
const void *p = [nsDataObject bytes];
NSUInteger length = [nsDataObject length];
*cipher.h, cipher.m all code : http://watchitlater.com/blog/2010/02/java-and-iphone-aes-interoperability
Cipher.m
-(NSData *)encrypt:(NSData *)plainText{
return [self transform:KCCEncrypt data:plainText;
}
step1.
Cipher *cipher = [[Cipher alloc]initWithKey:#"1234567890"];
NSData *input = [#"kevin" dataUsingEncoding:NSUTF8StringEncoding];
NSData *data = [cipher encrypt:input];
data variables NSLog print : <4d1c4d7f 1592718c fd588cec 84053e35>
step2.
NSString *changeVal = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
data variables NSLog print : null
NSData to NSString by changing the value null is returned. By converting NSString NSURLConnection want to transfer. I need you help
Kevin, I actually take the result of the cipher encrypt method and use an extension to NSData to convert it to a Base64 encoded string. The cipher text does NOT convert to a valid UTF8 string by itself.
In an iphone application, I'm looking to convert a float to NSData for it to be sent over bluetooth and then converted back again when it's received. I have the bluetooth part working fine, but when I use this to convert to NSData:
NSData *data = [[NSData alloc]init];
float z = 9.8574; // Get the float value, 9.8574 is just an example
[data getBytes:&z length:sizeof(float)];
I can not convert it back to a float. I've tried a couple of methods but I'm wondering if this is the correct way to encode the float to NSData??
Thanks
Here is how to encode and decode a float with NSData:
encoding:
NSMutableData * data = [NSMutableData dataWithCapacity:0];
float z = ...;
[data appendBytes:&z length:sizeof(float)];
decoding:
NSData * data = ...; // loaded from bluetooth
float z;
[data getBytes:&z length:sizeof(float)];
A couple of things to note here:
1. You have to use NSMutableData if you are going to add things to the data object after creating it. The other option is to simply load the data all in one shot:
NSData * data = [NSData dataWithBytes:&z length:sizeof(float)];
2. the getBytes:length: method is for retrieving bytes from an NSData object, not for copying bytes into it.