2015-06-13 60 views
1

我以为我会发布这个,因为我没有找到V3.00升级所需的AES加密的现成解决方案。Sage Pay Forms V3.00 AES-128加密VB.Net

由于某种原因,SagePay C#解决方案示例中没有加密/解密代码示例,据我所知。

我拼凑从现有柱和RijndaelManaged类VB示例(https://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1)代码....

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 

希望这将是有用的尤其因为只有6周左迁移到V3 .00和所有V2选项都关闭。

回答

0

也许我在这里很愚蠢,但如果你引用SagePay.IntegrationKit.DotNet DLL你应该有权访问他们的Crytography类。

至少这就是我所做的;添加.dll作为参考,将其作为文件顶部导入,然后使用Cryptography.DecodeAndDecrypt和Cryptography.EncryptAndEncode。

0

c# AES Decryption

是对所需V3.00升级SagePay C#的AES加密一个伟大的线程。