以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]请修改为Foxtable可用的方法  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=189578)

--  作者:nsdata
--  发布时间:2023/12/10 0:46:00
--  [求助]请修改为Foxtable可用的方法
  
  蓝老帮忙把下面加、解密的类改成Foxtable可以调用的全局方法,菜鸟的我把代码复制到foxtable报错不知道怎么解决

Imports System.Security.Cryptography
Imports System.Text
//加密
Public Class AESHelper
    Public Shared Function EncryptAES128(ByVal plainText As String, ByVal key As String) As String
        Dim result As String = ""

        Using aes As New AesManaged()
            aes.Mode = CipherMode.ECB
            aes.Padding = PaddingMode.PKCS7
            aes.Key = Encoding.UTF8.GetBytes(key)

            Using encryptor As ICryptoTransform = aes.CreateEncryptor(aes.Key, aes.IV)
                Using ms As New System.IO.MemoryStream()
                    Using cs As New CryptoStream(ms, encryptor, CryptoStreamMode.Write)
                        Dim plainBytes As Byte() = Encoding.UTF8.GetBytes(plainText)
                        cs.Write(plainBytes, 0, plainBytes.Length)
                        cs.FlushFinalBlock()

                        Dim encryptedBytes As Byte() = ms.ToArray()
                        result = Convert.ToBase64String(encryptedBytes)
                    End Using
                End Using
            End Using
        End Using

        Return result
    End Function
End Class



Imports System.Security.Cryptography
Imports System.Text
//解密
Public Class AESHelper
    Public Shared Function DecryptAES128(ByVal encryptedText As String, ByVal key As String) As String
        Dim result As String = ""

        Using aes As New AesManaged()
            aes.Mode = CipherMode.ECB
            aes.Padding = PaddingMode.PKCS7
            aes.Key = Encoding.UTF8.GetBytes(key)

            Using decryptor As ICryptoTransform = aes.CreateDecryptor(aes.Key, aes.IV)
                Dim encryptedBytes As Byte() = Convert.FromBase64String(encryptedText)

                Using ms As New System.IO.MemoryStream(encryptedBytes)
                    Using cs As New CryptoStream(ms, decryptor, CryptoStreamMode.Read)
                        Using reader As New System.IO.StreamReader(cs)
                            result = reader.ReadToEnd()
                        End Using
                    End Using
                End Using
            End Using
        End Using

        Return result
    End Function
End Class

--  作者:linyunu1
--  发布时间:2023/12/10 11:31:00
--  
去掉
Imports System.Security.Cryptography
Imports System.Text

然后下面代码补全命名空间 例如 Using aes As New System.Security.Cryptography.AesManaged()

--  作者:linyunu1
--  发布时间:2023/12/10 11:32:00
--  
加密和解密的代码并一起
--  作者:nsdata
--  发布时间:2023/12/10 23:47:00
--  
   试了也不行,能否上传个demo?

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






--  作者:z769036165
--  发布时间:2023/12/11 8:18:00
--  
AES128加密方法
Dim key As String = "12323123"
Dim data As String = "31231231214124123"
Dim encryptedBytes As Byte()
Dim aesAlg As New System.Security.Cryptography.AesCryptoServiceProvider()
aesAlg.Key = Encoding.ASCII.GetBytes(key)
aesAlg.Mode = System.Security.Cryptography.CipherMode.ECB
aesAlg.Padding = System.Security.Cryptography.PaddingMode.PKCS7   \'修改类型
Dim encryptor As System.Security.Cryptography.ICryptoTransform = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)
Dim memoryStream As New System.IO.MemoryStream()
Dim cryptoStream As New System.Security.Cryptography.CryptoStream(memoryStream, encryptor, System.Security.Cryptography.CryptoStreamMode.Write)
Dim dataBytes As Byte() = Encoding.ASCII.GetBytes(data)
cryptoStream.Write(dataBytes, 0, dataBytes.Length)
cryptoStream.FlushFinalBlock()
encryptedBytes = memoryStream.ToArray()
Output.Show(Convert.ToBase64String(encryptedBytes))



AES128解密方法

Dim key As String = "3123123"
Dim encryptedData As String = "PNOK3123AJs/9W3fVko19o="
Dim decryptedBytes As Byte()
Dim aesAlg As New System.Security.Cryptography.AesCryptoServiceProvider()
aesAlg.KeySize = 128
aesAlg.Key = Encoding.ASCII.GetBytes(key)
aesAlg.Mode = System.Security.Cryptography.CipherMode.ECB
aesAlg.Padding = System.Security.Cryptography.PaddingMode.PKCS7  \'修改类型

Dim decryptor As System.Security.Cryptography.ICryptoTransform = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)
Dim encryptedBytes As Byte() = Convert.FromBase64String(encryptedData)
Dim memoryStream As New System.IO.MemoryStream(encryptedBytes)
Dim cryptoStream As New System.Security.Cryptography.CryptoStream(memoryStream, decryptor, System.Security.Cryptography.CryptoStreamMode.Read)
Dim decryptedDataBytes As Byte() = New Byte(encryptedBytes.Length - 1) {}
Dim decryptedLength As Integer = cryptoStream.Read(decryptedDataBytes, 0, decryptedDataBytes.Length)
Dim unpaddedDataBytes As Byte() = New Byte(decryptedLength - 1) {}
Array.Copy(decryptedDataBytes, unpaddedDataBytes, decryptedLength)
decryptedBytes = unpaddedDataBytes
Output.Show(System.Text.ASCIIEncoding.ASCII.GetString(decryptedBytes))
[此贴子已经被作者于2023/12/11 8:18:51编辑过]

--  作者:nsdata
--  发布时间:2023/12/12 0:01:00
--  
  
  感谢大神!!