Foxtable(狐表)用户栏目专家坐堂 → 求助net代码转换成狐表代码


  共有5638人关注过本帖树形打印复制链接

主题:求助net代码转换成狐表代码

帅哥哟,离线,有人找我吗?
tianhao0817
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:122 积分:1290 威望:0 精华:0 注册:2020/5/16 10:30:00
求助net代码转换成狐表代码  发帖心情 Post By:2022/12/12 14:44:00 [显示全部帖子]

麻烦有点蓝老师把以下代码转换成狐表代码一下
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编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
tianhao0817
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:122 积分:1290 威望:0 精华:0 注册:2020/5/16 10:30:00
  发帖心情 Post By: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编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
tianhao0817
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:122 积分:1290 威望:0 精华:0 注册:2020/5/16 10:30:00
  发帖心情 Post By: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编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
tianhao0817
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:122 积分:1290 威望:0 精华:0 注册:2020/5/16 10:30:00
  发帖心情 Post By: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编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
tianhao0817
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:122 积分:1290 威望:0 精华:0 注册:2020/5/16 10:30:00
  发帖心情 Post By:2022/12/13 9:44:00 [显示全部帖子]

老师我按照你说的方法,还是不可以

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

 回到顶部
帅哥哟,离线,有人找我吗?
tianhao0817
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:122 积分:1290 威望:0 精华:0 注册:2020/5/16 10:30:00
  发帖心情 Post By:2022/12/13 10:19:00 [显示全部帖子]

谢谢老师,换了这个DLL文件可以了
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:bouncycastle.crypto.zip


 回到顶部