gpt4 book ai didi

ios - 使用 iv 的 AES128 CTR 加密

转载 作者:行者123 更新时间:2023-12-01 18:37:56 25 4
gpt4 key购买 nike

我想用 iv 和 key 实现 AES128 CTR。我正在寻找如何以最佳方式做到这一点而不是重新发明轮子的任何建议。

我为这个 RNCryptor 找到了很好的库, 但看起来那里不支持此 aes。

我也测试了this方法,但看起来这不是点击率。

编辑


我使用了来自@zaph 的 zpproach

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

但收到 CCCryptorCreate 状态:-4305

刚刚在资源中找到

@constant kCCUnimplemented 函数未针对当前算法实现。

Link

最佳答案

您需要使用 CommonCrypto 但不是一次性 CCCrypt 版本,而是完整的 CCCryptorCreateWithModeCCCryptorUpdateCCCryptorFinal CCCryptorRelease 模式为 kCCModeCTR 版本。

这是我的示例代码,它可能不适合您的需求,您必须决定柜台 (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;
}

示例调用:

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

输出:加密:064e8073 76973eba 3192474f 9831db34

关于ios - 使用 iv 的 AES128 CTR 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48688272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com