Encrypt in Objective C or Apple Swift Code with method PBKDF2WithHmacSHA1

1.4k views Asked by At

I come to you today after a long search for the solution.

I want to encrypt a word with a specific method.

This method has already been implemented in Java. I would get the same thing with the objective C language or swift.

I have already explored several methods. For example, I tried to run or RNCryptManager CPCryptController. But I get no conclusive results.

My java code is as follows:

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);
}

I intend to use https://developer.apple.com/library/mac/#samplecode/CryptoCompatibility/

The code I have now is

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");
}

However when I run this code, I get a null result. I do not see or can come to my mistake? What are the right solution to convert PBKDF2-HMAC-SHA1?

Thank you in advance.

1

There are 1 answers

2
Cy-4AH On

There right solution is:

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

I found this code in this article: http://robnapier.net/aes-commoncrypto