中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

iOS的AES加解密

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

   有時(shí)候項(xiàng)目可能要用到加解密,以此來(lái)保護(hù)用戶數(shù)據(jù)的安全性。下面我就來(lái)介紹AES的加解密。閑話不多說(shuō),直接上代碼。

    //  
    //  ViewController.m  
    //  test  
    //  
    //  Created by yons on 14-8-7.  
    //  Copyright (c) 2014年 yons. All rights reserved.  
    //  
      
    #import "ViewController.h"  
    #import "TableViewController.h"  
    #import "SecurityUtil.h"  
    #import "GTMBase64.h"  
      
    #define KEY @"ABCDEFGHIJKLMNOP" //key可修改  
      
    @interface ViewController ()  
    {  
        UIButton *encryption;  
        UIButton *decrypt;  
        UITextField *content;  
          
        UILabel *Before;  
        UILabel *after;  
        UILabel *key;  
    }  
      
    @end  
      
      
      
    @implementation ViewController  
      
    - (void)viewDidLoad  
    {  
        [super viewDidLoad];  
        // Do any additional setup after loading the view, typically from a nib.  
          
        self.view.backgroundColor = [UIColor whiteColor];  
          
        content = [[UITextField alloc] initWithFrame:CGRectMake(20, 60, 280, 40)];  
        content.backgroundColor = [UIColor whiteColor];  
        [self setBorder:content.layer];  
        content.placeholder = @" 請(qǐng)輸入加密或解密的字符串";  
         
        [self.view addSubview:content];  
          
         encryption = [[UIButton alloc] initWithFrame:CGRectMake(60, 125,80, 40)];  
        [encryption setTitle:@"加密" forState:UIControlStateNormal] ;  
        encryption.backgroundColor = [UIColor blackColor];  
        [encryption addTarget:self action:@selector(Encryption) forControlEvents:UIControlEventTouchUpInside];  
        [self.view addSubview:encryption];  
          
         decrypt = [[UIButton alloc] initWithFrame:CGRectMake(175, 125,80, 40)];  
        [decrypt setTitle:@"解密" forState:UIControlStateNormal] ;  
         decrypt.backgroundColor = [UIColor blackColor];  
        [decrypt addTarget:self action:@selector(Decrypt) forControlEvents:UIControlEventTouchUpInside];  
        [self.view addSubview:decrypt];  
          
        key = [[UILabel alloc] initWithFrame:CGRectMake(20, 190, 290, 20)];  
        Before = [[UILabel alloc] initWithFrame:CGRectMake(20, 220, 290, 40)];  
        Before.lineBreakMode = YES;  
        Before.numberOfLines = 0;  
         
        after = [[UILabel alloc] initWithFrame:CGRectMake(20, 270, 280, 40)];  
        after.lineBreakMode = YES;  
        after.numberOfLines = 0;  
          
        [key setFont:[UIFont fontWithName:@"Arial" size:14]];  
        [Before setFont:[UIFont fontWithName:@"Arial" size:14]];  
        [after setFont:[UIFont fontWithName:@"Arial" size:14]];  
          
        [self.view addSubview:key];  
        [self.view addSubview:Before];  
        [self.view addSubview:after];  
    }  
      
    // 加邊框  
    - (void) setBorder: (CALayer*) layer  
    {  
        [layer setMasksToBounds:YES];  
        [layer setCornerRadius:5.0]; //設(shè)置矩圓角半徑  
        [layer setBorderWidth:0.7];   //邊框?qū)挾? 
        [layer setBorderColor:[[UIColor lightGrayColor] CGColor]];  
    }  
      
    //加密  
    - (void) Encryption  
    {  
        if ([content.text isEqualToString:@""])  
        {  
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"溫馨提示!" message:@"親,你還沒(méi)有輸入任何內(nèi)容!" delegate:self cancelButtonTitle:@"確 定" otherButtonTitles:nil, nil nil];  
            [alert show];  
        }  
        else  
        {  
            NSString *string = [SecurityUtil encryptAESData:content.text app_key:KEY];  
            key.text = [NSString stringWithFormat:@"加密key:%@",KEY];  
            Before.text = [NSString stringWithFormat:@"加密前:%@",content.text];  
            after.text = [NSString stringWithFormat:@"加密后:%@",string];  
              
            NSLog(@"string:%@", string);  
        }  
         
    }  
      
    //解密  
    - (void) Decrypt  
    {  
        if ([content.text isEqualToString:@""])  
        {  
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"溫馨提示!" message:@"親,你還沒(méi)有輸入任何內(nèi)容!" delegate:self cancelButtonTitle:@"確 定" otherButtonTitles:nil, nil nil];  
            [alert show];  
        }  
        else  
        {  
            NSData *EncryptData = [GTMBase64 decodeString:content.text]; //解密前進(jìn)行GTMBase64編碼  
            NSString * string = [SecurityUtil decryptAESData:EncryptData app_key:KEY];  
              
            key.text = [NSString stringWithFormat:@"解密key:%@",KEY];  
            Before.text = [NSString stringWithFormat:@"解密前:%@",content.text];  
              
            if ([string isEqualToString:@""] | [string isEqualToString:nil]) {  
                string = @"解密失敗,親,請(qǐng)輸入加密后的字符串!";  
            }  
            after.text = [NSString stringWithFormat:@"解密后:%@",string];  
              
            NSLog(@"string:%@", string);  
        }  
    }  
      
      
    - (void)didReceiveMemoryWarning  
    {  
        [super didReceiveMemoryWarning];  
        // Dispose of any resources that can be recreated.  
    }  
      
    @end  

最后附上Demo的下載地址: AES加解密Demo(點(diǎn)擊下載)

來(lái)自:http://blog.csdn.net/by3g123/article/details/44617201

標(biāo)簽: isp 安全 代碼

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:Android 讀取assets文件下的txt文件

下一篇:一個(gè)數(shù)據(jù)庫(kù)操作PHP類