以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  求助net代码转换成狐表代码  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=184524)

--  作者:tianhao0817
--  发布时间:2022/12/12 14:44:00
--  求助net代码转换成狐表代码
麻烦有点蓝老师把以下代码转换成狐表代码一下
public class AesGcm
{
    private static string ALGORITHM = "AES/GCM/NoPadding";
    private static int TAG_LENGTH_BIT = 128;
    private static int NONCE_LENGTH_BYTE = 12;
    private static string AES_KEY = "yourkeyhere";

    public static string AesGcmDecrypt(string associatedData, string nonce, string ciphertext)
    {
        GcmBlockCipher gcmBlockCipher = new GcmBlockCipher(new AesEngine());
        AeadParameters aeadParameters = new AeadParameters(
            new KeyParameter(Encoding.UTF8.GetBytes(AES_KEY)), 
            128, 
            Encoding.UTF8.GetBytes(nonce), 
            Encoding.UTF8.GetBytes(associatedData));
        gcmBlockCipher.Init(false, aeadParameters);

        byte[] data = Convert.FromBase64String(ciphertext);
        byte[] plaintext = new byte[gcmBlockCipher.GetOutputSize(data.Length)];
        int length = gcmBlockCipher.ProcessBytes(data, 0, data.Length, plaintext, 0);
        gcmBlockCipher.DoFinal(plaintext, length);
        return Encoding.UTF8.GetString(plaintext);
    }
}
[此贴子已经被作者于2022/12/12 14:44:51编辑过]

--  作者:有点蓝
--  发布时间:2022/12/12 14:47:00
--  
自行转换:https://converter.telerik.com/
--  作者:tianhao0817
--  发布时间:2022/12/12 14:55:00
--  
老师这段代码要怎么修改才可以在命令窗口测试啊

返回的值
{"id":"e8ba42b9-8043-5a62-839b-e063ad17a75b","create_time":"2022-12-12T13:56:46+08:00","resource_type":"encrypt-resource","event_type":"TRANSACTION.SUCCESS","summary":"支付成功","resource":{"original_type":"transaction","algorithm":"AEAD_AES_256_GCM","ciphertext":"v6xaWI1sVjUI3Ql/duXAPSG4Iaddy25XZILFlQ+bklu/b2Gv5cqrYQyJ1OSXRdUBmPQjfAfNvdS986pLPDQqAXuJc1hQtuLGFy2HyZwtU0YOA4W9c/F3AJ32moc5vXzNntMicIK6pm/Hp540itbsHxxVS+Gtcft2dfF2f/wl+JmURerVLiYWw2iIGvvYjlQTgOpNZCZJsWEnyx1u9z0lpbjfhG8jsuImnkR7gyrCiH8ltJkDrZK6tuePrzAoQLIe2m9foXVt7JYJryvLgVjzA4HahIjZxjN6F8BfM4UiAexiyWEfMtJKCZmr+oUkfcHi3TPi40MFbpkzBqr/fBe8mI3pRdGTnxJYZiO9Ggy2g4w3HOBboqsjpcXwJQb8SexEyKUN9nCutfVgC1WjuOOnaod0x8hmrsAqin57wYIZce711VQXiG0jnM7AwbHtMglmv7vT7QcUwYgmVgVNuE0qettRapFDpPjrQftfIQMUcnG0xtaoOM50a4J45o7tQnh1Ce1eSTZ5ij55UNHFOqoITSgCmw2c/1n2jyqjMi3b31lIKN8QJgc1lJgVYLzkOgCzxHqiSyhf5kICIUWrk/ve","associated_data":"transaction","nonce":"d2lO8NoGbAbF"}}

Public Class AesGcm
    Private Shared ALGORITHM As String = "AES/GCM/NoPadding"
    Private Shared TAG_LENGTH_BIT As Integer = 128
    Private Shared NONCE_LENGTH_BYTE As Integer = 12
    Private Shared AES_KEY As String = "yourkeyhere"

    Public Shared Function AesGcmDecrypt(ByVal associatedData As String, ByVal nonce As String, ByVal ciphertext As String) As String
        Dim gcmBlockCipher As GcmBlockCipher = New GcmBlockCipher(New AesEngine())
        Dim aeadParameters As AeadParameters = New AeadParameters(New KeyParameter(Encoding.UTF8.GetBytes(AES_KEY)), 128, Encoding.UTF8.GetBytes(nonce), Encoding.UTF8.GetBytes(associatedData))
        gcmBlockCipher.Init(False, aeadParameters)
        Dim data As Byte() = Convert.FromBase64String(ciphertext)
        Dim plaintext As Byte() = New Byte(gcmBlockCipher.GetOutputSize(data.Length) - 1) {}
        Dim length As Integer = gcmBlockCipher.ProcessBytes(data, 0, data.Length, plaintext, 0)
        gcmBlockCipher.DoFinal(plaintext, length)
        Return Encoding.UTF8.GetString(plaintext)
    End Function
End Class

[此贴子已经被作者于2022/12/12 14:56:06编辑过]

--  作者:有点蓝
--  发布时间:2022/12/12 15:37:00
--  
转换的代码放到全局代码,但是上面给的代码是不完整的,缺少命名空间,我在微软网站都搜索不到GcmBlockCipher这些类型的用法。咨询一下对方客服吧
--  作者:tianhao0817
--  发布时间:2022/12/12 19:42:00
--  
再麻烦老师看一下这段代码放到狐表里要怎么改 ,老师请看一下是不是要引用这两个DLL文件 
 下载信息  [文件大小:   下载次数: ]
点击浏览该文件:dll.zip



官方的文档这里有个坑,如果你直接用是会报错的

所以需要引用一个包



此主题相关图片如下:微信截图_20221212194212.png
按此在新窗口浏览图片


Imports Org.BouncyCastle.Crypto.Engines
Imports Org.BouncyCastle.Crypto.Modes
Imports Org.BouncyCastle.Crypto.Parameters
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks

Namespace test1.WeiXinPay.Tools
    Public Class AesGcmHelper
        Private Shared ALGORITHM As String = "AES/GCM/NoPadding"
        Private Shared TAG_LENGTH_BIT As Integer = 128
        Private Shared NONCE_LENGTH_BYTE As Integer = 12
        Private Shared AES_KEY As String = "12345678901234567890qwertyuiopas"

        Public Shared Function AesGcmDecrypt(ByVal associatedData As String, ByVal nonce As String, ByVal ciphertext As String, ByVal APIV3Key As String) As String
            Dim gcmBlockCipher As GcmBlockCipher = New GcmBlockCipher(New AesEngine())
            Dim aeadParameters As AeadParameters = New AeadParameters(New KeyParameter(Encoding.UTF8.GetBytes(APIV3Key)), 128, Encoding.UTF8.GetBytes(nonce), Encoding.UTF8.GetBytes(associatedData))
            gcmBlockCipher.Init(False, aeadParameters)
            Dim data As Byte() = Convert.FromBase64String(ciphertext)
            Dim plaintext As Byte() = New Byte(gcmBlockCipher.GetOutputSize(data.Length) - 1) {}
            Dim length As Integer = gcmBlockCipher.ProcessBytes(data, 0, data.Length, plaintext, 0)
            gcmBlockCipher.DoFinal(plaintext, length)
            Return Encoding.UTF8.GetString(plaintext)
        End Function
    End Class
End Namespace
[此贴子已经被作者于2022/12/12 19:48:29编辑过]

--  作者:有点蓝
--  发布时间:2022/12/12 20:20:00
--  
把BouncyCastle.Crypto.dll复制到Foxtable安装目录,然后添加引用

Namespace test1.WeiXinPay.Tools及上面的代码去掉,把Public Class AesGcmHelper以及里面的代码补全命名空间,比如:Dim gcmBlockCipher As Org.BouncyCastle.Crypto.Modes.GcmBlockCipher = New Org.BouncyCastle.Crypto.Modes.GcmBlockCipher(New AesEngine())

然后放到全局代码。命令窗口调用参考:

dim 结果 as string = AesGcmHelper.AesGcmDecrypt(传入需要的参数) 
msgbox(结果)

--  作者:tianhao0817
--  发布时间:2022/12/12 20:38:00
--  
老师,这个报错了,上面的三个我都试了一下

 Org.BouncyCastle.Crypto.Engines
Org.BouncyCastle.Crypto.Modes
Org.BouncyCastle.Crypto.Parameters


此主题相关图片如下:ddd.png
按此在新窗口浏览图片
[此贴子已经被作者于2022/12/12 20:39:16编辑过]

--  作者:有点蓝
--  发布时间:2022/12/12 21:45:00
--  
AesGcmHelper是自己定义的类名称,不需要加什么命名空间。用到别人的第三方的类型才需要加命名空间
[此贴子已经被作者于2022/12/12 21:46:16编辑过]

--  作者:tianhao0817
--  发布时间:2022/12/13 9:44:00
--  
老师我按照你说的方法,还是不可以

图片点击可在新窗口打开查看此主题相关图片如下:112233.png
图片点击可在新窗口打开查看

--  作者:有点蓝
--  发布时间:2022/12/13 9:53:00
--  
BouncyCastle.Crypto.dll这个dll要下载for .net4.0的版本,不要使用netstandard的版本