C#加密代码移植到Java

发布于 2024-12-09 05:53:05 字数 1778 浏览 0 评论 0原文

我正在尝试将以下代码从 C# 转换为 Java,但我得到了不同的键结果值。这是我的代码:

C#:

        plainText = "Hello World";
        passPhrase = "IhDyHz6bgQyS0Ff1/1s=";
        saltValue = "0A0Qvv09OXd3GsYHVrA=";
        hashAlgorithm = "SHA1";
        passwordIterations = 3;
        initVector = "GjrlRZ6INgNckBqv";
        keySize = 256;

        byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
        byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);

        byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

        PasswordDeriveBytes password = new PasswordDeriveBytes(
                                                        passPhrase,
                                                        saltValueBytes,
                                                        hashAlgorithm,
                                                        passwordIterations);

        byte[] keyBytes = password.GetBytes(keySize / 8);

Java

        byte[] password = PassPhrase.getBytes("ASCII");
        byte[] salt = PKCS5S1ParametersGenerator.PKCS5PasswordToBytes(SaltValue.toCharArray());

        PKCS5S1ParametersGenerator generator = new PasswordDeriveBytes(new SHA1Digest()); 
        generator.init(password, salt, PasswordIterations); 

        byte[] key = ((KeyParameter)generator.generateDerivedParameters(32)).getKey(); 

问题是,即使两个代码上提供了相同的参数,我也会得到不同的密钥:(只有前 4 个字节匹配)

注意: 该值已转换为 Base64:

C#: GWR/loJAuuiPvP0cuGGCVXErz16HjOZ7yQkCCHfBux4= Java: GWR/4oCT

如果我继续使用提供给generateDerivedParameters(32) 的数字,它只会给我更多字节,但仍然与.Net 上的结果不匹配。

我无法更改.Net 代码,因此任何建议都必须在Java 端完成。

提前致谢。

I'm trying to convert the following code from C# to Java, but I get different result values of keys. Here are my codes:

C#:

        plainText = "Hello World";
        passPhrase = "IhDyHz6bgQyS0Ff1/1s=";
        saltValue = "0A0Qvv09OXd3GsYHVrA=";
        hashAlgorithm = "SHA1";
        passwordIterations = 3;
        initVector = "GjrlRZ6INgNckBqv";
        keySize = 256;

        byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
        byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);

        byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

        PasswordDeriveBytes password = new PasswordDeriveBytes(
                                                        passPhrase,
                                                        saltValueBytes,
                                                        hashAlgorithm,
                                                        passwordIterations);

        byte[] keyBytes = password.GetBytes(keySize / 8);

Java

        byte[] password = PassPhrase.getBytes("ASCII");
        byte[] salt = PKCS5S1ParametersGenerator.PKCS5PasswordToBytes(SaltValue.toCharArray());

        PKCS5S1ParametersGenerator generator = new PasswordDeriveBytes(new SHA1Digest()); 
        generator.init(password, salt, PasswordIterations); 

        byte[] key = ((KeyParameter)generator.generateDerivedParameters(32)).getKey(); 

The issue is that I get different keys even with the same parameters provided on both codes: (only the first 4 bytes match)

NOTE: this values had been converted to Base64:

C#: GWR/loJAuuiPvP0cuGGCVXErz16HjOZ7yQkCCHfBux4=
Java: GWR/4oCT

If I keep playing with the number provided to generateDerivedParameters(32), it only gives me more bytes but still doesn't match the result at .Net.

I can't change the .Net code, so please any suggestion must be done at Java side.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

メ斷腸人バ 2024-12-16 05:53:05

generateDerivedParameters 方法表示:“keySize 我们想要的密钥的大小(以位为单位)”。所以你必须使用generateDerivedParameters(32 * 8)

The generateDerivedParameters method says: "keySize the size of the key we want (in bits)". So you have to use generateDerivedParameters(32 * 8)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文