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

时间:2015-12-10 16:16:05

标签: c# encryption aes bouncycastle

我无法将此BouncyCastle java代码转换为c#版本。

特别是本节我遇到了转换问题:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim tNode As TreeNode
    Dim treeView As New TreeView
    Dim tNodeCollection As New TreeNodeCollection
    tNodeCollection = treeView.Nodes

    ' Code to generate and store within
    ' a System.Web.UI.WebControls.TreeView object
    ' ...
    ' ...
    ' ...

    repeater.DataSource = tNodeCollection          
    repeater.DataBind()
End Sub

Protected Sub loadTreeView(sender As Object, e As RepeaterItemEventArgs)
    Dim tNode As TreeNode
    Dim li As New HtmlGenericControl
    Dim ul As New HtmlGenericControl("ul")
    tNode = e.Item.DataItem

    If (tNode Is Nothing) Then
        Return
    End If

    li = e.Item.FindControl("listItem")
    li.ID = tNode.Value
    li.InnerHtml = tNode.Text

    If tNode.ChildNodes.Count > 0 Then
        li.Controls.Add(ul)
        searchChildNodes(tNode.ChildNodes, ul)
    End If
End Sub

Private Sub searchChildNodes(childNodes As TreeNodeCollection, ul As HtmlGenericControl)
    Dim tNode As TreeNode
    For Each tNode In childNodes
        Dim li As New HtmlGenericControl("li")
        li.ID = tNode.Value
        li.InnerHtml = tNode.Text
        ul.Controls.Add(li)
        If tNode.ChildNodes.Count > 0 Then
            Dim unorderedList As New HtmlGenericControl("ul")
            li.Controls.Add(unorderedList)
            searchChildNodes(tNode.ChildNodes, unorderedList)
        End If
    Next
End Sub

不确定新的SecretKeySpec(keyMaterial," AES")的c#版本是什么。 完整的Java源代码在这里:Java BouncyCastle source code trying to convert link

如果有人已经走这条路,感谢您的帮助。

{更新:}

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

         cipher.init(
                 Cipher.DECRYPT_MODE,
                 new SecretKeySpec(keyMaterial, "AES"), //C# version??
                 new IvParameterSpec(iv)
         );

Gcm AES C#BC版本将是:

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

也许其他人会发现此信息有用。

0 个答案:

没有答案