Sage Pay Forms V3.00 AES-128 Encryption VB.Net -


i thought post did not find off-the-shelf solution aes encryption needed v3.00 upgrade.

the sagepay c# solution example reason did not have have encryption/decryption code example in far see.

i cobbled code existing posts , rijndaelmanaged class vb example (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 string) string         dim keyandivbytes [byte]() = utf8encoding.utf8.getbytes(strencryptionpassword)          ' create new instance of rijndaelmanaged          ' class.  generates new key , initialization           ' vector (iv).          using aes new rijndaelmanaged()             ' set mode, padding , block size key             aes.padding = paddingmode.pkcs7             aes.mode = ciphermode.cbc             aes.keysize = 128             aes.blocksize = 128              ' encrypt string array of bytes.              dim encrypted byte() = encryptstringtobytes(strcrypt, keyandivbytes, keyandivbytes)              aesencryption = "@" & bitconverter.tostring(encrypted).replace("-", "").toupper         end using     end function     public shared function aesdecryption(byval strcrypt string) string         dim keyandivbytes [byte]() = utf8encoding.utf8.getbytes(strencryptionpassword)          ' create new instance of rijndaelmanaged          ' class.  generates new key , initialization           ' vector (iv).          using aes new rijndaelmanaged()             ' set mode, padding , block size key             aes.padding = paddingmode.pkcs7             aes.mode = ciphermode.cbc             aes.keysize = 128             aes.blocksize = 128              dim encrypteddata byte() = stringtobytearray(strcrypt.remove(0, 1))              dim roundtrip string = decryptstringfrombytes(encrypteddata, keyandivbytes, keyandivbytes)              aesdecryption = roundtrip         end using     end function     shared function bytearraytohexstring(byval ba byte()) string         return bitconverter.tostring(ba).replace("-", "")     end function     shared function stringtobytearray(byval hex string) 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 string, byval key() byte, byval iv() byte) byte()         ' check arguments.          if plaintext nothing orelse plaintext.length <= 0             throw new argumentnullexception("plaintext")         end if         if key nothing orelse key.length <= 0             throw new argumentnullexception("key")         end if         if iv nothing orelse iv.length <= 0             throw new argumentnullexception("iv")         end if         dim encrypted() byte         ' create rijndaelmanaged object          ' specified key , iv.          using aes new rijndaelmanaged()             aes.padding = paddingmode.pkcs7             aes.mode = ciphermode.cbc             aes.keysize = 128             aes.blocksize = 128              aes.key = key             aes.iv = iv              ' create decrytor perform stream transform.              dim encryptor icryptotransform = aes.createencryptor(aes.key, aes.iv)             ' create streams used encryption.              using msencrypt new memorystream()                 using csencrypt new cryptostream(msencrypt, encryptor, cryptostreammode.write)                     using swencrypt new streamwriter(csencrypt)                          'write data stream.                         swencrypt.write(plaintext)                     end using                     encrypted = msencrypt.toarray()                 end using             end using         end using          ' return encrypted bytes memory stream.          return encrypted      end function 'encryptstringtobytes      shared function decryptstringfrombytes(byval ciphertext() byte, byval key() byte, byval iv() byte) string          ' check arguments.          if ciphertext nothing orelse ciphertext.length <= 0             throw new argumentnullexception("ciphertext")         end if         if key nothing orelse key.length <= 0             throw new argumentnullexception("key")         end if         if iv nothing orelse iv.length <= 0             throw new argumentnullexception("iv")         end if         ' declare string used hold          ' decrypted text.          dim plaintext string = nothing          ' create rijndaelmanaged object          ' specified key , iv.          using aes new rijndaelmanaged             aes.padding = paddingmode.pkcs7             aes.mode = ciphermode.cbc             aes.keysize = 128             aes.blocksize = 128              'aes.key = key             'aes.iv = iv              ' create decrytor perform stream transform.              dim decryptor icryptotransform = aes.createdecryptor(key, iv)              ' create streams used decryption.              using msdecrypt new memorystream(ciphertext)                  using csdecrypt new cryptostream(msdecrypt, decryptor, cryptostreammode.read)                      using srdecrypt new streamreader(csdecrypt)                           ' read decrypted bytes decrypting stream                          ' , place them in string.                         plaintext = srdecrypt.readtoend()                     end using                 end using             end using         end using          return plaintext      end function 

hopefully of use there 6 weeks left migrate v3.00 , v2 options switched off.

maybe i'm being silly here if reference sagepay.integrationkit.dotnet dll should have access crytography class.

at least thats i've done; added .dll reference, imported top of file used cryptography.decodeanddecrypt & cryptography.encryptandencode.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -