简体   繁体   English

使用iv进行AES128 CTR加密

[英]AES128 CTR encryption with iv

I want to implement AES128 CTR with iv and key. 我想用iv和key实现AES128点击率。 I'm looking for any advice how to do that in best way and not reinvent wheel. 我正在寻找任何建议,如何以最好的方式做到这一点,而不是重新发明轮子。

I found good lib for this RNCryptor , but looks like this aes is not supported there. 我找到了这个RNCryptor的好lib,但看起来这里不支持这个。

Also I test this approach, but looks like this is not CTR. 我也测试了这种方法,但看起来这不是CTR。

EDIT 编辑


I used zpproach from @zaph 我使用了@zaph的zpproach

NSData *result = [NSData cryptData:dataStr
                         operation:kCCEncrypt
                              mode:kCCModeCTR
                         algorithm:kCCAlgorithmAES128
                           padding:ccNoPadding
                         keyLength:kCCKeySizeAES128
                                iv:ivHex
                               key:keyHex
                             error:&error];

but receive CCCryptorCreate status: -4305 但收到CCCryptorCreate status: -4305

Just found in sources 刚刚找到消息来源

@constant kCCUnimplemented Function not implemented for the current algorithm.

Link 链接

You need to use CommonCrypto but not the one-shot CCCrypt version, but instead the full CCCryptorCreateWithMode , CCCryptorUpdate , CCCryptorFinal and CCCryptorRelease with mode kCCModeCTR version. 您需要使用CommonCrypto而不是一次性CCCrypt版本,而是使用模式kCCModeCTR版本的完整CCCryptorCreateWithModeCCCryptorUpdateCCCryptorFinalCCCryptorRelease

Here is sample code I have, it may not fit your needs and you will have to decide on the counter (IV) form. 以下是我的示例代码,它可能不符合您的需求,您必须决定计数器(IV)表单。

#import <CommonCrypto/CommonCrypto.h>

+ (NSData *)cryptData:(NSData *)dataIn
            operation:(CCOperation)operation  // kCC Encrypt, Decrypt
                 mode:(CCMode)mode            // kCCMode ECB, CBC, CFB, CTR, OFB, RC4, CFB8
            algorithm:(CCAlgorithm)algorithm  // CCAlgorithm AES DES, 3DES, CAST, RC4, RC2, Blowfish
              padding:(CCPadding)padding      // cc NoPadding, PKCS7Padding
            keyLength:(size_t)keyLength       // kCCKeySizeAES 128, 192, 256
                   iv:(NSData *)iv            // CBC, CFB, CFB8, OFB, CTR
                  key:(NSData *)key
                error:(NSError **)error
{
    if (key.length != keyLength) {
        NSLog(@"CCCryptorArgument key.length: %lu != keyLength: %zu", (unsigned long)key.length, keyLength);
        if (error) {
            *error = [NSError errorWithDomain:@"kArgumentError key length" code:key.length userInfo:nil];
        }
        return nil;
    }

    size_t dataOutMoved = 0;
    size_t dataOutMovedTotal = 0;
    CCCryptorStatus ccStatus = 0;
    CCCryptorRef cryptor = NULL;

    ccStatus = CCCryptorCreateWithMode(operation, mode, algorithm,
                                       padding,
                                       iv.bytes, key.bytes,
                                       keyLength,
                                       NULL, 0, 0, // tweak XTS mode, numRounds
                                       kCCModeOptionCTR_BE, // CCModeOptions
                                       &cryptor);

    if (cryptor == 0 || ccStatus != kCCSuccess) {
        NSLog(@"CCCryptorCreate status: %d", ccStatus);
        if (error) {
            *error = [NSError errorWithDomain:@"kCreateError" code:ccStatus userInfo:nil];
        }
        CCCryptorRelease(cryptor);
        return nil;
    }

    size_t dataOutLength = CCCryptorGetOutputLength(cryptor, dataIn.length, true);
    NSMutableData *dataOut = [NSMutableData dataWithLength:dataOutLength];
    char *dataOutPointer = (char *)dataOut.mutableBytes;

    ccStatus = CCCryptorUpdate(cryptor,
                               dataIn.bytes, dataIn.length,
                               dataOutPointer, dataOutLength,
                               &dataOutMoved);
    dataOutMovedTotal += dataOutMoved;

    if (ccStatus != kCCSuccess) {
        NSLog(@"CCCryptorUpdate status: %d", ccStatus);
        if (error) {
            *error = [NSError errorWithDomain:@"kUpdateError" code:ccStatus userInfo:nil];
        }
        CCCryptorRelease(cryptor);
        return nil;
    }

    ccStatus = CCCryptorFinal(cryptor,
                              dataOutPointer + dataOutMoved, dataOutLength - dataOutMoved,
                              &dataOutMoved);
    if (ccStatus != kCCSuccess) {
        NSLog(@"CCCryptorFinal status: %d", ccStatus);
        if (error) {
            *error = [NSError errorWithDomain:@"kFinalError" code:ccStatus userInfo:nil];
        }
        CCCryptorRelease(cryptor);
        return nil;
    }

    CCCryptorRelease(cryptor);

    dataOutMovedTotal += dataOutMoved;
    dataOut.length = dataOutMovedTotal;

    return dataOut;
}

Sample invocation: 示例调用:

NSData *dataIn  = [@"DataInDataInData" dataUsingEncoding: NSUTF8StringEncoding];
NSData *key     = [@"KeyKeyKeyKeyKeyK" dataUsingEncoding: NSUTF8StringEncoding];
NSData *counter = [@"CounterCounterCo" dataUsingEncoding: NSUTF8StringEncoding];
NSError *error;
NSData *encrpted = [Crypto
                    cryptData:dataIn
                    operation:kCCEncrypt
                    mode:kCCModeCTR
                    algorithm:kCCAlgorithmAES
                    padding:ccNoPadding
                    keyLength:kCCKeySizeAES128
                    iv:counter
                    key:key
                    error:&error];
NSLog(@"encrypted: %@", encrpted);

Output: encrypted: 064e8073 76973eba 3192474f 9831db34 输出: encrypted: 064e8073 76973eba 3192474f 9831db34

Perhaps you are looking for CryptoSwift library. 也许您正在寻找CryptoSwift库。 It supports AES and CTR. 它支持AES和CTR。

Another popular crypoto-library is libsodium, but I can't be sure it supports CTR. 另一个流行的crypoto库是libsodium,但我不能确定它支持CTR。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM