使用bouncycastle将加密/解密从Java转换为C#
我正在尝试使用Portable.BouncyCastle将几个函数从Java转换为c#,虽然有很多例子,但我似乎找不到一个符合我的要求的,因为大多数例子似乎解释了一种特定的加密/解密方法,而这个函数似乎更通用。当然,我可能是错的,因为我是一个完全的新手,在BouncyCastle、Java或加密方面都没有任何经验,所以请在这一点上与我保持一致 java函数是:使用bouncycastle将加密/解密从Java转换为C#,c#,bouncycastle,C#,Bouncycastle,我正在尝试使用Portable.BouncyCastle将几个函数从Java转换为c#,虽然有很多例子,但我似乎找不到一个符合我的要求的,因为大多数例子似乎解释了一种特定的加密/解密方法,而这个函数似乎更通用。当然,我可能是错的,因为我是一个完全的新手,在BouncyCastle、Java或加密方面都没有任何经验,所以请在这一点上与我保持一致 java函数是: public static byte[] Cipher(int mode, byte[] key, byte[] dat
public static byte[] Cipher(int mode, byte[] key,
byte[] data, string algorithm, AlgorithmParameterSpec spec)
{
Cipher cipher = Cipher.getInstance(algorithm);
SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
if (spec != null)
cipher.init(mode, keySpec, spec);
else
cipher.init(mode, keySpec);
return cipher.doFinal(data);
}
我从BouncyCasle中找到了一些代码,在那里我可以从我所看到的内容中匹配大部分功能:
byte[] K = Hex.Decode("404142434445464748494a4b4c4d4e4f");
byte[] N = Hex.Decode("10111213141516");
byte[] P = Hex.Decode("68656c6c6f20776f726c642121");
byte[] C = Hex.Decode("39264f148b54c456035de0a531c8344f46db12b388");
KeyParameter key = ParameterUtilities.CreateKeyParameter("AES", K);
IBufferedCipher inCipher = CipherUtilities.
GetCipher("AES/CCM/NoPadding");
inCipher.Init(true, new ParametersWithIV(key, N));
byte[] enc = inCipher.DoFinal(P);
1。SecretKeySpec:
SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
How do I create this using BC? Is that the equivalent of the SecretKeySpec:
KeyParameter key = ParameterUtilities.CreateKeyParameter("AES", K);
If it is, can I pass the "AES/CCM/NoPadding" instead of AES as it is done in Java?
It passes parameters of type IvParameterSpec to the Cypher function when called from `Java` via the `AlgorithmParameterSpec spec` parameter:
Cipher(ENCRYPT_MODE, key, clearBytes,
algorithm,
new IvParameterSpec(iv))
`BouncyCastle` does not have an overloaded function for `.Init` to allow me to pass the spec parameter as it does in `Java`, so how do I mimic this behaviour?
2。规格参数:
SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
How do I create this using BC? Is that the equivalent of the SecretKeySpec:
KeyParameter key = ParameterUtilities.CreateKeyParameter("AES", K);
If it is, can I pass the "AES/CCM/NoPadding" instead of AES as it is done in Java?
It passes parameters of type IvParameterSpec to the Cypher function when called from `Java` via the `AlgorithmParameterSpec spec` parameter:
Cipher(ENCRYPT_MODE, key, clearBytes,
algorithm,
new IvParameterSpec(iv))
`BouncyCastle` does not have an overloaded function for `.Init` to allow me to pass the spec parameter as it does in `Java`, so how do I mimic this behaviour?
3。IvParameterSpec:您可以看到,当从java调用cypher时,它将算法参数spec
作为新的IvParameterSpec(iv)
传递,但对于BouncyCastle,它似乎需要一个键
ParametersWithIV(key, N)
这种差异会对加密/解密产生任何影响吗
这是我当前试图“转换”此函数的尝试:
public static byte[] Cipher(bool isEncrypt, byte[] key, byte[] data,
string algorithm, ICipherParameters spec)
{
IBufferedCipher cipher = CipherUtilities.GetCipher(algorithm);
KeyParameter keySpec = ParameterUtilities.
CreateKeyParameter(algorithm, key);
cipher.Init(isEncrypt, new ParametersWithIV(keySpec,
keySpec.GetKey()));
return cipher.DoFinal(data);
}
如您所见,我已将spec参数更改为ICipherParameters spec
,但我不知道这是否与使用Bouncy时一样有效,看起来当我创建一个新参数swithiv
时,它需要一个键,并且来自我上面提供的测试样本,该键是使用KeyParameter key=ParameterUtilities.CreateKeyParameter(“AES”,K)创建的
因此,在尝试调用我的密码函数时,技术上不起作用,因为我将调用此函数。我是否应该将spec参数改为iv并传递一个字节[]
道歉,如果有混乱或事情没有正确解释,但正如我所说,我是新的,并试图更好地理解它,同时也转换。我希望大部分都是有意义的,你能帮上忙
非常感谢
PS:请注意,我还不能在Java中测试这些,但我希望在新的几天内正确设置一个环境,这将有助于测试.net和Java之间的值
更新1
将AES/CCM/NOP添加到
KeyParameter key = ParameterUtilities.CreateKeyParameter
抛出一个错误,因此这部分回答了我的一个问题。
BouncyCastle
中是否有一个函数来确定当通过AES/CCM/NoPadding
时所需的正确值,即AES
?最终使用了下面的代码,因为它看起来像预期的那样工作,但仍然很恼火,我不得不将AES
硬编码为IV参数部分的键。理想情况下,我希望这是基于我的算法。现在我有一个函数来加密和解密:
public static byte[] Cipher(bool forEncryption, byte[] key,
byte[] data, string algorithm, byte[] iv)
{
IBufferedCipher cipher = CipherUtilities.GetCipher(algorithm);
KeyParameter keySpec = ParameterUtilities.CreateKeyParameter("AES", key);
cipher.Init(forEncryption, new ParametersWithIV(keySpec, iv));
return cipher.DoFinal(data);
}
希望这有帮助