iOS代码加密常用加密方式,常见的iOS代码加密常用加密方式算法包括MD5加密、AES加密、BASE64加密,三大算法iOS代码加密是如何进行加密的
MD5 iOS代码加密
创建MD5类
#import <Foundation/Foundation.h>
@interface CJMD5 : NSObject
+(NSString *)md5HexDigest:(NSString *)input;
@end
#import "CJMD5.h"
#import <CommonCrypto/CommonDigest.h>
@implementation CJMD5
+(NSString *)md5HexDigest:(NSString *)input{
const char* str = [input UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(str, strlen(str), result);
NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH];
for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
[ret appendFormat:@"%02X",result];
}
return ret;
}
@end
//MD5是不可逆的只有加密没有解密,iOS代码加密使用方式如下
NSString *userName = @"cerastes";
NSString *password = @"hello Word";
// MD5加密
NSString *md5 = [CJMD5 md5HexDigest:password];
NSLog(@"%@",md5);
AES加密iOS代码加密
//AES加密iOS代码加密使用方法
// AES加密
NSString *encryptedData = [AESCrypt encrypt:userName password:password];//加密
NSString *message = [AESCrypt decrypt:encryptedData password:password]; //解密
NSLog(@"加密结果 = %@",encryptedData);
NSLog(@"解密结果 = %@",message);
BASE64加密iOS代码加密:
#import <Foundation/Foundation.h>
@interface BASE64_Demo : NSObject
+ (NSString*)encodeBase64String:(NSString *)input;
+ (NSString*)decodeBase64String:(NSString *)input;
+ (NSString*)encodeBase64Data:(NSData *)data;
+ (NSString*)decodeBase64Data:(NSData *)data;
@end
#import "BASE64_Demo.h"
+ (NSString*)encodeBase64String:(NSString * )input {
NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
data = [GTMBase64 encodeData:data];
NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
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];
return base64String;
}
+ (NSString*)encodeBase64Data:(NSData *)data {
data = [GTMBase64 encodeData:data];
NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return base64String;
}
+ (NSString*)decodeBase64Data:(NSData *)data {
data = [GTMBase64 decodeData:data];
NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return base64String;
}
//BASE64加密iOS代码加密使用方法
// BASE64加密
NSString *baseEncodeString = [GTMBase64 encodeBase64String:password];
NSString *baseDecodeString = [GTMBase64 decodeBase64String:baseEncodeString];
NSLog(@"baseEncodeString = %@",baseEncodeString);
NSLog(@"baseDecodeString = %@",baseDecodeString);
1)本地数据加密
对NSUserDefaults,sqlite存储文件数据加密,保护帐号和关键信息。
2)URL编码加密
对程序中出现的URL进行编码加密,防止URL被静态分析
3)网络传输数据加密
对客户端传输数据提供加密方案,有效防止通过网络接口的拦截获取
4)方法体,方法名高级混淆
对应用程序的方法名和方法体进行混淆,保证源码被逆向后无法解析代码
5)程序结构混排加密
对应用程序逻辑结构进行打乱混排,保证源码可读性降到最低