iOS开发(93)之常用加密方法(aes、md5、base64)
ios常用方法(aes、md5、base64)
1、aes加密
nsdata+aes.h文件
[plain] //
// nsdata-aes.h
// smile
//
// created by 周 敏 on 12-11-24.
// copyright (c) 2012年 box. all rights reserved.
//
#import <foundation/foundation.h>
@class nsstring;
@interface nsdata (encryption)
- (nsdata *)aes256encryptwithkey:(nsstring *)key; //加密
- (nsdata *)aes256decryptwithkey:(nsstring *)key; //解密
@end
nsdata+aes.m文件
[plain] //
// nsdata-aes.h
// smile
//
// created by 周 敏 on 12-11-24.
// copyright (c) 2012年 box. all rights reserved.
//
#import "nsdata+aes.h"
#import <commoncrypto/commoncryptor.h>
@implementation nsdata (encryption)
- (nsdata *)aes256encryptwithkey:(nsstring *)key {//加密
char keyptr[kcckeysizeaes256+1];
bzero(keyptr, sizeof(keyptr));
[key getcstring:keyptr maxlength:sizeof(keyptr) encoding:nsutf8stringencoding];
nsuinteger datalength = [self length];
size_t buffersize = datalength + kccblocksizeaes128;
void *buffer = malloc(buffersize);
size_t numbytesencrypted = 0;
cccryptorstatus cryptstatus = cccrypt(kccencrypt, kccalgorithmaes128,
kccoptionpkcs7padding | kccoptionecbmode,
keyptr, kccblocksizeaes128,
null,
[self bytes], datalength,
buffer, buffersize,
&numbytesencrypted);
if (cryptstatus == kccsuccess) {
return [nsdata datawithbytesnocopy:buffer length:numbytesencrypted];
}
free(buffer);
return nil;
}
- (nsdata *)aes256decryptwithkey:(nsstring *)key {//解密
char keyptr[kcckeysizeaes256+1];
bzero(keyptr, sizeof(keyptr));
[key getcstring:keyptr maxlength:sizeof(keyptr) encoding:nsutf8stringencoding];
nsuinteger datalength = [self length];
size_t buffersize = datalength + kccblocksizeaes128;
void *buffer = malloc(buffersize);
size_t numbytesdecrypted = 0;
cccryptorstatus cryptstatus = cccrypt(kccdecrypt, kccalgorithmaes128,
kccoptionpkcs7padding | kccoptionecbmode,
keyptr, kccblocksizeaes128,
null,
[self bytes], datalength,
buffer, buffersize,
&numbytesdecrypted);
if (cryptstatus == kccsuccess) {
return [nsdata datawithbytesnocopy:buffer length:numbytesdecrypted];
}
free(buffer);
return nil;
}
@end
这里aes在ios加过密以后以nsdata的形式存下来,如果想以nsstring形式存储,那么对nsdata进行base64位编码。
2、base64编码
首先下载gtmbase64文件,在工程中加入三个文件
gtmdefines.h
gtmbase64.h
gtmbase64.m
你可以在这里找到这三个文件
https://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/foundation/?r=87
你也可以在下面的demo里面找到这3个文件,demo会完整实现文章里面常用的3种编码方法。
我在此稍微封装一下:
.h文件
[plain] #pragma mark - base64
+ (nsstring*)encodebase64string:(nsstring *)input;
+ (nsstring*)decodebase64string:(nsstring *)input;
+ (nsstring*)encodebase64data:(nsdata *)data;
+ (nsstring*)decodebase64data:(nsdata *)data;
.m文件
[plain] #pragma mark - base64
+ (nsstring*)encodebase64string:(nsstring * )input {
nsdata *data = [input datausingencoding:nsutf8stringencoding allowlossyconversion:yes];
data = [gtmbase64 encodedata:data];
nsstring *base64string = [[[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding] autorelease];
return base64string;
}
+ (nsstring*)decodebase64string:(nsstring * )input {
nsdata *data = [input datausingencoding:nsutf8stringencoding allowlossyconversion:yes];
data = [gtmbase64 decodedata:data];
nsstring *base64string = [[[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding] autorelease];
return base64string;
}
+ (nsstring*)encodebase64data:(nsdata *)data {
data = [gtmbase64 encodedata:data];
nsstring *base64string = [[[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding] autorelease];
return base64string;
}
+ (nsstring*)decodebase64data:(nsdata *)data {
data = [gtmbase64 decodedata:data];
nsstring *base64string = [[[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding] autorelease];
return base64string;
}
3、md5加密
nsstring+md5.h文件
[plain] //
// nsstring+md5encrypt.h
// smile
//
// created by 周 敏 on 12-11-24.
// copyright (c) 2012年 box. all rights reserved.
//
#import <commoncrypto/commondigest.h>
@interface nsstring (md5)
- (nsstring *)md5encrypt;
@end
nsstring+md5.m文件
[plain] //
// nsstring+md5encrypt.h
// smile
//
// created by 周 敏 on 12-11-24.
// copyright (c) 2012年 box. all rights reserved.
//
#import "nsstring+md5.h"
@implementation nsstring (md5)
- (nsstring *)md5encrypt {
const char *original_str = [self utf8string];
unsigned char result[cc_md5_digest_length];
cc_md5(original_str, strlen(original_str), result);
nsmutablestring *hash = [nsmutablestring string];
for (int i = 0; i < 16; i++)
[hash appendformat:@"%02x", result[i]];
return [hash lowercasestring];
}
@end
上一篇: 事件的基本使用