ios小项目之密码生成器-------生成密码部分

来源:互联网 发布:挖矿赚钱软件 编辑:程序博客网 时间:2024/05/20 04:29

前面已经讲了生成密码需要用到随机数随机种子函数和随机数生成函数,下面就总结一下在这个项目中是如何结合随机数来生成密码的。

需求:输入密码长度,选择密码特点(比如包含数字,字母),点击按钮生成相应位数的密码,并把密码对应的密码原文显示出来。

实现步骤:

1.设置总的密码库(组成密码的所有可能字符)和密码原文,并用字典把密码和密码原文对应起来。

2.根据密码特性把密码归类,比如数字类,字母类,方面后面根据用户选择的密码包含哪些特点来设置密码库。

3.根据选择特点来构建一个密码库(如密码只包含数字和字母,那么这个密码库就包含所有的数字和字母)。

4.获取密码:随机的从密码库中获取每一个密码,密码库是一个字符串,把随机数对字符串的个数取模,就得到字符串个数范围内的一个随机数n,取密码库(字符串)第n个字符就得到一个随机密码。这样依次按照密码长度来循环,就得到一个随机密码。

5.遍历生产的密码,根据键值对得到每个密码原文,组成字符串显示到视图上。

代码:

#import "MainViewController.h"#define RANDOM_SEED() srandom(time(NULL))#define RANDOM_INT(__MIN__,__MAX__) ((__MIN__) + random() % ((__MAX__+1) - (__MIN__)))@interface MainViewController ()@end@implementation MainViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {            }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    _passwordLength.delegate = self;    _password.delegate = self;        UIImage *buttonBackgrd = [[UIImage imageNamed:@"blueButton.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:12.0];//注意:查一下这个方法    [_creatPwButton setBackgroundImage:buttonBackgrd forState:UIControlStateNormal];    [_emailPwButton setBackgroundImage:buttonBackgrd forState:UIControlStateNormal];        [_creatPwButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];    [_emailPwButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];        UIImage *buttonBackgrdSel = [[UIImage imageNamed:@"whiteButton.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:12.0];    [_creatPwButton setBackgroundImage:buttonBackgrdSel forState:UIControlStateHighlighted];    [_creatPwButton setBackgroundImage:buttonBackgrdSel forState:UIControlStateSelected];    [_emailPwButton setBackgroundImage:buttonBackgrdSel forState:UIControlStateHighlighted];    [_emailPwButton setBackgroundImage:buttonBackgrdSel forState:UIControlStateSelected];        [_creatPwButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];    [_creatPwButton setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];    [_emailPwButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];    [_emailPwButton setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];    }- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    }- (void)dealloc {    [_passwordLength release];    [_phonetic release];    [_lowerCase release];    [_upperCase release];    [_number release];    [_punctuation release];    [_password release];    [_passwordPhonetic release];    [_creatPwButton release];    [_emailPwButton release];    [super dealloc];}- (IBAction)creatPassword:(id)sender {        [UIView beginAnimations:nil context:NULL];    [UIView setAnimationDuration:1];    [_password setAlpha:0.0];    [_passwordPhonetic setAlpha:0.0];    [_emailPwButton setAlpha:0.0];    [UIView commitAnimations];        [UIView beginAnimations:nil context:NULL];    [UIView setAnimationDuration:1];    [_password setAlpha:1.0];    [_passwordPhonetic setAlpha:1.0];    [_emailPwButton setAlpha:1.0];    [UIView commitAnimations];    NSArray *passwordKeys = [NSArray arrayWithObjects:                     @"a", @"b", @"c", @"d", @"e",                     @"f", @"g", @"h", @"i", @"j",                     @"k", @"l", @"m", @"n", @"o",                     @"p", @"q", @"r", @"s",@"t",                     @"u", @"v", @"w", @"x",@"y", @"z",                                          @"A", @"B", @"C", @"D", @"E",                     @"F", @"G", @"H", @"I", @"J",                     @"K", @"L", @"M", @"N", @"O",                     @"P", @"Q", @"R", @"S", @"T",                     @"U", @"V", @"W", @"X", @"Y", @"Z",                                          @"0", @"1", @"2", @"3", @"4",                     @"5", @"6", @"7", @"8", @"9",                                          @"~", @"!", @"?", @"#", @"%",                     @"^", @"&", @"*", @"(", @")",                      nil];NSArray *passwordObjects = [NSArray arrayWithObjects:                        @"alpha", @"bravo", @"charlie", @"delta", @"echo",                        @"foxtrot", @"golf", @"hotel", @"india", @"juliet",                        @"kilo", @"lima", @"mike", @"november", @"oscar",                        @"papa", @"quebec", @"romeo", @"sierra", @"tango",                        @"uniform", @"victor", @"whiskey", @"xray", @"yankee", @"zulu",                        @"ALPHA", @"BRAVO", @"CHARLIE", @"DELTA", @"ECHO",                        @"FOXTROT", @"GOLF", @"HOTEL", @"INDIA", @"JULIET",                        @"KILO", @"LIMA", @"MIKE", @"NOVEMBER", @"OSCAR",                        @"PAPA", @"QUEBEC", @"ROMEO", @"SIERRA", @"TANGO",                        @"UNIFORM", @"VICTOR", @"WHISKEY", @"XRAY", @"YANKEE", @"ZULU",                        @"Zero", @"One", @"Two", @"Three", @"Four",                         @"Five", @"Six", @"Seven", @"Eight", @"Nine",                        @"Tilda", @"Exclamation Point", @"Question Mark", @"Pound Sign", @"Percent Sign",                         @"Carrot Sign", @"Ampersand", @"Asterick", @"Left Parenthesis", @"Right Parenthesis",                        nil];    NSDictionary *passwordDic = [[NSDictionary alloc] initWithObjects:passwordObjects forKeys:passwordKeys];        NSString *lowercaseChars = @"abcdefghijklmnopqrstuvwxyz";    NSString *uppercaseChars = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";    NSString *numberChars = @"0123456789";    NSString *puntuationChars = @"~!?#%^&*()";        NSString *passwordLib = @"";        if (_lowerCase.on) {       passwordLib = [passwordLib stringByAppendingString:lowercaseChars];    }    if (_upperCase.on) {       passwordLib = [passwordLib stringByAppendingString:uppercaseChars];    }    if (_number.on) {       passwordLib = [passwordLib stringByAppendingString:numberChars];    }    if (_punctuation.on) {       passwordLib = [passwordLib stringByAppendingString:puntuationChars];    }    //    NSLog(@"%@",passwordLib);        NSInteger pswordlength = [_passwordLength.text intValue];    if (pswordlength == 0) {                UIAlertView *lengthAlert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"请设置密码长度" delegate:self cancelButtonTitle:@"立刻设置" otherButtonTitles: nil];        [lengthAlert show];        [lengthAlert release];                return;    }       // NSLog(@"wrong:%d",pswordlength);    if (!_lowerCase.on && !_upperCase.on && !_number.on && !_punctuation.on) {                UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"请设置密码" delegate:self cancelButtonTitle:@"设置" otherButtonTitles: nil];        [alerView show];        [alerView release];            return;    }    NSString *password = [self passwordWithLenth:pswordlength fromPasswordLibrary:passwordLib];    _password.text = password;        NSString *pswrdPhonetic = @"密码原文:";        if (password.length >0) {        NSString *phoneticKey = [password substringWithRange:NSMakeRange(0, 1)];        NSString *phoneticValue = [passwordDic objectForKey:phoneticKey];        pswrdPhonetic = [pswrdPhonetic stringByAppendingString:phoneticValue];                for (int i = 1; i<password.length; i++) {            phoneticKey = [password substringWithRange:NSMakeRange(i, 1)];            phoneticValue = [passwordDic objectForKey:phoneticKey];            pswrdPhonetic = [pswrdPhonetic stringByAppendingFormat:@",%@",phoneticValue];        }    }    _passwordPhonetic.text = pswrdPhonetic;    }//传入密码长度和密码库,生成密码- (NSString *)passwordWithLenth:(NSInteger)length fromPasswordLibrary:(NSString *)pswordLibrary {        RANDOM_SEED();    NSString *password = @"";    for (int i = 0; i<length; i++) {        int index = RANDOM_INT(0, pswordLibrary.length-1);        NSRange randge = NSMakeRange(index, 1);        NSString *word = [pswordLibrary substringWithRange:randge];        password = [password stringByAppendingFormat:@"%@",word];            }    return password;}#pragma mark - UITextField Delegate- (BOOL)textFieldShouldReturn:(UITextField *)textField {    if ([_passwordLength isFirstResponder]) {        [_passwordLength resignFirstResponder];    }else if ([_password isFirstResponder]) {        [_password resignFirstResponder];    }        return YES;}@end

运行效果:


0 0
原创粉丝点击