iOS加密:AES

AES:对称加密算法

AES是一种对称加密算法,加密和解密密钥为同一个,密钥的长度最少支持为128、192、256,分组长度128位。

我们项目中是AES加密完成之后再用Base64加密一下,所以解密的时候就需要先解密Base64,再进行AES解密,Base64这步并不是必须的。

关键代码

//定义加密密钥长度类型
typedef NS_ENUM(NSInteger, AESType) {
    AES128 = 0,
    AES192,
    AES256
};

//AES加密实现
+ (NSString *)aesEncrypt:(NSString *)string key:(NSString *)key {
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    NSData * result = [self aes:data key:key mode:kCCEncrypt type:AES128];
    if (result.length == 0) {return @"";}
    NSString *resultStr = nil;
    NSData * aesData = [NSData dataWithBytes:(const void *)result.bytes length:result.length];
    NSData *tmpData = [aesData base64EncodedDataWithOptions:0];
    resultStr = [[NSString alloc] initWithData:tmpData encoding:NSUTF8StringEncoding];;
    if (resultStr.length == 0) {return @"";}
    return resultStr;
}
//AES解密
+ (NSString *)aesDecrypt:(NSString *)string key:(NSString *)key {
    NSData *data = [[NSData alloc] initWithBase64EncodedString:string options:NSDataBase64DecodingIgnoreUnknownCharacters];
    NSData* result = [self aes:data key:key mode:kCCDecrypt type:AES128];
    NSString * resultStr = nil;
    if (result.length == 0) {return @"";}
    resultStr = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
    if (resultStr.length == 0) {return @"";}
    return resultStr;
}
//加解密算法实现
+ (NSData *)aes:(NSData *)data key:(NSString *)key mode:(CCOperation)operation type:(AESType)type {
    NSInteger size;
    switch (type) {
        case AES128:
            size = kCCKeySizeAES128;
            break;
        case AES192: {
            size = kCCKeySizeAES192;
        }
            break;
        case AES256: {
            size = kCCKeySizeAES256;
        }
            break;
        default:
            size = kCCKeySizeAES128;
            break;
    }
    char keyPtr[size + 1];
    bzero(keyPtr, sizeof(keyPtr));
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    NSUInteger dataLength = data.length;
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void * buffer = malloc(bufferSize);
    size_t numBytesDecrypted = 0;
    NSString * initIv = [self iv];
    char ivPtr[size + 1];
    memset(ivPtr, 0, sizeof(ivPtr));
    [initIv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
    NSData * result;
    CCCryptorStatus cryptStatus = CCCrypt(operation,
                                          kCCAlgorithmAES128,
                                          kCCOptionPKCS7Padding,
                                          keyPtr,
                                          size,
                                          ivPtr,
                                          data.bytes,
                                          dataLength,
                                          buffer,
                                          bufferSize,
                                          &numBytesDecrypted);
    if (cryptStatus == kCCSuccess) {
        result = [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
        return result;

    } else {
        free(buffer);
        return nil;
    }
}

//偏移量
+ (NSString *)iv {
    if (self.aesType == AES128) {
        return @"8841054029634287";
    }
    if (self.aesType == AES192) {
        return @"884105402963428788410540";
    }
    if (self.aesType == AES256) {
        return @"88410540296342878841054029634287";
    }
    return @"";
}

源码下载链接

你可能感兴趣的:(OC)