常见的iOS代码加密常用加密方式包括Base64加密、MD5加密、AES加密、RSA加密等。
Base64加密
Base64加密特点:
- 数据加密之后,数据量会变大,变大1/3左右。
- 可进行反向解密。
- 编码后有个非常显著的特点,末尾有个=号。
加密过程:
+ (NSString*)base64EncodedStringWithData:(NSData*)data{
//判断是否传入需要加密数据参数
if((data ==nil) || (data ==NULL)) {
return nil; }
else if (![data isKindOfClass:[NSDataclass]]) {
return nil; }
//判断设备系统是否满足条件
if([[[UIDevicecurrentDevice] systemVersion] doubleValue] <=6.9) {
return nil; }
//使用系统的API进行Base64加密操作
NSDataBase64EncodingOptionsoptions;
options =NSDataBase64EncodingEndLineWithLineFeed;
return [data base64EncodedStringWithOptions:options];
}
解密过程:
+ (NSData*)base64DecodeDataWithString:(NSString*)string{
//判断是否传入需要加密数据参数
if((string ==nil) || (string ==NULL)) {
return nil; }
elseif(![string isKindOfClass:[NSStringclass]]) {
return nil; }
//判断设备系统是否满足条件
if([[[UIDevicecurrentDevice] systemVersion] doubleValue] <=6.9) {
return nil; }
//使用系统的API进行Base64解密操作
NSDataBase64DecodingOptionsoptions;
options =NSDataBase64DecodingIgnoreUnknownCharacters;
return [[NSData alloc] initWithBase64EncodedString:string options:options];}
MD5加密(MD5是一种摘要,而非加密,只是经常与加密配合使用)
//对字符串数据进行MD5的签名
+ (NSString *)md5SignWithString:(NSString *)string{
const char *object = [string UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(object,(CC_LONG)strlen(object),result);
NSMutableString *hash = [NSMutableString string];
for (int i = 0; i < 16; i ++) {
[hash appendFormat:@"%02X", result[i]];
}
return [hash lowercaseString];
}
//对二进制数据进行MD5的签名
+ (NSData *)md5SignWithData:(NSData *)data{
Byte byte[CC_MD5_DIGEST_LENGTH]; //定义一个字节数组来接收结果 CC_MD5((const void*)([data bytes]), (CC_LONG)[data length], byte);
return [NSData dataWithBytes:byte length:CC_MD5_DIGEST_LENGTH];}
因为蓝牙4.0芯片自带AES128加密算法,所以只要涉及到蓝牙开发都会使用AES128加密算法
加密方法:
+(NSMutableData*)AES_NEW_AESEn:(NSMutableData*)plain pass:(NSMutableData*)key
{
// NSLog(@"key是%@",key);
charkeyPtr[kCCKeySizeAES128+1];
memset(keyPtr,0,sizeof(keyPtr));
[keygetBytes:keyPtrlength:sizeof(keyPtr)];
//NSLog(@"加密的KEY是%s",keyPtr);
NSData* data = [[NSDataalloc]initWithData:plain];
NSUIntegerdataLength = [datalength];
size_tbufferSize = dataLength +kCCBlockSizeAES128;
void*buffer =malloc(bufferSize);
size_tnumBytesEncrypted =0;
CCCryptorStatuscryptStatus =CCCrypt(kCCEncrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding|kCCOptionECBMode,
keyPtr,
kCCBlockSizeAES128,
NULL,
[databytes],
dataLength,
buffer,
bufferSize,
&numBytesEncrypted);
if(cryptStatus ==kCCSuccess) {
NSMutableData*resultData = [NSMutableDatadataWithBytes:bufferlength:numBytesEncrypted];
returnresultData;
}
free(buffer);
returnnil;
}
解密方法:
+(NSMutableData*)AES_NEW_AESDecrypt:(NSMutableData*)encryptText password:(NSMutableData*)key
{
charkeyPtr[kCCKeySizeAES128+1];
memset(keyPtr,0,sizeof(keyPtr));
[keygetBytes:keyPtrlength:sizeof(keyPtr)];
NSData*data=[[NSDataalloc]initWithData:encryptText];
NSUIntegerdataLength = [datalength];
size_tbufferSize = dataLength +kCCBlockSizeAES128;
void*buffer =malloc(bufferSize);
size_tnumBytesCrypted =0;
CCCryptorStatuscryptStatus =CCCrypt(kCCDecrypt,
kCCAlgorithmAES128,
kCCOptionECBMode,
keyPtr,
kCCBlockSizeAES128,
NULL,
[databytes],
dataLength,
buffer,
bufferSize,
&numBytesCrypted);
if(cryptStatus ==kCCSuccess) {
NSMutableData*resultData = [NSMutableDatadataWithBytesNoCopy:bufferlength:numBytesCrypted];
returnresultData;
}
free(buffer);
returnnil;
}
解密的时候没带 kCCOptionPKCS7Padding 如果发现需要就加上
网友评论