【问题标题】:How to resolve File not availabe如何解决文件不可用
【发布时间】:2011-10-22 09:47:29
【问题描述】:
This is the code I am using for the encryption but it generate an error 

“CCKeyDerivationPBKDF 不可用”在 AESKeyForPassword 方法中,虽然它是在实现之前声明的。如何解决。

     #ifndef _CC_PBKDF_H_
#define _CC_PBKDF_H_

#include <sys/types.h>
#include <sys/param.h>

#include <string.h>
#include <limits.h>
#include <stdlib.h>

#include <Availability.h>

#include <CommonCrypto/CommonDigest.h>
#include <CommonCrypto/CommonHMAC.h>


#ifdef __cplusplus
extern "C" {
#endif

    enum {
        kCCPBKDF2 = 2,
    };


    typedef uint32_t CCPBKDFAlgorithm;


    enum {
        kCCPRFHmacAlgSHA1 = 1, 
        kCCPRFHmacAlgSHA224 = 2,
        kCCPRFHmacAlgSHA256 = 3,
        kCCPRFHmacAlgSHA384 = 4,
        kCCPRFHmacAlgSHA512 = 5,
    };


    typedef uint32_t CCPseudoRandomAlgorithm;

    /*

     @function  CCKeyDerivationPBKDF
     @abstract  Derive a key from a text password/passphrase

     @param algorithm       Currently only PBKDF2 is available via kCCPBKDF2
     @param password        The text password used as input to the derivation 
     function.  The actual octets present in this string 
     will be used with no additional processing.  It's 
     extremely important that the same encoding and 
     normalization be used each time this routine is 
     called if the same key is  expected to be derived.
     @param passwordLen     The length of the text password in bytes.
     @param salt            The salt byte values used as input to the derivation 
     function.
     @param saltLen         The length of the salt in bytes.
     @param prf             The Pseudo Random Algorithm to use for the derivation 
     iterations.
     @param rounds          The number of rounds of the Pseudo Random Algorithm 
     to use.
     @param derivedKey      The resulting derived key produced by the function.  
     The space for this must be provided by the caller.
     @param derivedKeyLen   The expected length of the derived key in bytes.

     @discussion The following values are used to designate the PRF:

     * kCCPRFHmacAlgSHA1
     * kCCPRFHmacAlgSHA224
     * kCCPRFHmacAlgSHA256
     * kCCPRFHmacAlgSHA384
     * kCCPRFHmacAlgSHA512

     @result     kCCParamError can result from bad values for the password, salt, 
     and unwrapped key pointers as well as a bad value for the prf function.

     */

    int CCKeyDerivationPBKDF( CCPBKDFAlgorithm algorithm, const char *password, size_t passwordLen,
                             const uint8_t *salt, size_t saltLen,
                             CCPseudoRandomAlgorithm prf, uint rounds, 
                             uint8_t *derivedKey, size_t derivedKeyLen)
    __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_NA);

    /*
     * All lengths are in bytes - not bits.
     */

    /*

     @function  CCCalibratePBKDF
     @abstract  Determine the number of PRF rounds to use for a specific delay on 
     the current platform.  
     @param algorithm       Currently only PBKDF2 is available via kCCPBKDF2
     @param passwordLen     The length of the text password in bytes.
     @param saltLen         The length of the salt in bytes.
     @param prf             The Pseudo Random Algorithm to use for the derivation 
     iterations.
     @param derivedKeyLen   The expected length of the derived key in bytes.
     @param msec            The targetted duration we want to achieve for a key 
     derivation with these parameters.

     @result the number of iterations to use for the desired processing time.

     */

    uint CCCalibratePBKDF(CCPBKDFAlgorithm algorithm, size_t passwordLen, size_t saltLen,
                          CCPseudoRandomAlgorithm prf, size_t derivedKeyLen, uint32_t msec)
    __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_NA);

#ifdef __cplusplus
}
#endif

#endif  /* _CC_PBKDF_H_ */







#import "AESEncryption.h"
#import <CommonCrypto/CommonCryptor.h>
//#import <CommonCrypto/CommonKeyDerivation.h>
//#import <CommonKeyDerivation.h>

@implementation AESEncryption




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 = 1000;//0;  // ~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);
    NSLog(@"%d",result);
    NSAssert1(result == 0, @"Unable to generate random bytes: %d", errno);
    //NSAssert( @"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.length,  // passwordLength
                                  salt.bytes,           // salt
                                  salt.length,          // saltLen
                                  kCCPRFHmacAlgSHA1,    // PRF
                                  kPBKDFRounds,         // rounds
                                  derivedKey.mutableBytes, // derivedKey
                                  derivedKey.length); // derivedKeyLen
    NSLog(@"%d",result);
    // Do not log password here
    NSAssert1(result == kCCSuccess,@"Unable to create AES key for password: %d", result);
    //NSAssert(@"Unable to create AES key for password: %d", result);
    return derivedKey;
}
@end

上面实现的代码是 CommonCrypto/CommonKeyDerivation.h 的代码,在我的 xcode 中找不到,因此我将代码直接放在顶部。

【问题讨论】:

  • 包含.h 头文件的内容并不是让库实际可用的替代品。另外,您确定可以简单地将 C++ 和 Objective-C 混合在同一个源代码文件中吗?
  • 我是 iphone 新手,突然我需要在我的项目中使用加密。同样的项目是在 android 和 .Net 中制作的,它们使用相同的方法,所以无论如何我都必须使用它。Xcode 无法识别CommonCrypto/CommonKeyDerivation.h 所以我直接从苹果开源粘贴它的内容。而且它仍然在 AESKeyForPassword 方法中给出“CCKeyDerivationPBKDF 不可用”的错误

标签: iphone aes


【解决方案1】:

尝试注释掉这行:

__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_NA);

我认为他们将方法限制为特定的操作系统,而这正是您不需要的。 但我不能保证是否会出现进一步的问题。我正在努力实现同样的目标。

【讨论】:

    【解决方案2】:

    您只为 CCKeyDerivationPBKDF 和 CCCalibratePBKDF 声明了 2 个原型。要么将函数的完整代码放在此位置,要么将它们声明为 extern 并将它们放在单独的模块或库中。

    【讨论】:

    • 这真的让我很沮丧。我无论如何都要实现它。最初 CommonCrypto/CommonKeyDerivation.h 在那里显示错误不可用我直接粘贴代码仍然显示我在我的问题中提到的错误。现在我添加 CommonCrypto/CommonKeyDerivation.c 文件仍然无法纠正它。我正在使用robnapier.net/blog/aes-commoncrypto-564 并且拥有 Xcode 3.2.6 并且无法使用来自苹果开源的 CommonCrypto
    猜你喜欢
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 2011-06-16
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多