5

I want to create random AES Encryption key (128 bit) in ios. I have searched in SO but I cannot find a good answer. Please give me some advice. thanks in advance.

UPDATE:

I have used BBAES lib. I used the below code to generate the encryption key but when I convert from NSData to NSString, it shows NULL

  -(NSData*)randomDataWithLength{
    NSData* salt = [BBAES randomDataWithLength:BBAESSaltDefaultLength];
    NSData *key = [BBAES keyBySaltingPassword:@"password" salt:salt keySize:BBAESKeySize128 numberOfIterations:BBAESPBKDF2DefaultIterationsCount];
    NSLog(@"Data ASE Key %@",key);
    NSString *aString  = [[NSString alloc] initWithData:key encoding:NSUTF8StringEncoding];
}
user3214941
  • 387
  • 1
  • 9
  • 17

3 Answers3

12

Woah, that's complicated code for a simple task!

- (NSData *)random128BitAESKey {
    unsigned char buf[16];
    arc4random_buf(buf, sizeof(buf));
    return [NSData dataWithBytes:buf length:sizeof(buf)];
}

You probably heard somewhere that you should use salt and hash your passwords. It looks like you took this advice a little too far: there are no passwords here and yet your code still salts and hashes the data! This is completely useless when the input comes from a secure random number generator like arc4random.

Of course it won't convert to an NSString because random data is unlikely to be valid UTF-8 string.

Michael Voznesensky
  • 1,612
  • 12
  • 15
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • Thanks so much. But how can I write this key to file? Thanks – user3214941 May 08 '14 at 06:52
  • 4
    You're mostly right, but I believe that arc4random is not always cryptographically secure. `SecRandomCopyBytes` is the RNG function to use for AES keys. – Chris Devereux May 09 '14 at 19:34
  • 1
    @ChrisDevereux: Do you have a citation for this? As far as I know, arc4random is designed to be used for cryptography, so it should be fine as long as it was properly seeded. – Dietrich Epp May 09 '14 at 19:43
  • Hmmm, I might have been thinking about something else. Sorry. Looks like there are some issues with ARC4 but I couldn't say whether its relevant here... http://en.wikipedia.org/wiki/RC4 – Chris Devereux May 09 '14 at 21:27
12

You might want to use Apple's random byte generator for this which is considered more secure than arc4random.

int SecRandomCopyBytes ( SecRandomRef rnd, size_t count, uint8_t *bytes ); 

https://developer.apple.com/library/ios/documentation/Security/Reference/RandomizationReference/index.html#//apple_ref/c/func/SecRandomCopyBytes

A good explanation for this can be found on a blog post by James Carroll:

http://jamescarroll.xyz/2015/09/09/safely-generating-cryptographically-secure-random-numbers-with-swift/

Open Whisper Systems use this for the iOS version of their popular secure chat app Signal

Patrick Domegan
  • 261
  • 2
  • 5
-4

This might help

- (NSString *)getRandomKey{
    NSString *alphabet  = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY0123456789/=+";
    NSMutableString *s = [NSMutableString stringWithCapacity:20];
    for (NSUInteger i = 0; i < 20; i++) {
        u_int32_t r = arc4random() % [alphabet length];
        unichar c = [alphabet characterAtIndex:r];
        [s appendFormat:@"%C", c];
    }
    NSLog(@"%@", s);
    NSString *key = s;
    return key;
}
  • I won't use this algorithm for anything related to security – Bryan Chen May 08 '14 at 05:44
  • @BryanChen: Please tell me why wont use this algorithm ? – user3214941 May 08 '14 at 06:28
  • 1
    Often those random functions do depend on time which makes them predictable for an attacker. Also using arc4random is not recommended because of modulo bias (http://stackoverflow.com/questions/648739/objective-c-modulo-bias) – Quxflux May 08 '14 at 06:39
  • 1
    @Lukas: arc4random is not one of those RNGs that gets seeded with `time()`—actually, it's a secure RNG and quite slow compared to other RNGs, and it's the right choice for key generation. However, you are right about the modulo bias. – Dietrich Epp May 08 '14 at 06:41
  • Do not use `arc4random() % range` there will be some modulo bias, use `arc4random_uniform(range)`. – zaph Apr 03 '16 at 11:55