2

Previously I did Triple DES encryption to encrypt some data using a string key file, by defining a method

+ (NSData *)tripleDESEncryptWithKey:(NSString *)key dataToEncrypt:(NSData*)convertedData   {}. 

Now, similarly I am doing AES 256 encryption. But this time, I can not use a string as the key. I need to get NSData from a file in resources using

NSData *keyData = [NSData dataWithContentsOfFile:keyPath];

As I need to pass this key as parameter, I tried to convert it into string with

NSString *key = [[NSString alloc] initWithData:keyData encoding:NSUTF8StringEncoding];

but it returned NULL. So, How to use the Data key for AES 256 encryption ?

EDIT: I got that I should not use UTF8 encoding as the data file was made by Base64 encoding of a string. So Now the question is, how to get the string key using Base64 from keyData ?

utsabiem
  • 920
  • 4
  • 10
  • 21
  • Did you already use breakpoint tool to ensure that `keyData` variable is not `NULL`? check your `keyPath` – Sakares May 28 '12 at 18:05
  • Keypath is fine. Keydata also exists: <792f83e5 806c5f51 abf8fdba 9b371610 b8f2c474 b24d89d2 707c7da0 815b964a>. I checked it long back using breakpoints. – utsabiem May 29 '12 at 06:53

2 Answers2

0

If key variable returned NULL. Try to use like this.

NSString* key = [NSString stringWithUTF8String:[keyData bytes]];

Refer to KennyTM's Answer

UPDATE 1: You can also read the string directly from your file without convert in NSData

By NSFileHandle:

NSString * path = @"your key file path";
NSFileHandle * fileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSData * buffer = nil;
while ((buffer = [fileHandle readDataOfLength:1024])) {
    //do something with buffer
}

or use NSString

NSString * key = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

Refer to Dave DeLong's Answer

Note: Ensure your keyPath variable , Is it correct in the format of full or relative path?

UPDATE 2: You can also deal with Base64 Encoding with Matt Gallagher NSData+Base64 Class&Header (See also his blog there)

For encode base64

NSString * key = [[path dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString];

Hope it helps you!

Community
  • 1
  • 1
Sakares
  • 606
  • 12
  • 31
  • Tried that. Still it is null ! – utsabiem May 29 '12 at 06:52
  • Actually, you need to read the key string from your file in resource asset,right? just read the string from your file directly . see my update answer – Sakares May 29 '12 at 09:13
  • EDIT: I got that I should not use UTF8 encoding as the data file was made by Base64 encoding of a string. So Now the question is, how to get the string key using Base64 from keyData ? – utsabiem May 30 '12 at 14:22
  • I guess, I can use this line of your answer: NSString * key = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; , but I only need to use Base64 instead of NSUTF8StringEncoding. How ? – utsabiem May 30 '12 at 14:23
0

Read the file:

NSData* myData = [NSData dataWithContentsOfFile:myFileWithPath];

Then use one of the implementations in this post:

Base64 encoding options on the Mac and iPhone

to do your Base64 conversion.

Note: simply sticking your decryption key into a file is a bad idea. Optimally, the key would be stored securely by being encrypted by the user's own key.

Vikas S Singh
  • 1,748
  • 1
  • 14
  • 29
wadesworld
  • 13,535
  • 14
  • 60
  • 93