Monday, 26 August 2013

Encryption between .NET and Iphone

Encryption between .NET and Iphone

I am converting my .NET encryption code to Objective C. Here is my code in
.NET to create Key:
byte[] _corrpassword = Encoding.UTF8.GetBytes("1234567891123456");
PasswordDeriveBytes DerivedPassword = new
PasswordDeriveBytes(_corrpassword, salt, "SHA1",
_passwordIterations);
byte[] KeyBytes = DerivedPassword.GetBytes(_keySize / 8);
I am looking for equivelent of this into Objective C.
I tried creating this but both gives different results if I convert them
to base64 string:
-(NSData *)AESKeyForPassword:(NSString *)password salt:(NSData *)salt {
NSMutableData * derivedKey = [NSMutableData
dataWithLength:kCCKeySizeAES256];
int result = CCKeyDerivationPBKDF(kCCPBKDF2, // algorithm
password.UTF8String, // password
password.length, // passwordLength
salt.bytes, // salt
salt.length, // saltLen
kCCPRFHmacAlgSHA1, // PRF
2, // rounds
derivedKey.mutableBytes, // derivedKey
derivedKey.length); // derivedKeyLen
NSAssert(result == kCCSuccess,
@"Unable to create AES key for password: %d", result);
return derivedKey;
}
If I compare [derivedKey base64EncodingWithLineLength:0] in objective C
with string passKey = Convert.ToBase64String(KeyBytes); in .NET both gives
different results.

No comments:

Post a Comment