Create random 128 bit AES Encryption key in iOS

前端 未结 3 2106
小蘑菇
小蘑菇 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:06

    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;
    }
    

提交回复
热议问题