IOS 加密(其中包括AES256、MD5)

时间:2022-09-28 18:33:46

前言:加密算法在IOS中是很普遍的,很多地方都可能用的到,其中AES256和MD5加密是比较常用的。

一、MD5加密

#import <Foundation/Foundation.h>

@interface NSString (MD5Encrypt)

- (NSString *)md5Encrypt;

@end

#import "NSString+MD5Encrypt.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (MD5Encrypt)

- (NSString *)md5Encrypt {
const char *original_str = [self UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(original_str, (unsigned int)strlen(original_str), result);
NSMutableString *hash = [NSMutableString string];
for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++){
[hash appendFormat:@"%02x", result[i]];
}
return [hash uppercaseString];
}

+ (NSString *)md5:(NSString *)input
{
const char *cStr = [input UTF8String];
unsigned char digest[16];
CC_MD5( cStr, (unsigned int)strlen(cStr), digest );
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", digest[i]];

return output;
}

@end


二、AES256加密及解密

1、对NSData加密解密

#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonCryptor.h>

@interface NSData(AES256)

- (NSData *)aes256_encrypt:(NSString *)key;
- (NSData *)aes256_decrypt:(NSString *)key;

@end

#import "NSData+AES256.h"

@implementation NSData(AES256)

- (NSData *)aes256_encrypt:(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 *)aes256_decrypt:(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

2、对NSString加密解密

#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonCryptor.h>

#import "NSData+AES256.h"

@interface NSString(AES256)

- (NSString *)aes256_encrypt:(NSString *)key;
- (NSString *)aes256_decrypt:(NSString *)key;

@end
#import "NSString +AES256.h"@implementation NSString(AES256)- (NSString *)aes256_encrypt:(NSString *)key{    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];    //对数据进行加密    NSData *result = [data aes256_encrypt:key];    //转换为2进制字符串    if (result && result.length > 0) {                Byte *datas = (Byte*)[result bytes];        NSMutableString *output = [NSMutableString stringWithCapacity:result.length * 2];                for(int i = 0; i < result.length; i++){            [output appendFormat:@"%02x", datas[i]];        }        return output;    }    return nil;}- (NSString *)aes256_decrypt:(NSString *)key{    //转换为2进制Data    NSMutableData *data = [NSMutableData dataWithCapacity:self.length / 2];    unsigned char whole_byte;    char byte_chars[3] = {'\0','\0','\0'};    int i;    for (i=0; i < [self length] / 2; i++) {        byte_chars[0] = [self characterAtIndex:i*2];        byte_chars[1] = [self characterAtIndex:i*2+1];        whole_byte = strtol(byte_chars, NULL, 16);        [data appendBytes:&whole_byte length:1];    }        //对数据进行解密    NSData* result = [data aes256_decrypt:key];    if (result && result.length > 0) {        return [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];    }    return nil;}@end