// // ViewController.m // 06 了解加密相关 // // Created by kun on 16/8/15. // Copyright © 2016年 kun. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"%@", [self base64EncodeString:@"A"]); NSLog(@"%@", [self base64AcodeString:[self base64EncodeString:@"A"]]); } // 对一个字符串进行base64编码,并且返回 - (NSString *)base64EncodeString:(NSString *)string { // 先转换为二进制数据 NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; // 对二进制数据进行base64编码,完成之后返回字符串 return [data base64EncodedStringWithOptions:0]; } - (NSString *)base64AcodeString:(NSString *)string { // 注意:该字符串是base64编码后的字符串 // 转换为二进制数据(完成了解码的过程) NSData *data = [[NSData alloc] initWithBase64EncodedString:string options:0]; // 把二进制数据在转换为字符串 return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
时间: 2024-10-09 12:08:15