SagePay 协议 3.00 加密错误与 ASP.NET

本文关键字:ASP NET 错误 加密 协议 SagePay | 更新日期: 2023-09-27 17:55:17

花了一天时间努力为SagePay Forms Protocol 3.00获得AES/CBC/PKKS#5加密

SagePay .Net 集成工具包的加密功能位于编译的 DLL 中,这使得该工具包对于理解加密需要如何工作几乎毫无用处。

经过多次尝试正确加密后,我不断遇到SagePay的错误:

"5068:此协议版本不支持加密方法"

任何拥有一些使用AES/CBC/PCKS#5与SagePay for Protocol 3.00配合使用的人,我真的很感激他们,因为我相信许多其他人也会......

谢谢

SagePay 协议 3.00 加密错误与 ASP.NET

因此,

在阅读了大量不起作用的示例之后,我有一个解决方案 vb.net - 我希望这也适用于其他人:

Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Namespace Cypher
    Public NotInheritable Class SagePayAESCBCPKCS5
        ' Singleton pattern used here with ensured thread safety
        Protected Shared ReadOnly _instance As New SagePayAESCBCPKCS5()
        Public Shared ReadOnly Property Instance() As SagePayAESCBCPKCS5
            Get
                Return _instance
            End Get
        End Property

        Public Sub New()
        End Sub
        Public Function DecryptText(encryptedString As String, encryptionKey As String) As String
            Using myRijndael As RijndaelManaged = New RijndaelManaged()
                myRijndael.BlockSize = 128
                myRijndael.KeySize = 128
                myRijndael.Key = UTF8Encoding.UTF8.GetBytes(encryptionKey)
                myRijndael.IV = UTF8Encoding.UTF8.GetBytes(encryptionKey)
                myRijndael.Mode = CipherMode.CBC
                myRijndael.Padding = PaddingMode.None
                Dim encByte As [Byte]() = HexStringToByte(encryptedString)
                'Create a decrytor to perform the stream transform.
                Dim decryptor As ICryptoTransform = myRijndael.CreateDecryptor(myRijndael.Key, myRijndael.IV)
                Dim plaintext As String = ""
                ' Create the streams used for decryption. 
                Using msDecrypt As New MemoryStream(encByte)
                    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()
                            srDecrypt.Close()
                            csDecrypt.Close()
                            msDecrypt.Close()
                        End Using
                    End Using
                End Using
                Return plaintext
            End Using
        End Function

        Public Function EncryptText(plainText As String, encryptionKey As String) As String
            Using myRijndael As RijndaelManaged = New RijndaelManaged()
                myRijndael.BlockSize = 128
                myRijndael.KeySize = 128
                myRijndael.Key = UTF8Encoding.UTF8.GetBytes(encryptionKey)
                myRijndael.IV = UTF8Encoding.UTF8.GetBytes(encryptionKey)
                myRijndael.Mode = CipherMode.CBC
                myRijndael.Padding = PaddingMode.PKCS7
                Dim encrypted As Byte()
                ' Create a decrytor to perform the stream transform.
                Dim encryptor As ICryptoTransform = myRijndael.CreateEncryptor(myRijndael.Key, myRijndael.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
                'Dim encrypted As Byte() = EncryptStringToBytes(plainText, myRijndael.Key, myRijndael.IV)
                'Dim encString As String = Convert.ToBase64String(encrypted)
                Dim encString As String = ByteArrayToHexString(encrypted)
                Return encString
            End Using
        End Function
        Protected Shared Function HexStringToByte(hexString As String) As Byte()
            Try
                Dim bytesCount As Integer = (hexString.Length) ' 2
                Dim bytes As Byte() = New Byte(bytesCount - 1) {}
                For x As Integer = 0 To bytesCount - 1
                    bytes(x) = Convert.ToByte(hexString.Substring(x * 2, 2), 16)
                Next
                Return bytes
            Catch
                Throw
            End Try
        End Function
        Public Shared Function ByteArrayToHexString(ba As Byte()) As String
            Dim hex As New StringBuilder(ba.Length * 2)
            For Each b As Byte In ba
                hex.AppendFormat("{0:x2}", b)
            Next
            Return hex.ToString()
        End Function
    End Class
End Namespace

为SagePay加密

Dim EncryptedString as string = "@" & Cypher.SagePayAESCBCPKCS5.Instance.EncryptText(strPost, strEncryptionPassword).ToUpper()

解密

Dim DecyptedString as string = Cypher.SagePayAESCBCPKCS5.Instance.DecryptText(strCrypt.Remove(0, 1), strEncryptionPassword)

希望能帮助那里的人,这几乎让我今天发疯!