Strategy for generating and saving password for encryption and decryption

516 views Asked by At

I'm using the RNEncryptor and RNDecryptor classes for encryption and decryption as follows:

NSData *encryptedData = [RNEncryptor encryptData:input
                                    withSettings:kRNCryptorAES256Settings
                                        password:thePassword
                                           error:nil];

NSData *output = [RNDecryptor decryptData:encryptedData
                             withSettings:kRNCryptorAES256Settings
                                 password:thePassword
                                    error:nil];

The first time I have to encrypt data in my app I generate a password using the RNCryptor class as follows:

NSData *thePasswordData = [RNCryptor randomDataOfLength:32];
NSString *thePassword = [aesPasswordData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

I save this generated password in the app's Keychain and use it for all subsequent encryption and decryption.

Is this a good strategy for encryption/decryption or can anyone see any flaws in it? Should I instead be generating an AES key and storing that in the Keychain and working with that rather than a password?

Edit: I changed above how I generate the password from using the NSProcessInfo class to using the RNCryptor class based on Rob Napier's answer. That aside I'm still curious to know whether the strategy in general of working with a password rather than an AES key is correct and secure.

2

There are 2 answers

3
Rob Napier On

This is not a good way to pick a password. globallyUniqueString can be quite predictable in many of its bits. You're much better off using something like RNCryptor.randomDataOfLength to generate a blob of desired length (32 bytes would be ideal), and then base-64 encode it to get a password.

Using randomDataOfLength to generate keys instead wouldn't really be any more secure, but it would be faster (by 10s of milliseconds depending on the device, if that matters to you). In general, I recommend using the password interface unless you have a special problem where keys are particularly helpful. Keys are just a little harder to use correctly.

2
J.B On

The actual encryption key used by these libs is derived from the password, if you use one, but, IIRC, salted so it isn't intrinsically less secure.

There are concerns regarding the theoretical level of security provided by these libraries, (see How to correctly encrypt data with proper authentication using AES-256-CBC in php?) but you have to make a judgement call on that. Thing to bear in mind is that RNCryptor is a set of libraries (which may have published attack vectors in different implementations) AND a consistent format, which is useful if you want to move encrypted data across platforms.

Personally, I would feel more comfortable with using a unique, random AES key for each encryption and storing that key encrypted with RSA stored in the keychain.