Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用目标c解密AES-iOS_Ios_Objective C_Xcode_Commoncrypto - Fatal编程技术网

用目标c解密AES-iOS

用目标c解密AES-iOS,ios,objective-c,xcode,commoncrypto,Ios,Objective C,Xcode,Commoncrypto,我计划在我的应用程序中实现AES加密,为此,我学习了Rob Napier提供的一个信息丰富的教程: 这是一次精彩的阅读,我能够使用以下方法加密一些字符串: 使用ROB NAPIER RNCRYPTOR类 NSString * const kRNCryptManagerErrorDomain = @"net.robnapier.RNCryptManager"; const CCAlgorithm kAlgorithm = kCCAlgorithmAES128; const N

我计划在我的应用程序中实现AES加密,为此,我学习了Rob Napier提供的一个信息丰富的教程:

这是一次精彩的阅读,我能够使用以下方法加密一些字符串:

使用ROB NAPIER RNCRYPTOR类

NSString * const
kRNCryptManagerErrorDomain = @"net.robnapier.RNCryptManager";

const CCAlgorithm kAlgorithm = kCCAlgorithmAES128;
const NSUInteger kAlgorithmKeySize = kCCKeySizeAES128;
const NSUInteger kAlgorithmBlockSize = kCCBlockSizeAES128;
const NSUInteger kAlgorithmIVSize = kCCBlockSizeAES128;
const NSUInteger kPBKDFSaltSize = 8;
const NSUInteger kPBKDFRounds = 10000;  // ~80ms on an iPhone 4

// ===================

+ (NSData *)encryptedDataForData:(NSData *)data
                        password:(NSString *)password
                              iv:(NSData **)iv
                            salt:(NSData **)salt
                           error:(NSError **)error {
  NSAssert(iv, @"IV must not be NULL");
  NSAssert(salt, @"salt must not be NULL");
  
  *iv = [self randomDataOfLength:kAlgorithmIVSize];
  *salt = [self randomDataOfLength:kPBKDFSaltSize];
  
  NSData *key = [self AESKeyForPassword:password salt:*salt];
  
  size_t outLength;
  NSMutableData *
  cipherData = [NSMutableData dataWithLength:data.length +
                kAlgorithmBlockSize];

  CCCryptorStatus
  result = CCCrypt(kCCEncrypt, // operation
                   kAlgorithm, // Algorithm
                   kCCOptionPKCS7Padding, // options
                   key.bytes, // key
                   key.length, // keylength
                   (*iv).bytes,// iv
                   data.bytes, // dataIn
                   data.length, // dataInLength,
                   cipherData.mutableBytes, // dataOut
                   cipherData.length, // dataOutAvailable
                   &outLength); // dataOutMoved

  if (result == kCCSuccess) {
    cipherData.length = outLength;
  }
  else {
    if (error) {
      *error = [NSError errorWithDomain:kRNCryptManagerErrorDomain
                                   code:result
                               userInfo:nil];
    }
    return nil;
  }
  
  return cipherData;
}

// ===================

+ (NSData *)randomDataOfLength:(size_t)length {
  NSMutableData *data = [NSMutableData dataWithLength:length];
  
  int result = SecRandomCopyBytes(kSecRandomDefault, 
                                  length,
                                  data.mutableBytes);
  NSAssert(result == 0, @"Unable to generate random bytes: %d",
           errno);
  
  return data;
}

// ===================

// Replace this with a 10,000 hash calls if you don't have CCKeyDerivationPBKDF
+ (NSData *)AESKeyForPassword:(NSString *)password 
                         salt:(NSData *)salt {
  NSMutableData *
  derivedKey = [NSMutableData dataWithLength:kAlgorithmKeySize];
  
  int 
  result = CCKeyDerivationPBKDF(kCCPBKDF2,            // algorithm
                                password.UTF8String,  // password
                                [password lengthOfBytesUsingEncoding:NSUTF8StringEncoding],  // passwordLength
                                salt.bytes,           // salt
                                salt.length,          // saltLen
                                kCCPRFHmacAlgSHA1,    // PRF
                                kPBKDFRounds,         // rounds
                                derivedKey.mutableBytes, // derivedKey
                                derivedKey.length); // derivedKeyLen
  
  // Do not log password here
  NSAssert(result == kCCSuccess,
           @"Unable to create AES key for password: %d", result);
  
  return derivedKey;
}
但是,在解密时,我无法正确解密,并且在场景中我将变为null:供您参考,解密代码为:

    + (NSData*)decryptData:(NSData*)data key:(NSData*)key error:(NSError **)error
{
    if (key.length != 16 && key.length != 24 && key.length != 32) {
        *error = [NSError errorWithDomain:@"keyLengthError" code:-1 userInfo:nil];
        return nil;
    }

    
    CCCryptorStatus ccStatus   = kCCSuccess;
    int             ivLength   = kCCBlockSizeAES128;
    size_t          clearBytes = 0;
    NSMutableData *dataOut     = [NSMutableData dataWithLength:data.length - ivLength];
    
    NSLog(@"Data Out String Decrypt%@", dataOut);

    ccStatus = CCCrypt(kCCDecrypt,
                       kCCAlgorithmAES,
                       kCCOptionPKCS7Padding,
                       key.bytes,
                       key.length,
                       data.bytes,
                       data.bytes + ivLength,
                       data.length - ivLength,
                       dataOut.mutableBytes,
                       dataOut.length,
                       &clearBytes);

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

    return dataOut;
}

在这种情况下,我错在哪里?几天来我一直在努力解决这个问题。有人能帮我吗?

你提到的例子中给出的方法是指。 只需使用您给定的密码、salt等进行测试。。它真的很管用
是的,明白了,您想在解密时抛出
密码:
iv:
以及
salt:
参数,并且只使用
密钥:
。你至少需要
iv:
才能做到这一点。但是,正如罗伯对你的另一个问题所说的那样,不要再发明轮子了

我上面链接的方法可以很好地处理解密参数。您的代码唯一的区别是
密码
iv
salt
被赋予解密功能

除了你想开发一种不用密码就能解密的东西之外,你还需要深入了解
cckeydrivationpbkdf()
(commonkeydrivation.h)的工作原理

编辑:当你要求有一种方法来打包和解包你的盐、iv和密码时,使用NSData非常简单

+ (NSData *)packWithSalt:(NSData*)salt IV:(NSData*)iv Cypher:(NSData*)tocypher {
    
    //adding Salt + IV + Cipher text
    NSMutableData *combi = [NSMutableData data];
    
    //[combi appendBytes:salt.bytes length:16];
    //[combi appendBytes:iv.bytes length:16]; //16
    //[combi appendBytes:tocypher.bytes length:tocypher.length];
    
    [combi appendData:salt];
    [combi appendData:iv];
    [combi appendData:tocypher];
    
    return combi;
}

+ (NSData*)cypherUnpackToSalt:(NSMutableData**)salt andIV:(NSMutableData**)iv fromPackData:(NSData*)pack {
    
    void *sBuff[16] = {};
    void *iBuff[16] = {};
    NSUInteger len = pack.length - 16 - 16; //keep length flexible
    void *pBuff = malloc(sizeof(Byte)*len); //needs dynamically size of buff
    [pack getBytes:sBuff range:NSMakeRange(0, 16)];
    [pack getBytes:iBuff range:NSMakeRange(16, 32)];
    [pack getBytes:pBuff range:NSMakeRange(32, len)];
    
    [(*salt) replaceBytesInRange:NSMakeRange(0, 16) withBytes:sBuff];
    [(*iv) replaceBytesInRange:NSMakeRange(0, 16) withBytes:iBuff];

    NSMutableData *unpack = [NSMutableData dataWithLength:len];
    [unpack replaceBytesInRange:NSMakeRange(0, len) withBytes:pBuff];
    free(pBuff);
    return unpack;
} 
集成这两种方法的加密和解密应该非常简单

概念证明:我们可以一起打包吗?然后再打开行李

NSData *salt = [CryptAES randomDataOfLength:16];
NSData *iv = [CryptAES randomDataOfLength:16];
NSData *chunk = [CryptAES packWithSalt:salt IV:iv Cypher:plaintextData];
NSLog(@"salt=%@ iv=%@ pack=%@ ",[salt base64EncodedStringWithOptions:0], [iv base64EncodedStringWithOptions:0], [chunk base64EncodedStringWithOptions:0] );
    
NSMutableData *unSalt = [NSMutableData dataWithLength:16];
NSMutableData *unIv = [NSMutableData dataWithLength:16];
NSData *unchunk = [CryptAES cypherUnpackToSalt:&unSalt andIV:&unIv fromPackData:chunk];
NSString *plainAgain = [[NSString alloc] initWithData:unchunk encoding:NSUTF8StringEncoding];
NSLog(@"salt=%@ iv=%@ unpack=%@",[unSalt base64EncodedStringWithOptions:0], [unIv base64EncodedStringWithOptions:0], plainAgain );
因此,解密方法仍然需要密码参数。
这并不完美,但因为你不应该把加密数据和密码一起扔掉——这应该没问题——你只需要在用户端处理密码。我的意思是,否则整个加密就没用了

是的,我删除了它,但你仍然可以帮助我正确的解密代码??这是我之前发布的问题,以防你需要更多的澄清,但我无法像IV。。。所以请给我一个合适的密码。。。这将是更有用的基本上,简而言之,我只想在(IV+密码文本)前面加上salt,并在
时使用它进行解密在实现端的代码中,那么任何结果都必须为零。是的,但我仍然没有得到正确的解密,它返回零。。。。我在寻找一个好的解密代码使用盐和IV。。。因此,如果你能帮助我,这将是非常有用的。谢谢,你能解释一下如何在不传递的情况下生成随机盐吗?Rob提到了如何将盐添加到数据中,就像添加IV一样。你能帮助我实现同样的效果吗?再次感谢你的解释,但是如果你看到我已经制作了一个动态IV,并且预先准备好了加密密钥。我希望以[0-16]Salt、[16-32]IV和[16-length]cypher文本格式预先编写动态Salt的方法与此基本相同。所以作为一个整体,我只需要像往常一样传递密钥和字符串…:)你的意思是,如果我把salt作为Nil传递,把IV作为Nil传递,代码会自动生成IV和salt,并将其预编码到加密密钥中?这很有趣。任何门都很容易敲。好吧,但为什么不。。你是对的