iOS开发加密-iOS常用加密方法三种

2016-11-28  本文已影响330人  丶墨墨丶

看到网上有常用加密方法的总结,就转存了一下,应该对我们以后有帮助

常用的是MD5加密,这是一种单向加密,实现如下:

需要添加的类库:#import#import#import#include

MD5实现方法(16位):

+ (NSString *)md5:(NSString *)str

{

constchar *concat_str = [strUTF8String];

unsignedchar result[CC_MD5_DIGEST_LENGTH];

CC_MD5(concat_str, (unsignedint)strlen(concat_str), result);

NSMutableString *hash = [NSMutableStringstring];

for (int i =0; i <16; i++){

[hashappendFormat:@"%02X", result[i]];

}

return [hashuppercaseString];

}

还有就是base64的加密,这个加密在别的资源网站或是技术贴里面的描述的都一样,我们都需要去下载

提供使用的相关类,GTMBase64.zip库文件包,并解压,获得GTMBase64.h,GTMBase64.m和

GTMDefines.h三个文件。

下载地址是:https://github.com/r258833095/GTMBase64

新建一个base64的类,在base64.h中天假四个函数:

+ (NSString*)encodeBase64String:(NSString*)input;

+ (NSString*)decodeBase64String:(NSString*)input;

+ (NSString*)encodeBase64Data:(NSData*)data;

+ (NSString*)decodeBase64Data:(NSData*)data;

在base64.m文件中,实现上面4个函数:

+ (NSString*)encodeBase64String:(NSString* )input {

NSData*data = [inputdataUsingEncoding:NSUTF8StringEncodingallowLossyConversion:YES];

data = [GTMBase64encodeData:data];

NSString*base64String = [[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding] ;

returnbase64String;

}

+ (NSString*)decodeBase64String:(NSString* )input {

NSData*data = [inputdataUsingEncoding:NSUTF8StringEncodingallowLossyConversion:YES];

data = [GTMBase64decodeData:data];

NSString*base64String = [[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding] ;

returnbase64String

}

+ (NSString*)encodeBase64Data:(NSData*)data {

data = [GTMBase64encodeData:data];

NSString*base64String = [[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding] ;

returnbase64String;

}

+ (NSString*)decodeBase64Data:(NSData*)data {

data = [GTMBase64decodeData:data];

NSString*base64String = [[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding] ;

returnbase64String;

}

直接调用+ (NSString*)encodeBase64String:(NSString* )input和

+ (NSString*)decodeBase64String:(NSString* )input就可以进行加解密。

常用的AES加密,详解:

h文件内容:

#import

@class NSString;

@interface NSData (Encryption)

- (NSData *)AESOverCycle:(NSString *)key; //加密

- (NSData *)AESRelaes:(NSString *)key; //解密

@end

m文件内容:

加密:

@implementation NSData (Encryption)

- (NSData *)AESOverCycle:(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 *)AESRelaes:(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

上一篇下一篇

猜你喜欢

热点阅读