Encryption Woes

Dec 19, 2013 at 7:06 PM
Edited Dec 19, 2013 at 7:06 PM
I need to integrate basic encryption into a .Net portable class using PCLContrib

I have converted desktop framework code to portable as below but I get a padding error on decryption.
Can anybody help me out?:

Public Function XAES_Encrypt(input As String, pass As String) As String
    Dim AES As New System.Security.Cryptography.AesManaged
    Dim Hash_AES As New System.Security.Cryptography.SHA256Managed
    Dim encrypted As String = ""
    Try
        Dim hash As Byte() = New Byte(31) {}
        Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.UnicodeEncoding.Unicode.GetBytes(pass))
        Array.Copy(temp, 0, hash, 0, 16)
        Array.Copy(temp, 0, hash, 15, 16)
        AES.Key = hash
        ''''''''''''''''''AES.Mode = System.Security.Cryptography.CipherMode.ECB
        Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor()
        Dim Buffer As Byte() = System.Text.UnicodeEncoding.Unicode.GetBytes(input)
        encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
        Return encrypted
    Catch ex As Exception
        Return ex.Message & "error"
    End Try
End Function

Public Function XAES_Decrypt(input As String, pass As String) As String

    Dim AES As New System.Security.Cryptography.AesManaged
    Dim Hash_AES As New System.Security.Cryptography.SHA256Managed
    Dim decrypted As String = ""
    Try
        Dim hash As Byte() = New Byte(31) {}
        Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.UnicodeEncoding.Unicode.GetBytes(pass))
        Array.Copy(temp, 0, hash, 0, 16)
        Array.Copy(temp, 0, hash, 15, 16)
        AES.Key = hash
        '''''''''''''AES.Mode = System.Security.Cryptography.CipherMode.ECB
        Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor()
        Dim Buffer As Byte() = Convert.FromBase64String(input)
        Dim TFB() As Byte = DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)
        '''''''''''ERROR:Padding is invalid and cannot be removed.
        decrypted = System.Text.UnicodeEncoding.Unicode.GetString(TFB, 0, TFB.Length)

        Return decrypted
    Catch ex As Exception
        Return ex.Message & "Error"
    End Try
End Function