Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 903873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:57:30+00:00 2026-05-15T15:57:30+00:00

I get the following error when I try to create a IV initialization vector

  • 0

I get the following error when I try to create a IV initialization vector for TripleDES encryptor.

Please see the code example:

TripleDESCryptoServiceProvider tripDES = new TripleDESCryptoServiceProvider();

byte[] key = Encoding.ASCII.GetBytes("SomeKey132123ABC");
byte[] v4 = key;
byte[] connectionString = Encoding.ASCII.GetBytes("SomeConnectionStringValue");
byte[] encryptedConnectionString = Encoding.ASCII.GetBytes("");

// Read the key and convert it to byte stream
tripDES.Key = key; 
tripDES.IV = v4;

This is the exception that I get from the VS.

Specified initialization vector (IV) does not match the block size for this algorithm.

Where am I going wrong?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:57:30+00:00Added an answer on May 15, 2026 at 3:57 pm

    I’ve upvoted every answer (well the ones that are here before mine!) here as they’re all correct.

    However there’s a bigger mistake you’re making (one which I also made v.early on) – DO NOT USE A STRING TO SEED THE IV OR KEY!!!

    A compile-time string literal is a unicode string and, despite the fact that you will not be getting either a random or wide-enough spread of byte values (because even a random string contains lots of repeating bytes due to the narrow byte range of printable characters), it’s very easy to get a character which actually requires 2 bytes instead of 1 – try using 8 of some of the more exotic characters on the keyboard and you’ll see what I mean – when converted to bytes you can end up with more than 8 bytes.

    Okay – so you’re using ASCII Encoding – but that doesn’t solve the non-random problem.

    Instead you should use RNGCryptoServiceProvider to initialise your IV and Key and, if you need to capture a constant value for this for future use, then you should still use that class – but capture the result as a hex string or Base-64 encoded value (I prefer hex, though).

    To achieve this simply, I’ve written a macro that I use in VS (bound to the keyboard shortcut CTRL+SHIFT+G, CTRL+SHIFT+H) which uses the .Net PRNG to produce a hex string:

    Public Sub GenerateHexKey()
      Dim result As String = InputBox("How many bits?", "Key Generator", 128)
    
      Dim len As Int32 = 128
    
      If String.IsNullOrEmpty(result) Then Return
    
      If System.Int32.TryParse(result, len) = False Then
          Return
      End If
    
      Dim oldCursor As Cursor = Cursor.Current
    
      Cursor.Current = Cursors.WaitCursor
    
      Dim buff((len / 8) - 1) As Byte
      Dim rng As New System.Security.Cryptography.RNGCryptoServiceProvider()
    
      rng.GetBytes(buff)
    
      Dim sb As New StringBuilder(CType((len / 8) * 2, Integer))
      For Each b In buff
          sb.AppendFormat("{0:X2}", b)
      Next
    
      Dim selection As EnvDTE.TextSelection = DTE.ActiveDocument.Selection
      Dim editPoint As EnvDTE.EditPoint
    
      selection.Insert(sb.ToString())
      Cursor.Current = oldCursor
    End Sub
    

    Now all you need to do is to turn your hex string literal into a byte array – I do this with a helpful extension method:

    public static byte[] FromHexString(this string str)
    {
      //null check a good idea
      int NumberChars = str.Length;
      byte[] bytes = new byte[NumberChars / 2];
      for (int i = 0; i < NumberChars; i += 2)
        bytes[i / 2] = Convert.ToByte(str.Substring(i, 2), 16);
      return bytes;
    }
    

    There are probably better ways of doing that bit – but it works for me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 457k
  • Answers 457k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, put !important behind them: .class{ height:100px !important; width: ...etc… May 15, 2026 at 10:46 pm
  • Editorial Team
    Editorial Team added an answer If you're persisting your workflow, you want to make sure… May 15, 2026 at 10:46 pm
  • Editorial Team
    Editorial Team added an answer A small amount of Regex a bit of Convert.FromBase64String bake… May 15, 2026 at 10:46 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.