本文介绍了如何发送NSData作为CCCrypt的关键字和IV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要AES128使用密钥和IV到16字节的AES块对16字节的数据块进行加密。



我看过的每个CCCrypt示例都输入了(NSString *)键和IV。我的密钥和IV是NSData 16字节。我将NSData转换成十六进制字符串,但结果不正确。当我这样做时,我得到一个32字节的AES块。



所以我的问题是,我需要做什么才能将NSData作为const void *读取到CCCrypt?


$ b (NSData *)键iv:(NSString *)iv数据:(NSData *)theData
{
char keyPtr [kCCKeySizeAES128 + 1];
bzero(keyPtr,sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr)encoding:NSUTF8StringEncoding];

char ivPtr [kCCBlockSizeAES128 + 1];
bzero(ivPtr,sizeof(ivPtr));
if(iv){
[iv getCString:ivPtr maxLength:sizeof(ivPtr)encoding:NSUTF8StringEncoding];
}

NSUInteger dataLength = [theData length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void * buffer = malloc(bufferSize);

size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(operation,
kCCAlgorithmAES128,
kCCOptionECBMode,
keyPtr,
kCCBlockSizeAES128,
ivPtr,
[theData bytes]
dataLength,
buffer,
bufferSize,
& numBytesEncrypted);


if(cryptStatus == kCCSuccess){
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer);
return nil;
}


解决方案
  +(NSData *)AES128:(NSData *)dataIn 
操作:(CCOperation)操作// kCC加密,解密
键:(NSData *)键
选项:(CCOptions )选项// kCCOption PKCS7Padding,ECBMode,
iv:(NSData *)iv
错误:(NSError **)错误
{
CCCryptorStatus ccStatus = kCCSuccess;
size_t cryptBytes = 0;
NSMutableData * dataOut = [NSMutableData dataWithLength:dataIn.length + kCCBlockSizeAES128];

ccStatus = CCCrypt(operation,
kCCAlgorithmAES,
options,
key.bytes,key.length,
iv.bytes,
dataIn.length,dataIn.length,
dataOut.mutableBytes,dataOut.length,
& cryptBytes);

if(ccStatus == kCCSuccess){
dataOut.length = cryptBytes;
}
else {
if(error){
* error = [NSError errorWithDomain:@kEncryptionError
code:ccStatus
userInfo:nil] ;
}
dataOut = nil;
}

return dataOut;
}


I need to AES128 encrypt a 16 byte block of data using a key and an IV to a 16 byte AES block.

Every CCCrypt example I've seen takes in (NSString *) for key and IV. My Key and IV are NSData 16 bytes. I've converted the NSData into hex strings but the result is not correct. I get an AES block of 32 bytes when I do it that way.

So my question is, what do I have to do to get NSData to be read into CCCrypt as a const void * ?

- (NSData *)AES128Operation:(CCOperation)operation key:(NSString *)key iv:(NSString *)iv theData:(NSData *)theData
{
    char keyPtr[kCCKeySizeAES128 + 1];
    bzero(keyPtr, sizeof(keyPtr));
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    char ivPtr[kCCBlockSizeAES128 + 1];
    bzero(ivPtr, sizeof(ivPtr));
    if (iv) {
        [iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
    }

    NSUInteger dataLength = [theData length];
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);

    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(operation,
                                          kCCAlgorithmAES128,
                                          kCCOptionECBMode,
                                          keyPtr,
                                          kCCBlockSizeAES128,
                                          ivPtr,
                                          [theData bytes],
                                          dataLength,
                                          buffer,
                                          bufferSize,
                                          &numBytesEncrypted);


    if (cryptStatus == kCCSuccess) {
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }
    free(buffer);
    return nil;
}
解决方案
+ (NSData *)AES128:(NSData *)dataIn
         operation:(CCOperation)operation  // kCC Encrypt, Decrypt
               key:(NSData *)key
           options:(CCOptions)options      // kCCOption PKCS7Padding, ECBMode,
                iv:(NSData *)iv
             error:(NSError **)error
{
    CCCryptorStatus ccStatus   = kCCSuccess;
    size_t          cryptBytes = 0;
    NSMutableData  *dataOut    = [NSMutableData dataWithLength:dataIn.length + kCCBlockSizeAES128];

    ccStatus = CCCrypt( operation,
                       kCCAlgorithmAES,
                       options,
                       key.bytes, key.length,
                       iv.bytes,
                       dataIn.bytes, dataIn.length,
                       dataOut.mutableBytes, dataOut.length,
                       &cryptBytes);

    if (ccStatus == kCCSuccess) {
        dataOut.length = cryptBytes;
    }
    else {
        if (error) {
            *error = [NSError errorWithDomain:@"kEncryptionError"
                                         code:ccStatus
                                     userInfo:nil];
        }
        dataOut = nil;
    }

    return dataOut;
}

这篇关于如何发送NSData作为CCCrypt的关键字和IV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:45