iOS的AES加解密

wdfd 9年前發布 | 1K 次閱讀 Objective-C IOS

   有時候項目可能要用到加解密,以此來保護用戶數據的安全性。下面我就來介紹AES的加解密。閑話不多說,直接上代碼。

    //
// 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 = @" 請輸入加密或解密的字符串";  

    [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]; //設置矩圓角半徑  
    [layer setBorderWidth:0.7];   //邊框寬度  
    [layer setBorderColor:[[UIColor lightGrayColor] CGColor]];  
}  

//加密  
- (void) Encryption  
{  
    if ([content.text isEqualToString:@""])  
    {  
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"溫馨提示!" message:@"親,你還沒有輸入任何內容!" 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:@"親,你還沒有輸入任何內容!" delegate:self cancelButtonTitle:@"確 定" otherButtonTitles:nil, nil nil];  
        [alert show];  
    }  
    else  
    {  
        NSData *EncryptData = [GTMBase64 decodeString:content.text]; //解密前進行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 = @"解密失敗,親,請輸入加密后的字符串!";  
        }  
        after.text = [NSString stringWithFormat:@"解密后:%@",string];  

        NSLog(@"string:%@", string);  
    }  
}  


- (void)didReceiveMemoryWarning  
{  
    [super didReceiveMemoryWarning];  
    // Dispose of any resources that can be recreated.  
}  

@end  </pre> 


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

來自:http://blog.csdn.net/by3g123/article/details/44617201

 本文由用戶 wdfd 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!