看到網(wǎng)上有常用加密方法的總結(jié)袱饭,就轉(zhuǎn)存了一下挽铁,應(yīng)該對我們以后有幫助
常用的是MD5加密,這是一種單向加密雕蔽,實(shí)現(xiàn)如下:
需要添加的類庫:#import#import#import#include
MD5實(shí)現(xiàn)方法(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的加密诅岩,這個(gè)加密在別的資源網(wǎng)站或是技術(shù)貼里面的描述的都一樣讳苦,我們都需要去下載
提供使用的相關(guān)類,GTMBase64.zip庫文件包吩谦,并解壓鸳谜,獲得GTMBase64.h,GTMBase64.m和
GTMDefines.h三個(gè)文件。
下載地址是:https://github.com/r258833095/GTMBase64
新建一個(gè)base64的類逮京,在base64.h中天假四個(gè)函數(shù):
+ (NSString*)encodeBase64String:(NSString*)input;
+ (NSString*)decodeBase64String:(NSString*)input;
+ (NSString*)encodeBase64Data:(NSData*)data;
+ (NSString*)decodeBase64Data:(NSData*)data;
在base64.m文件中卿堂,實(shí)現(xiàn)上面4個(gè)函數(shù):
+ (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;
}
直接調(diào)用+ (NSString*)encodeBase64String:(NSString* )input和
+ (NSString*)decodeBase64String:(NSString* )input就可以進(jìn)行加解密。
常用的AES加密懒棉,詳解:
h文件內(nèi)容:
#import
@class NSString;
@interface NSData (Encryption)
- (NSData *)AESOverCycle:(NSString *)key; //加密
- (NSData *)AESRelaes:(NSString *)key; //解密
@end
m文件內(nèi)容:
加密:
@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