c# - 使用 bouncycaSTLe 将加密/解密从 Java 转换为 C#

标签 c# bouncycastle

我正在尝试使用 Portable.BouncyCaSTLe 将几个函数从 Java 转换为 c#,虽然那里有很多示例,但我似乎无法找到符合我要求的一个,因为大多数示例看起来如此解释一种特定的加密/解密方法,而此功能似乎更通用。当然,我可能是错的,因为我对此完全是新手,并且在 BouncyCaSTLe、Java 或加密方面没有任何经验,所以请耐心等待。

java函数是:

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。 key 规范:

    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?

<强>2。规范参数:

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 时,它会将 AlgorithmParameterSpec spec 作为 new IvParameterSpec(iv) 传递,但对于 BouncyCaSTLe,它似乎是期待一把 key ?

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 时那样工作,当我创建 new ParametersWithIV,它需要一个 key ,根据我上面提供的测试示例,该 key 是使用 KeyParameter key = ParameterUtilities.CreateKeyParameter("AES", K); 创建的,所以技术上不会'尝试调用我的 Cipher 函数时不起作用,因为我将调用此函数。我应该将 spec 参数更改为 iv 并改为传递 byte[] 吗?

如果有混淆或解释不正确,我深表歉意,但正如我所说,我是新手,我试图在转换的同时更好地理解它。我希望其中的大部分内容是有意义的,并且您将能够提供帮助。

非常感谢。

PS:请注意,我还不能在 Java 中测试这些,但我希望在新的几天内正确设置一个环境,这将有望帮助测试 .net 和 java 之间的值。

更新 1

AES/CCM/NoPadding 传递给:

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);
 }

希望这对您有所帮助。

关于c# - 使用 bouncycaSTLe 将加密/解密从 Java 转换为 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37006546/

相关文章:

java - 将 "strong"JRE 策略文件与 BouncyCaSTLe 一起使用

java - BouncycaSTLe DER长度IO错误:

c# - 是否可以将 Func<T1,T2,TResult> 转换为 Func<T2,T1,TResult>

c# - 如何创建已在后端 HTML 表和 ASP.NET 中的 div 中创建的 onserverclick 事件

c# - 反射能解决什么问题?

c# - 弹跳城堡 C# : How to Implement EllipticCurve Encryption/Decryption

java - 使用 TLS PSK 加密时如何正确检测流结束?

c# - 是否可以编写自定义事件以便在后进先出的基础上调用事件处理程序?

c# - 如何在返回集合的 lambda 中使用异步

java - 使用 Java 中的 Bouncy CaSTLe 自签名 X509 证书