nsstringaesnsmutabledata

How to assign NSString directly to NSMutableData?


I am developing an application including sqlite database. I want to encrypt and decrypt data's inside the database using AES encryption. I have successfully implemented the AES encryption and inserted the encrypted data's in the database. And am fetching the data's into a NSString.

Then how can I assign that string without changing the string value into a NSMutableData. Because I want to pass that data to AES decrypt method.


Solution

  • Here is some example of NSString <-> NSData conversion:

    NSString *someString = @"string";
    
    //NSString to NSData
    NSData* data=[someString dataUsingEncoding: [NSString defaultCStringEncoding]];
    
    //NSData to NSString
    someString = [[NSString alloc] initWithData:data encoding:[NSString defaultCStringEncoding]];
    
    NSLog(@"%@", someString);