Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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
Sage Pay Forms V3.00 AES-128加密VB.Net_Vb.net_Aes_Opayo - Fatal编程技术网

Sage Pay Forms V3.00 AES-128加密VB.Net

Sage Pay Forms V3.00 AES-128加密VB.Net,vb.net,aes,opayo,Vb.net,Aes,Opayo,我想我会发布这篇文章,因为我没有找到V3.00升级所需的AES加密的现成解决方案 就我所见,出于某种原因,SagePay C#解决方案示例中没有加密/解密代码示例 我从现有文章和RijndaelManaged类VB示例()中拼凑了代码 导入System.Security.Cryptography 公共共享函数AESECRYPTEncryption(ByVal strCrypt作为字符串)作为字符串 Dim keyAndIvBytes为[Byte]()=UTF8Encoding.UTF8.GetB

我想我会发布这篇文章,因为我没有找到V3.00升级所需的AES加密的现成解决方案

就我所见,出于某种原因,SagePay C#解决方案示例中没有加密/解密代码示例

我从现有文章和RijndaelManaged类VB示例()中拼凑了代码

导入System.Security.Cryptography
公共共享函数AESECRYPTEncryption(ByVal strCrypt作为字符串)作为字符串
Dim keyAndIvBytes为[Byte]()=UTF8Encoding.UTF8.GetBytes(加密密码)
'创建RijndaelManaged的新实例
“阶级。这将生成一个新的密钥和初始化
"向量(IV)。
将AES用作新的RijndaelManaged()
'设置键的模式、填充和块大小
AES.Padding=PaddingMode.PKCS7
AES.Mode=CipherMode.CBC
AES.KeySize=128
AES.BlockSize=128
'将字符串加密为字节数组。
Dim加密为Byte()=EncryptStringToBytes(strCrypt、keyAndIvBytes、keyAndIvBytes)
AESEncryption=“@”和BitConverter.ToString(加密)。替换(“-”,“”)。ToUpper
终端使用
端函数
作为字符串的公共共享函数解密(ByVal strCrypt作为字符串)
Dim keyAndIvBytes为[Byte]()=UTF8Encoding.UTF8.GetBytes(加密密码)
'创建RijndaelManaged的新实例
“阶级。这将生成一个新的密钥和初始化
"向量(IV)。
将AES用作新的RijndaelManaged()
'设置键的模式、填充和块大小
AES.Padding=PaddingMode.PKCS7
AES.Mode=CipherMode.CBC
AES.KeySize=128
AES.BlockSize=128
Dim encryptedData As Byte()=StringToByteArray(strCrypt.Remove(0,1))
Dim往返字符串=DecryptStringFromBytes(encryptedData、keyAndIvBytes、keyAndIvBytes)
AES解密=往返
终端使用
端函数
共享函数ByteArrayTohextString(ByVal ba作为Byte())作为字符串
返回BitConverter.ToString(ba)。替换(“-”,“”)
端函数
共享函数StringToByteArray(ByVal十六进制作为字符串)作为字节()
返回可枚举的.Range(0,十六进制长度)。其中(函数(x)x Mod 2=0)。[选择](函数(x)Convert.ToByte(十六进制子字符串(x,2),16)).ToArray()
端函数
共享函数EncryptStringToBytes(ByVal明文作为字符串,ByVal Key()作为字节,ByVal IV()作为字节)作为字节()
'检查参数。

如果纯文本不算什么,或者是LSE PLAINTEXTEXT.Length也许我在这里很傻,但是如果你引用SagePay.IntegrationKit.DotNet dll,你应该可以访问他们的Crytography类


至少我是这么做的;添加.dll作为引用,将其作为文件顶部导入,然后使用Cryptography.decodeandecryptography&Cryptography.EncryptAndEncode

也许我在这里有点傻,但如果你引用SagePay.IntegrationKit.DotNet dll,你应该可以访问他们的Crytography类

至少我是这么做的;添加.dll作为引用,将其作为文件顶部导入,然后使用Cryptography.decodeandecryptography&Cryptography.EncryptAndEncode

是V3.00升级所需的SagePay C#AES加密的好线程。

是V3.00升级所需的SagePay C#AES加密的好线程

Imports System.Security.Cryptography

Public Shared Function AESEncryption(ByVal strCrypt As String) As String
        Dim keyAndIvBytes As [Byte]() = UTF8Encoding.UTF8.GetBytes(strEncryptionPassword)

        ' Create a new instance of the RijndaelManaged 
        ' class.  This generates a new key and initialization  
        ' vector (IV). 
        Using AES As New RijndaelManaged()
            ' Set the mode, padding and block size for the key
            AES.Padding = PaddingMode.PKCS7
            AES.Mode = CipherMode.CBC
            AES.KeySize = 128
            AES.BlockSize = 128

            ' Encrypt the string to an array of bytes. 
            Dim encrypted As Byte() = EncryptStringToBytes(strCrypt, keyAndIvBytes, keyAndIvBytes)

            AESEncryption = "@" & BitConverter.ToString(encrypted).Replace("-", "").ToUpper
        End Using
    End Function
    Public Shared Function AESDecryption(ByVal strCrypt As String) As String
        Dim keyAndIvBytes As [Byte]() = UTF8Encoding.UTF8.GetBytes(strEncryptionPassword)

        ' Create a new instance of the RijndaelManaged 
        ' class.  This generates a new key and initialization  
        ' vector (IV). 
        Using AES As New RijndaelManaged()
            ' Set the mode, padding and block size for the key
            AES.Padding = PaddingMode.PKCS7
            AES.Mode = CipherMode.CBC
            AES.KeySize = 128
            AES.BlockSize = 128

            Dim encryptedData As Byte() = StringToByteArray(strCrypt.Remove(0, 1))

            Dim roundtrip As String = DecryptStringFromBytes(encryptedData, keyAndIvBytes, keyAndIvBytes)

            AESDecryption = roundtrip
        End Using
    End Function
    Shared Function byteArrayToHexString(ByVal ba As Byte()) As String
        Return BitConverter.ToString(ba).Replace("-", "")
    End Function
    Shared Function StringToByteArray(ByVal hex As String) As Byte()
        Return Enumerable.Range(0, hex.Length).Where(Function(x) x Mod 2 = 0).[Select](Function(x) Convert.ToByte(hex.Substring(x, 2), 16)).ToArray()
    End Function
    Shared Function EncryptStringToBytes(ByVal plainText As String, ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
        ' Check arguments. 
        If plainText Is Nothing OrElse plainText.Length <= 0 Then
            Throw New ArgumentNullException("plainText")
        End If
        If Key Is Nothing OrElse Key.Length <= 0 Then
            Throw New ArgumentNullException("Key")
        End If
        If IV Is Nothing OrElse IV.Length <= 0 Then
            Throw New ArgumentNullException("IV")
        End If
        Dim encrypted() As Byte
        ' Create an RijndaelManaged object 
        ' with the specified key and IV. 
        Using AES As New RijndaelManaged()
            AES.Padding = PaddingMode.PKCS7
            AES.Mode = CipherMode.CBC
            AES.KeySize = 128
            AES.BlockSize = 128

            AES.Key = Key
            AES.IV = IV

            ' Create a decrytor to perform the stream transform. 
            Dim encryptor As ICryptoTransform = AES.CreateEncryptor(AES.Key, AES.IV)
            ' Create the streams used for encryption. 
            Using msEncrypt As New MemoryStream()
                Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
                    Using swEncrypt As New StreamWriter(csEncrypt)

                        'Write all data to the stream.
                        swEncrypt.Write(plainText)
                    End Using
                    encrypted = msEncrypt.ToArray()
                End Using
            End Using
        End Using

        ' Return the encrypted bytes from the memory stream. 
        Return encrypted

    End Function 'EncryptStringToBytes

    Shared Function DecryptStringFromBytes(ByVal cipherText() As Byte, ByVal Key() As Byte, ByVal IV() As Byte) As String

        ' Check arguments. 
        If cipherText Is Nothing OrElse cipherText.Length <= 0 Then
            Throw New ArgumentNullException("cipherText")
        End If
        If Key Is Nothing OrElse Key.Length <= 0 Then
            Throw New ArgumentNullException("Key")
        End If
        If IV Is Nothing OrElse IV.Length <= 0 Then
            Throw New ArgumentNullException("IV")
        End If
        ' Declare the string used to hold 
        ' the decrypted text. 
        Dim plaintext As String = Nothing

        ' Create an RijndaelManaged object 
        ' with the specified key and IV. 
        Using AES As New RijndaelManaged
            AES.Padding = PaddingMode.PKCS7
            AES.Mode = CipherMode.CBC
            AES.KeySize = 128
            AES.BlockSize = 128

            'AES.Key = Key
            'AES.IV = IV

            ' Create a decrytor to perform the stream transform. 
            Dim decryptor As ICryptoTransform = AES.CreateDecryptor(Key, IV)

            ' Create the streams used for decryption. 
            Using msDecrypt As New MemoryStream(cipherText)

                Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)

                    Using srDecrypt As New StreamReader(csDecrypt)


                        ' Read the decrypted bytes from the decrypting stream 
                        ' and place them in a string.
                        plaintext = srDecrypt.ReadToEnd()
                    End Using
                End Using
            End Using
        End Using

        Return plaintext

    End Function