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;
}

你可能感兴趣的:(iOS AES128加解密(AES-128-ECB))