ASP.NET 的 SagePay 协议 3.00 加密错误
SagePay Protocol 3.00 Encryption Error with ASP.NET
花了一天时间努力为 SagePay Forms Protocol 3.00 获取 AES/CBC/PCKS#5 加密
有用的是,SagePay .Net 集成套件的加密功能位于已编译的 DLL 中,这使得该套件几乎无法理解加密需要如何工作。
经过多次尝试正确加密后,我将 运行 保存在 SagePay 的此错误中:
"5068: 该协议版本不支持加密方式"
任何人如果有一些有效的加密/解密功能可以使用 SagePay for Protocol 3.00 使用 AES/CBC/PCKS#5 我真的很感激他们,因为我相信很多其他人也会...
谢谢
因此,在大量阅读和查看大量无效示例之后,我在 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)
希望对那里的人有所帮助,今天这几乎把我逼疯了!
花了一天时间努力为 SagePay Forms Protocol 3.00 获取 AES/CBC/PCKS#5 加密
有用的是,SagePay .Net 集成套件的加密功能位于已编译的 DLL 中,这使得该套件几乎无法理解加密需要如何工作。
经过多次尝试正确加密后,我将 运行 保存在 SagePay 的此错误中:
"5068: 该协议版本不支持加密方式"
任何人如果有一些有效的加密/解密功能可以使用 SagePay for Protocol 3.00 使用 AES/CBC/PCKS#5 我真的很感激他们,因为我相信很多其他人也会...
谢谢
因此,在大量阅读和查看大量无效示例之后,我在 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)
希望对那里的人有所帮助,今天这几乎把我逼疯了!