Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
BouncyCastle-(AES/GCM/NoPadding)和c中的SecretKeySpec#_C#_Encryption_Aes_Bouncycastle - Fatal编程技术网

BouncyCastle-(AES/GCM/NoPadding)和c中的SecretKeySpec#

BouncyCastle-(AES/GCM/NoPadding)和c中的SecretKeySpec#,c#,encryption,aes,bouncycastle,C#,Encryption,Aes,Bouncycastle,我很难将这个BouncyCastle java代码转换成c版本 具体来说,我在转换此部分时遇到了问题: Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC"); cipher.init( Cipher.DECRYPT_MODE, new SecretKeySpec(keyMaterial, "AES"), //C# vers

我很难将这个BouncyCastle java代码转换成c版本

具体来说,我在转换此部分时遇到了问题:

         Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");

         cipher.init(
                 Cipher.DECRYPT_MODE,
                 new SecretKeySpec(keyMaterial, "AES"), //C# version??
                 new IvParameterSpec(iv)
         );
不确定新SecretKeySpec(keyMaterial,“AES”)的c#版本会是什么。 完整的java源代码如下:

如果有人已经走过这条路了,谢谢你的帮助

{更新:}

    public byte[] kdf(byte[] z, byte[] otherInfo) 
    {
        //Java BC
        //Digest digest = new SHA256Digest();
        //byte[] result = new byte[digest.getDigestSize()];
        //digest.update((byte)(1 >> 24));
        //digest.update((byte)(1 >> 16));
        //digest.update((byte)(1 >> 8));
        //digest.update((byte)1);
        //digest.update(z, 0, z.length);
        //digest.update(otherInfo, 0, otherInfo.length);
        //digest.doFinal(result, 0);
        //return result;

        // C# BC
        IDigest digest = new Sha256Digest();
        byte[] result = new byte[digest.GetDigestSize()];
        digest.Update((byte)(1 >> 24));
        digest.Update((byte)(1 >> 16));
        digest.Update((byte)(1 >> 8));
        digest.Update((byte)1);
        digest.BlockUpdate(z, 0, z.Length);
        digest.BlockUpdate(otherInfo, 0, otherInfo.Length);
        digest.DoFinal(result, 0);

        return result;
    }
Gcm AES C#BC版本将是:

        byte[] aesKeyData = kdf(sharedSecret, other);
        var cipher = new GcmBlockCipher(new AesFastEngine());
        KeyParameter keyParameter = ParameterUtilities.CreateKeyParameter("AES", aesKeyData);
        ICipherParameters cipherParameters = new ParametersWithIV(keyParameter, IV);
        cipher.Init(false, cipherParameters);

        byte[] output = new byte[cipher.GetOutputSize(ciphertext.Length)];
        int len = cipher.ProcessBytes(ciphertext, 0, ciphertext.Length, output, 0);
        try
        {
            cipher.DoFinal(output, len);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

也许其他人会觉得此信息很有用。

这将是您的AES密钥(字节)。谢谢Artjom,我需要更多信息。我的问题更多的是BC c#/java语法问题。特别是c#IDigest与keyMaterial等效物的外观。用更多信息和进度更新了问题。我不确定在Java版本中添加NOP,我现在得到[mac check in GCM failed],但我不确定是否存在输入数据问题。(1)您是否检查过
AESKYDATA
(C#)和
keyMaterial
(Java)是否包含相同的值?你检查过静脉注射是一样的吗?你检查过密文的长度是否相同吗?(2) 为GCM添加NOP是正确的。如果它有效,那么您可以提供问题的答案。我仍然不知道为什么会这样。这将是你的AES密钥(字节)。谢谢Artjom,我需要更多的信息。我的问题更多的是BC c#/java语法问题。特别是c#IDigest与keyMaterial等效物的外观。用更多信息和进度更新了问题。我不确定在Java版本中添加NOP,我现在得到[mac check in GCM failed],但我不确定是否存在输入数据问题。(1)您是否检查过
AESKYDATA
(C#)和
keyMaterial
(Java)是否包含相同的值?你检查过静脉注射是一样的吗?你检查过密文的长度是否相同吗?(2) 为GCM添加NOP是正确的。如果它有效,那么您可以提供问题的答案。我仍然不知道为什么会这样。