iOS AES128加解密(AES-128-ECB)

在线加解密网址
http://www.seacha.com/tools/aes.html?src=wenhan123455678888050i33i206279278&mode=ECB&keylen=128&key=9ab7b2d96c018e7a&iv=9ab7b2d96c018e7a&bpkcs=pkcs7padding&session=SWkqdRzCEJoOtWBTKl6R&aes=1f074e036cd2b7d995649a1175a5d6f5&encoding=hex&type=0

本篇只讲(AES-128-ECB)的实现

//加密
+(NSString *)AES128Encrypt:(NSString *)plainText key:(NSString *)key  
{  
    char keyPtr[kCCKeySizeAES128+1];  
    memset(keyPtr, 0, sizeof(keyPtr));  
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];  
      
    NSData* data = [plainText dataUsingEncoding:NSUTF8StringEncoding];  
    NSUInteger dataLength = [data length];  
      
    size_t bufferSize = dataLength + kCCBlockSizeAES128;  
    void *buffer = malloc(bufferSize);  
    size_t numBytesEncrypted = 0;  
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,  
                                          kCCAlgorithmAES128,  
                                          kCCOptionPKCS7Padding|kCCOptionECBMode,  
                                          keyPtr,  
                                          kCCBlockSizeAES128,  
                                          NULL,  
                                          [data bytes],  
                                          dataLength,  
                                          buffer,  
                                          bufferSize,  
                                          &numBytesEncrypted);  
    if (cryptStatus == kCCSuccess) {  
        NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];  
        //return [GTMBase64 stringByEncodingData:resultData];  
        return [self hexStringFromData:resultData];  
  
    }  
    free(buffer);  
    return nil;  
}  
//解密
+(NSString *)AES128Decrypt:(NSString *)encryptText key:(NSString *)key  
{  
    char keyPtr[kCCKeySizeAES128 + 1];  
    memset(keyPtr, 0, sizeof(keyPtr));  
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];  
      
    //NSData *data = [GTMBase64 decodeData:[encryptText dataUsingEncoding:NSUTF8StringEncoding]];  
      
    NSData *data=[self dataForHexString:encryptText];  
      
    NSUInteger dataLength = [data length];  
    size_t bufferSize = dataLength + kCCBlockSizeAES128;  
    void *buffer = malloc(bufferSize);  
      
    size_t numBytesCrypted = 0;  
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,  
                                          kCCAlgorithmAES128,  
                                          kCCOptionPKCS7Padding|kCCOptionECBMode,  
                                          keyPtr,  
                                          kCCBlockSizeAES128,  
                                          NULL,  
                                          [data bytes],  
                                          dataLength,  
                                          buffer,  
                                          bufferSize,  
                                          &numBytesCrypted);  
    if (cryptStatus == kCCSuccess) {  
        NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted];  
        return [[[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding] autorelease];  
    }  
    free(buffer);  
    return nil;  
} 
// 普通字符串转换为十六进 
+ (NSString *)hexStringFromData:(NSData *)data {  
    Byte *bytes = (Byte *)[data bytes];  
    // 下面是Byte 转换为16进制。  
    NSString *hexStr = @"";  
    for(int i=0; i<[data length]; i++) {  
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i] & 0xff]; //16进制数  
        newHexStr = [newHexStr uppercaseString];  
          
        if([newHexStr length] == 1) {  
            newHexStr = [NSString stringWithFormat:@"0%@",newHexStr];  
        }  
          
        hexStr = [hexStr stringByAppendingString:newHexStr];  
          
    }  
    return hexStr;  
} 
//十六进制转Data  
//十六进制转Data
+ (NSData*)dataForHexString:(NSString*)hexString
{
    if (hexString == nil) {
        return nil;
    }
    const char* ch = [[hexString lowercaseString] cStringUsingEncoding:NSUTF8StringEncoding];
    NSMutableData* data = [NSMutableData data];
    while (*ch) {
        if (*ch == ' ') {
            continue;
        }
        char byte = 0;
        if ('0' <= *ch && *ch <= '9') {
            byte = *ch - '0';
        }else if ('a' <= *ch && *ch <= 'f') {
            byte = *ch - 'a' + 10;
        }else if ('A' <= *ch && *ch <= 'F') {
            byte = *ch - 'A' + 10;
        }
        ch++;
        byte = byte << 4;
        if (*ch) {
            if ('0' <= *ch && *ch <= '9') {
                byte += *ch - '0';
            } else if ('a' <= *ch && *ch <= 'f') {
                byte += *ch - 'a' + 10;
            }else if('A' <= *ch && *ch <= 'F'){
                byte += *ch - 'A' + 10;
            }
            ch++;
        }
        [data appendBytes:&byte length:1];
    }
    return data;
}

推荐阅读更多精彩内容

  • 1、不安全的随机数生成,在CSRF TOKEN生成、password reset token生成等,会造成toke...
    nightmare丿阅读 1,138评论 0 0
  • 分组密码每次只能处理加密固定长度的分组,但是我们加密的明文可能会超过分组密码处理的长度。这时便需要对所有分组进行迭...
    AlexMercer313阅读 226评论 0 0
  • 本文主要介绍移动端的加解密算法的分类、其优缺点特性及应用,帮助读者由浅入深地了解和选择加解密算法。文中会包含算法的...
    voyagelab阅读 6,136评论 5 29
  • 前言: 在我们开发中免不了和服务器做一些数据交互,在交互过程中走得都是http请求,这类请求不像https那样的安...
    吊儿郎当的小伙子阅读 1,397评论 0 5
  • 文|杭心 01 “2018年,朝着太阳成长,不卑不亢,清澈善良。”零点时,安安在朋友圈写下这句话。 前年,去年,今...
    杭心阅读 541评论 12 16