Results 1 to 2 of 2

Thread: AES Encryption - Invalid block size for Initialization Vector

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2010
    Posts
    79

    AES Encryption - Invalid block size for Initialization Vector

    greetings. I am trying to encrypt a string using the code below. The issue is I get this error and I have no clue (I'm just learning about encryption) what to do or even where to look. The SharedKey and the IV have been supplied as Hex values. The SharedKey is 64 bytes and the IV is 32 bytes.

    System.Security.Cryptography.CryptographicException: 'Specified initialization vector (IV) does not match the block size for this algorithm.'

    Code:
        Public Function Encrypt(ByVal strValue As String) As String
            'Create instance of a Rijndael Managed object
            Dim aes As New RijndaelManaged
            'Set appropriate values of object
            aes.Padding = PaddingMode.PKCS7
            aes.KeySize = 256
            aes.Mode = CipherMode.CBC
    
            'Create streams to work with encryption process
            Dim msEncrypt As New MemoryStream()
            'SharedKey = "64 byte string"
            'IV = "32 byte string"
            Dim SharedKey As Byte() = Encoding.GetEncoding(1252).GetBytes(strSharedKey)
            Dim IV As Byte() = Encoding.GetEncoding(1252).GetBytes(strIV)
            Dim csEncrypt As New CryptoStream(msEncrypt, aes.CreateEncryptor(SharedKey, IV), CryptoStreamMode.Write)
            'Convert string value to byte array
            Dim toEncrypt As Byte() = Encoding.GetEncoding(1252).GetBytes(strValue)
            toEncrypt = Encoding.Convert(Encoding.GetEncoding(1252), Encoding.UTF8, toEncrypt)
            'Perform encryption
    
            csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)
            csEncrypt.FlushFinalBlock()
            'Return Base64 string
            Return Convert.ToBase64String(msEncrypt.ToArray())
    
            'Dim u As System.Text.UnicodeEncoding = System.Text.Encoding.Unicode
            'Dim a As System.Text.ASCIIEncoding = System.Text.Encoding.ASCII
            'Return a.GetByteCount(SharedKey)  '64 bytes
    
        End Function
    Any help is mucho appreciated

  2. #2
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: AES Encryption - Invalid block size for Initialization Vector

    Ensure that your IV is 16 bytes (from memory )

    Kris

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width