Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PBKDF2带HMACSHA1 Java到Swift 3_Java_Ios_Swift3 - Fatal编程技术网

PBKDF2带HMACSHA1 Java到Swift 3

PBKDF2带HMACSHA1 Java到Swift 3,java,ios,swift3,Java,Ios,Swift3,我想使用Java中相同的算法(PBKDF2WithHmacSHA1)在swift 3中散列密码 以下是Java代码: char[] passwordChars = password.toCharArray(); byte[] saltBytes = Constantes.SALT.getBytes(); PBEKeySpec spec = new PBEKeySpec(passwordChars, saltBytes, Constantes.ITERATIONS,192); try {

我想使用Java中相同的算法(PBKDF2WithHmacSHA1)在swift 3中散列密码

以下是Java代码:

char[] passwordChars = password.toCharArray();
byte[] saltBytes = Constantes.SALT.getBytes();
PBEKeySpec spec = new PBEKeySpec(passwordChars, saltBytes, Constantes.ITERATIONS,192);

try {
    SecretKeyFactory  key = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    byte[] hashedPassword = key.generateSecret(spec).getEncoded();   
    return String.format("%x", new BigInteger(hashedPassword));
} catch {[..]}
关键字“测试”的结果示例:2d21d1a136b22280a47499789ae4bedfb63ce900e97064

我试着像这样使用CryptoSwift:

let passwordArray: [UInt8] = Array(test.utf8)
let saltArray: [UInt8] = Array(salt.utf8)

let result = try! PKCS5.PBKDF2(password: passwordArray, salt: saltArray, iterations: iter, keyLength: 192, variant: .sha1).calculate()
结果:

B35A9B2A6150373B5CF81A7A616BC80F8CBE9EC25EAC9B111798FEB9E2FA9B1C0AA4627D0FB6C1820D2A5B432B1DD688A06692F3A8E2B2136D8C03F26D28DE49BDFE4ECB76821EE74139F2580361405B788EAB5D339A916EC13D96F8C812ACB84A8E927FAD7C9AE7B2136D8C03F28F26D28EC7DE49BDFE4ECB768EE74139F2580361405B788EAB818CF41414141414141418CFB738F758EAB738CFB1638B7B7B7B7B7B7B7B7B7B7B7B7B7B7B7B7B7B7B7B7B7B7B7B7B7B8e63cea96212574817426a1cd1d35dd2c22e1a

我注意到结果不一样

你知道问题可能来自哪里吗?

是的,我有解决办法 问题出在Android上:

SecretKeyFactory key = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        byte[] hashedPassword = key.generateSecret(spec).getEncoded();
        DebugLog.logd("Tools","hashPassword tableau bytes = " + hashedPassword);            
        return toHex(hashedPassword);
使用toHex():


您应该会找到与Swift相同的结果:-)

您得到答案了吗?我也有同样的问题是的,我发布了我的解决方案,很抱歉没有回答
private static String toHex(byte[] array) {
    BigInteger bi = new BigInteger(1, array);
    String hex = bi.toString(16);
    int paddingLength = (array.length * 2) - hex.length();
    if(paddingLength > 0)
        return String.format("%0" + paddingLength + "d", 0) + hex;
    else
        return hex;
}