SHA256、AES256加密

    NSTimeZone * zone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone systemTimeZone]];

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];


    formatter.timeZone = zone;

    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString * locationString = [formatter stringFromDate:[NSDate date]];

    NSDateFormatter * formatter1 = [[NSDateFormatter alloc]init];

    [formatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSDate * date = [formatter dateFromString:locationString];

    NSTimeInterval time = [date timeIntervalSince1970];

    //yyyyMMdd

    NSString *timeStr = [Common timeWithTimeIntervalString:[[NSNumber numberWithLongLong:time] stringValue]];

    //时间 AES256的key键用SHA256加密

    NSString *sha256TimeStr = [SHA256 SHA256String:timeStr];

    //AES256加密

    NSString *aesStr = [SecurityUtil encryptAESData:txtPwd key:sha256TimeStr];




#import


@interface SHA256 : NSObject


//sha256加密方式 防止数据被篡改

+ (NSString *)SHA256String:(NSString *)srcString;


@end



#import "SHA256.h"

#import


@implementation SHA256


//sha256加密方式

+ (NSString *)SHA256String:(NSString *)srcString

{

    const char *cstr = [srcString cStringUsingEncoding:NSUTF8StringEncoding];

    NSData *data = [NSData dataWithBytes:cstr length:srcString.length];

    uint8_t digest[CC_SHA256_DIGEST_LENGTH];

    CC_SHA256(data.bytes, (CC_LONG)data.length, digest);

    NSMutableString* result = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];

    for (int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++) {

        [result appendFormat:@"%02x", digest[i]];

    }

    return result;

}


@end



#import


@class NSString;


@interface NSData (Encryption)


- (NSData *)AES128EncryptWithKey:(NSString *)key gIv:(NSString *)Iv;   //加密

- (NSData *)AES128DecryptWithKey:(NSString *)key gIv:(NSString *)Iv;   //解密


@end


#import "NSData+AES.h"

#import


@implementation NSData (Encryption)


//(key和iv向量这里是16位的) 这里是CBC加密模式,安全性更高


- (NSData *)AES128EncryptWithKey:(NSString *)key gIv:(NSString *)Iv{//加密

    char keyPtr[kCCKeySizeAES128+1];

    bzero(keyPtr, sizeof(keyPtr));

    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    

    char ivPtr[kCCKeySizeAES128+1];

    memset(ivPtr, 0, sizeof(ivPtr));

    [Iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];

    

    NSUInteger dataLength = [self length];

    size_t bufferSize = dataLength + kCCBlockSizeAES128;

    void *buffer = malloc(bufferSize);

    size_t numBytesEncrypted = 0;

    

    //以前加密的字节数组是写死的

    //Byte byte[] = {90, 74, 32, 114, -25, -16, 89, -24, -39, 41, -117, -115, 0, 54, -37, -124};

    

    //日期加密后的字节数据 服务器说太长 他不能解析 所以和安卓取加密后字节数组的前16位

    NSData* keyData = [key dataUsingEncoding:NSUTF8StringEncoding];

    Byte *keyByte = (Byte *)[keyData bytes];

    Byte byte[16];

    for(int i = 0; i < 16; i++) {

        byte[i] = keyByte[i];

        //printf("keyByte = %d\n",keyByte[i]);

    }

    

//    for(int i = 0; i < 16; i++) {

//        printf("byte[%d] = %d\n", i, byte[i]);

//    }

    

    NSData *salt = [NSData dataWithBytes:byte length:kCCKeySizeAES128];

    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,

                                          kCCAlgorithmAES128,

                                          kCCOptionPKCS7Padding,

                                          salt.bytes,

                                          kCCBlockSizeAES128,

                                          ivPtr,

                                          [self bytes],

                                          dataLength,

                                          buffer,

                                          bufferSize,

                                          &numBytesEncrypted);

    if (cryptStatus == kCCSuccess) {

        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];

    }

    free(buffer);

    return nil;

}



- (NSData *)AES128DecryptWithKey:(NSString *)key gIv:(NSString *)Iv{//解密

    char keyPtr[kCCKeySizeAES128+1];

    bzero(keyPtr, sizeof(keyPtr));

    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    

    char ivPtr[kCCKeySizeAES128+1];

    memset(ivPtr, 0, sizeof(ivPtr));

    [Iv getCString:ivPtr maxLength:sizeof(ivPtr) 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,

                                          keyPtr,

                                          kCCBlockSizeAES128,

                                          ivPtr,

                                          [self bytes],

                                          dataLength,

                                          buffer,

                                          bufferSize,

                                          &numBytesDecrypted);

    if (cryptStatus == kCCSuccess) {

        return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];

    }

    free(buffer);

    return nil;

}


#import


@interface SecurityUtil : NSObject 


#pragma mark - base64

+ (NSString*)encodeBase64String:(NSString *)input;

+ (NSString*)decodeBase64String:(NSString *)input;


+ (NSString*)encodeBase64Data:(NSData *)data;

+ (NSString*)decodeBase64Data:(NSData *)data;


#pragma mark - AES加密

//将string转成带密码的data

+ (NSString*)encryptAESData:(NSString*)string;

//将带密码的data转成string

+ (NSString*)decryptAESData:(NSString*)string;


//将string转成带密码的data 带key

+(NSString*)encryptAESData:(NSString*)string key:(NSString *)key;


@end



#import "SecurityUtil.h"

#import "GTMBase64.h"

#import "NSData+AES.h"


#define Iv          @"0392039203920300" //偏移量,可自行修改

#define KEY         @"smkldospdosldaaa" //key,可自行修改


@implementation SecurityUtil


#pragma mark - base64

+ (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;

}


#pragma mark - AES加密

//将string转成带密码的data

+(NSString*)encryptAESData:(NSString*)string

{

    //将nsstring转化为nsdata

    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];

    //使用密码对nsdata进行加密

    NSData *encryptedData = [data AES128EncryptWithKey:KEY gIv:Iv];

    //返回进行base64进行转码的加密字符串

    return [self encodeBase64Data:encryptedData];

}


//将string转成带密码的data 带key

+(NSString*)encryptAESData:(NSString*)string key:(NSString *)key

{

    //将nsstring转化为nsdata

    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];

    //使用密码对nsdata进行加密

    NSData *encryptedData = [data AES128EncryptWithKey:key gIv:Iv];

    //返回进行base64进行转码的加密字符串

    return [self encodeBase64Data:encryptedData];

}


#pragma mark - AES解密

//将带密码的data转成string

+(NSString*)decryptAESData:(NSString *)string

{

    //base64解密

    NSData *decodeBase64Data=[GTMBase64 decodeString:string];

    //使用密码对data进行解密

    NSData *decryData = [decodeBase64Data AES128DecryptWithKey:KEY gIv:Iv];

    //将解了密码的nsdata转化为nsstring

    NSString *str = [[NSString alloc] initWithData:decryData encoding:NSUTF8StringEncoding];

    return str;

}


@end


你可能感兴趣的:(学习)