美文网首页
iOS中AES加解密

iOS中AES加解密

作者: zgwyvd | 来源:发表于2020-04-05 10:38 被阅读0次

    由于最近在做蓝牙项目,和固件程序通讯需要使用到AES加解密,特此记录下具体过程。
    加密方法如下:

    +(NSString *)AES128Encrypt:(NSString *)plainText key:(NSString *)key{
        NSData *keyData = [self dataForHexString:key];
        Byte *aKey = (Byte *)[keyData bytes];
        
        NSData *data = [plainText dataUsingEncoding:NSUTF8StringEncoding];
        char dataPtr[kCCKeySizeAES128];
        memcpy(dataPtr, [data bytes], [data length]);
        for(int i = 0; i < 8; i++){
            dataPtr[i + 8] = 0x00;
        }
        data = [NSData dataWithBytes:dataPtr length:kCCKeySizeAES128];
        
        unsigned char result[kCCKeySizeAES128];
        bzero(result, kCCKeySizeAES128);
        size_t numBytesCrypted = 0;
        CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
                                              kCCAlgorithmAES128,
                                              kCCOptionECBMode,
                                              aKey,
                                              kCCKeySizeAES128,
                                              NULL,
                                              data.bytes,
                                              16,
                                              result,
                                              sizeof(result),
                                              &numBytesCrypted);
        if(cryptStatus == kCCSuccess){
            NSData *ret = [NSData dataWithBytes:result length:numBytesCrypted];
            return [self hexStringFromData:ret];
        }
        return nil;
    }
    

    解密方法如下:

    +(NSString *)AES128Decrypt:(NSString *)encryptText key:(NSString *)key{
        NSData *keyData = [self dataForHexString:key];
        Byte *aKey = (Byte *)[keyData bytes];
        
        NSData *data = [self dataForHexString:encryptText];
        data = [NSData dataWithBytes:dataPtr length:kCCKeySizeAES128];
        */
        
        unsigned char result[kCCKeySizeAES128];
        bzero(result, sizeof(result));
        size_t numBytesCrypted = 0;
        CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
                                              kCCAlgorithmAES128,
                                              kCCOptionECBMode,
                                              aKey,
                                              kCCKeySizeAES128,
                                              NULL,
                                              data.bytes,
                                              16,
                                              result,
                                              sizeof(result),
                                              &numBytesCrypted);
        if(cryptStatus == kCCSuccess){
            NSData *ret = [NSData dataWithBytes:result length:numBytesCrypted];
            NSString *string = [self hexStringFromData:ret];
            return [self stringFromHexString:string];
        }
        return nil;
    }
    

    相关方法如下:

    + (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;
    }
    
    // 普通字符串转换为十六进制
    + (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;
    }
    

    相关文章

      网友评论

          本文标题:iOS中AES加解密

          本文链接:https://www.haomeiwen.com/subject/rugyphtx.html