如何将CCKeyDerivationPBKDF转换为Xamarin.iOS?

时间:2013-08-21 12:45:41

标签: c# objective-c xamarin.ios cryptography pkcs#5

我有下面的ObjC方法,我想调用do将它移植到托管代码但不知道从哪里开始。有人可以帮忙吗?

- (NSData *)deriveKey
{
    NSData *passphrase = [self.passwordField.stringValue dataUsingEncoding:NSUTF8StringEncoding];
    NSData *salt = [self.saltField.stringValue dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableData *key = [NSMutableData dataWithLength:kCCKeySizeAES256];
    CCKeyDerivationPBKDF(kCCPBKDF2,
                         [passphrase bytes],
                         [passphrase length],
                         [salt bytes],
                         [salt length],
                         kCCPRFHmacAlgSHA256,
                         PBKDFNumberOfRounds,
                         [key mutableBytes],
                         [key length]);
    return key;
}

1 个答案:

答案 0 :(得分:1)

在.NET中,使用Rfc2898DeriveBytes可以获得PKCS#5v2(定义 PBKDF2 )支持。但是它不允许您选择哈希算法。

var salt = new byte [32]; // do not use it empty :)
var key = new Rfc2898DeriveBytes ("passphrase", salt, 1000).GetBytes (length);