返回首页
当前位置: 主页 > 编程语言 > Perl教程 >

base64加密和解密实现代码

时间:2016-10-14 23:48来源:电脑教程学习网 www.etwiki.cn 编辑:admin

// ViewController.m
#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,copy) NSString *password;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

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

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

//对图片进行加密
[self test1];
//对图片进行解密
[self test2];
}

//加密
- (void)test1{

//1.获取需要加密文件的二进制数据

//网络地址
//NSData *data = [NSData dataWithContentsOfURL:@"http://xxx.xxx.xxx"];

//获取本地
NSData *data = [NSData dataWithContentsOfFile:@"/Users/xxx/Desktop/123.jpg"];

//2.获得加密之后的内容(二进制/字符串)
NSString *base64Str = [data base64EncodedStringWithOptions:0];

self.password = @"zxcbnmlkvjfidkxw";//将字符串拼接到加密文件中,提高加密的安全性
base64Str = [base64Str stringByAppendingString:self.password];

NSData *base64Data = [base64Str dataUsingEncoding:NSUTF8StringEncoding];

//保存加密之后的数据
[base64Data writeToFile:@"/Users/xxx/Desktop/加密文件" atomically:YES];
}

//解密
- (void)test2{
//1.获取加密之后的二进制数据
NSData *base64Data = [NSData dataWithContentsOfFile:@"/Users/xxx/Desktop/加密文件"];
//2.解析
NSData *data = [[NSData alloc] initWithBase64EncodedData:base64Data options:0];

[data writeToFile:@"/User/xxx/Desktop/123" atomically:YES];
}
@end

------分隔线----------------------------
标签(Tag):base64加密 base64解密
------分隔线----------------------------
推荐内容
猜你感兴趣