2011-10-22 63 views
0
This is the code I am using for the encryption but it generate an error 

AESKeyForPassword方法中的「CCKeyDerivationPBKDF不可用」,雖然它是在實現前聲明的。如何解決它。如何解決文件不可用

 #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因此我把代碼直接在頂部的。

+0

包含'.h'頭文件的內容並不代表庫的實際可用性。另外,你確定你可以在同一個源代碼文件中簡單地混合使用C++和Objective-C嗎? –

+0

我是新來的iPhone,突然間我需要在我的項目中使用加密。我的項目是在Android和.Net中製作的,他們使用相同的方法,所以我必須使用這個anyhow.Xcode無法識別CommonCrypto/CommonKeyDerivation.h,所以我直接粘貼來自蘋果開源的內容。在AESKeyForPassword方法中,仍然會出現「CCKeyDerivationPBKDF不可用」的錯誤 – Soniya

回答

0

您僅僅爲CCKeyDerivationPBKDF和CCCalibratePBKDF聲明瞭2個原型。要麼將這些函數的完整代碼放在這個地方,要麼將它們聲明爲extern,並將它們放在單獨的模塊或庫中。

+0

這對我來說確實令人沮喪。我hv執行它anyhow.Initially CommonCrypto/CommonKeyDerivation.h ws那裏bt它ws顯示錯誤不可用我直接粘貼代碼仍顯示錯誤我在我的問題中提到。現在我添加CommonCrypto/CommonKeyDerivation.c文件仍然無法糾正它。我正在使用http://robnapier.net/blog/aes-commoncrypto-564,並有Xcode 3.2.6,並且無法使用蘋果公開源代碼中的CommonCrypto – Soniya

1

嘗試註釋掉該行:

__OSX_AVAILABLE_STARTING(__ MAC_10_7,__IPHONE_NA);

我認爲他們將方法限制到特定的操作系統,這正是您不需要的。 但我不能保證是否會出現更多問題。我試圖達到同樣的效果。

+0

它對我來說工作正常。 :) –

相關問題