I have an AES256 key in hexadecimal "8A64E947DAA12B6B7761F30192219270883F24D358D55A1E98E5364B801E9120".
That key is encrypted in AES256 and have to decrypt the following key and initialization vector:
Key = "517565747A616C636F61746C5349495038383838383838382020202020202020"
IV = "00000000000000000000000000000000".
The result of the decryption must be a string of 64 characters (hexadecimal).
This is where I'm having trouble, I can not get that string of 64 characters with the code.
Imports System.IO Imports System.Security.Cryptography Imports System.Text
Public Class AES
Public Function Decrypt(ByVal encryptedBytes As String, ByVal secretKey As String) As String Dim plainText As String = Nothing
Using inputStream As MemoryStream = New MemoryStream(Convert.FromBase64String(encryptedBytes))
Dim algorithm As RijndaelManaged = getAlgorithm(secretKey)
Using cryptoStream As CryptoStream = New CryptoStream(inputStream, algorithm.CreateDecryptor(), CryptoStreamMode.Read)
Dim outputBuffer(0 To CType(inputStream.Length - 1, Integer)) As Byte Dim readBytes As Integer = cryptoStream.Read(outputBuffer, 0, CType(inputStream.Length, Integer)) plainText = Encoding.Unicode.GetString(outputBuffer, 0, readBytes)
End Using
End Using
Return plainText
End Function
Public Function Encrypt(ByVal plainText As String, ByVal secretKey As String) As String Dim encryptedPassword As String = Nothing
Using outputStream As MemoryStream = New MemoryStream()
Dim algorithm As RijndaelManaged = getAlgorithm(secretKey)
Using cryptoStream As CryptoStream = New CryptoStream(outputStream, algorithm.CreateEncryptor(), CryptoStreamMode.Write)
Dim inputBuffer() As Byte = Text.Encoding.Unicode.GetBytes(plainText) cryptoStream.Write(inputBuffer, 0, inputBuffer.Length) cryptoStream.FlushFinalBlock() encryptedPassword = Convert.ToBase64String(outputStream.ToArray())
End Using
End Using
Return encryptedPassword
End Function
Private Function getAlgorithm(ByVal secretKey As String) As RijndaelManaged Const salt As String = "00000000000000000000000000000000" Const keySize As Integer = 256 Dim keyBuilder As Rfc2898DeriveBytes = New Rfc2898DeriveBytes(secretKey, Text.Encoding.Unicode.GetBytes(salt)) Dim algorithm As RijndaelManaged = New RijndaelManaged() algorithm.KeySize = keySize algorithm.IV = keyBuilder.GetBytes(CType(algorithm.BlockSize / 8, Integer)) algorithm.Key = keyBuilder.GetBytes(CType(algorithm.KeySize / 8, Integer)) algorithm.Padding = PaddingMode.None algorithm.Mode = CipherMode.CBC
Return algorithm
End Function
End Class
I tried first convert hexadecimal string to decrypt, but bring me a double chain length 64 characters * 2.
I hope they can support me, I have two days researching on the Internet and I can not provide solution to my problem :(
Thanks!