项目常用的加密工具类
#import <Foundation/Foundation.h>
@interface MD5Encrypt : NSObject
// MD5加密
/*
*由于MD5加密是不可逆的,多用来进行验证
*/
// 32位小写
+(NSString *)MD5ForLower32Bate:(NSString *)str;
// 32位大写
+(NSString *)MD5ForUpper32Bate:(NSString *)str;
// 16为大写
+(NSString *)MD5ForUpper16Bate:(NSString *)str;
// 16位小写
+(NSString *)MD5ForLower16Bate:(NSString *)str;
@end
#import "MD5Encrypt.h"
#import <CommonCrypto/CommonDigest.h>
@implementation MD5Encrypt
#pragma mark - 32位 小写
+(NSString *)MD5ForLower32Bate:(NSString *)str{
//要进行UTF8的转码
const char* input = [str UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(input, (CC_LONG)strlen(input), result);
NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
[digest appendFormat:@"%02x", result[i]];
}
return digest;
}
#pragma mark - 32位 大写
+(NSString *)MD5ForUpper32Bate:(NSString *)str{
//要进行UTF8的转码
const char* input = [str UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(input, (CC_LONG)strlen(input), result);
NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
[digest appendFormat:@"%02X", result[i]];
}
return digest;
}
#pragma mark - 16位 大写
+(NSString *)MD5ForUpper16Bate:(NSString *)str{
NSString *md5Str = [self MD5ForUpper32Bate:str];
NSString *string;
for (int i=0; i<24; i++) {
string=[md5Str substringWithRange:NSMakeRange(8, 16)];
}
return string;
}
#pragma mark - 16位 小写
+(NSString *)MD5ForLower16Bate:(NSString *)str{
NSString *md5Str = [self MD5ForLower32Bate:str];
NSString *string;
for (int i=0; i<24; i++) {
string=[md5Str substringWithRange:NSMakeRange(8, 16)];
}
return string;
}
@end
测试了啊啊啊啊啊啊!!!!!
NSString *str = @"123456";
NSString *ssss = [MD5Encrypt MD5ForUpper32Bate:str];
NSLog(@"%@",ssss);
2017-02-13 18:49:33.242 sway[7943:336086] E759BFEA12A9797E02E03E61450AFFEB
温馨提示
- MD5加密是不可逆的,所以iOS端加密后,把加密的字符串传给服务器,服务器直接存你发过去的32位或者16位的字符。服务器是解不了密的。如果反解密或者用于测试加密是否正确戳这里,不一定可以解密成功哦。