Create random 128 bit AES Encryption key in iOS

前端 未结 3 2105
小蘑菇
小蘑菇 2020-12-30 16:59

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.

UP

3条回答
  •  孤独总比滥情好
    2020-12-30 17:10

    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.

提交回复
热议问题