C#加密代码移植到Java
我正在尝试将以下代码从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
generateDerivedParameters
方法表示:“keySize 我们想要的密钥的大小(以位为单位)”。所以你必须使用generateDerivedParameters(32 * 8)
The
generateDerivedParameters
method says: "keySize the size of the key we want (in bits)". So you have to usegenerateDerivedParameters(32 * 8)