【问题标题】:PBKDF2 in Java with Bouncy Castle vs .NET Rfc2898DeriveBytes?Java 中的 PBKDF2 与 Bouncy Castle 对比 .NET Rfc2898DeriveBytes?
【发布时间】:2011-09-15 14:50:45
【问题描述】:

我有一些使用 PBKDF2 生成密钥的 C# 代码。

//byte[] salt = new RNGCryptoServiceProvider().GetBytes(salt);
byte[] salt = new byte[] { 19, 3, 248, 189, 144, 42, 57, 23 }; // for testing

byte[] bcKey = new Rfc2898DeriveBytes("mypassword", salt, 8192).GetBytes(32);

这很好用。我正在尝试使用 Bouncy Castle 在 Java 中实现相同的功能。无法让它工作(Java 缺少无符号类型的事实使它更加烦人)。

SecureRandom random = new SecureRandom();
byte[] salt = u2s(new int[] { 19, 3, 248, 189, 144, 42, 57, 23 });
//random.nextBytes(salt);

PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
generator.init(PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(("BLK" + password).toCharArray()), salt, keyTransformationRounds);
KeyParameter params = (KeyParameter)generator.generateDerivedParameters(keyLengthBits);
byte[] bcKey = params.getKey();
int[] bcKeyU = s2u(bcKey);
System.out.println(new String(Base64.encode(bcKey), "UTF-8"));

// Helper functions because Java has no unsigned types
//
// EDIT: THESE FUNCTIONS ARE INCORRECT.
// See my answer below for the correct versions.
//
static byte[] u2s(int[] unsignedArray) throws IOException
{
    byte[] signedArray = new byte[unsignedArray.length];
    for (int i = 0; i < signedArray.length; i++)
    {
        if (unsignedArray[i] < 0 || unsignedArray[i] > 255)
        {
            throw new IOException("unsignedArray at " + i + " was not within the range 0 to 255.");
        }

        signedArray[i] = (byte)(unsignedArray[i] - 128);
    }

    return signedArray;
}

static int[] s2u(byte[] signedArray)
{
    int[] unsignedArray = new int[signedArray.length];
    for (int i = 0; i < unsignedArray.length; i++)
    {
        unsignedArray[i] = (int)(signedArray[i] + 128);
    }

    return unsignedArray;
}

生成的 bcKey 字节数组不同。我究竟做错了什么?我是要正确处理从无符号到有符号的转换,还是不能按我预期的那样工作?

【问题讨论】:

    标签: java aes bouncycastle unsigned pbkdf2


    【解决方案1】:

    我没有正确处理有符号/无符号转换。下面是一些帮助函数,演示整数数组(表示无符号字节数组)和有符号字节数组之间的转换。

    检查intsToBytes 中 0-255 范围之外的整数不是必需的,但可能有助于调试。

    static byte[] intsToBytes(int[] ints)
    {
        byte[] bytes = new byte[ints.length];
        for (int i = 0; i < ints.length; i++)
        {
            if (ints[i] < 0 || ints[i] > 255) System.out.println(String.format("WARNING: ints at index %1$d (%2$d) was not a valid byte value (0-255)", i, ints[i]));
    
            bytes[i] = (byte)ints[i];
        }
    
        return bytes;
    }
    
    static int[] bytesToInts(byte[] bytes)
    {
        int[] ints = new int[bytes.length];
        for (int i = 0; i < bytes.length; i++)
        {
            ints[i] = bytes[i] & 0xff;
        }
    
        return ints;
    }
    

    【讨论】:

    • 不要做所有这些,只需将其转换为字节。 signedArray[i] = (byte)unsignedArray[i]
    • 它不会只是截断超出范围的数字吗?
    • 是的,但这几乎总是您想要的行为。实际上,它只是将低位 8 位从源复制到目标。
    • 你是对的......我在想 (byte)129 == 0 而不是 -127。反过来也可以缩短为 & 0xff。我已经修改了我的答案;谢谢你的建议。 :)
    猜你喜欢
    • 2021-08-03
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    相关资源
    最近更新 更多