2014-11-17 104 views
0

經過漫長的搜索解決方案後,我今天就來找你。使用方法PBKDF2WithHmacSHA1在目標C或Apple Swift代碼中加密

我想用特定的方法加密一個單詞。

該方法已經在Java中實現。我會用客觀的C語言或迅速獲得同樣的東西。

我已經探索了幾種方法。例如,我嘗試運行或RNCryptManager CPCryptController。但我沒有得出確鑿的結果。

我的Java代碼如下:

public String encrypt(String texte) { 
    byte[] bytePassword = Base64.decode(PASSWORD, Base64.DEFAULT); 
    byte[] byteSalt = Base64.decode(SALT, Base64.DEFAULT); 
    byte[] bytesIv = Base64.decode(IV, Base64.DEFAULT); 
    SecretKeyFactory factory = null; 
    factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); 
    KeySpec spec = new PBEKeySpec(PASSWORD.toCharArray(), byteSalt, NB_ITER_RFC, 128); 
    SecretKey temp = null; 
    temp = factory.generateSecret(spec); 
    byte[] clef = temp.getEncoded(); 
    Cipher c = Cipher.getInstance("DESede/CBC/PKCS5Padding"); 
    IvParameterSpec ivParam = new IvParameterSpec(bytesIv); 
    c.init(Cipher.ENCRYPT_MODE, temp, ivParam);  
    byte[] encrypted = c.doFinal(texte.getBytes("UTF-8")); 
    mdp = Base64.encodeToString(encrypted, Base64.DEFAULT); 
    Log.i("MDP CHIFFRE", " = " + mdp); 
} 

我打算用https://developer.apple.com/library/mac/#samplecode/CryptoCompatibility/

我現在的代碼是

QCCPBKDF2SHA1KeyDerivation * op; 
NSString * passwordString; 
NSData * saltData; 
NSData * expectedKeyData; 

passwordString = @ "Hello Cruel World!"; 
saltData = [@ "Some salt sir?" dataUsingEncoding: NSUTF8StringEncoding]; 
expectedKeyData = [QHex dataWithHexString: @ "e56c27f5eed251db50a3"]; 

op = [[QCCPBKDF2SHA1KeyDerivation alloc] initWithPasswordString: passwordString saltData: saltData] 

op.rounds = 1000; 
op.derivedKeyLength = 10; 

[[ToolCommon sharedInstance] synchronouslyRunOperation: op]; 

if (nil == op.error) { 
    NSString * newStr = [[NSString alloc] initWithData: op.derivedKeyData encoding: NSUTF8StringEncoding] 
    NSLog (@ "This is it:% @", newStr); 
} Else { 
    NSLog (@ "Error"); 
} 

然而,當我運行此代碼,我得到一個空結果。 我沒有看到或可能來我的錯誤?什麼是轉換PBKDF2-HMAC-SHA1的正確解決方案?

預先感謝您。

+0

您提供Objective-C代碼密鑰導出,但缺少的是解密代碼。 – zaph

+0

是的,但你有什麼想法,爲什麼它需要我空?我應該去哪個方向? – MichelRobico

+0

查看@ Cy-4AH爲執行「PBKDF2」所做的回答。使用Common Crypto,它是內置的。 – zaph

回答

-1

有正確的解決方案是:

+ (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; 
} 

我發現這個代碼在這篇文章中: http://robnapier.net/aes-commoncrypto

+2

P.S. PBKDF2-HMAC-SHA1不加密,它是密碼加強方法。使用AES256進行加密。 –

+0

我必須做一個客觀的方法,返回與java方法相同的結果。 – MichelRobico