C#无法生成初始化向量IV [英] C# Can't generate initialization vector IV

查看:801
本文介绍了C#无法生成初始化向量IV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误,当我试图创建的TripleDES加密一个IV初始化向量

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

请查看代码示例:

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;

这是我从VS得到的异常。

This is the exception that I get from the VS.

指定的初始化向量(IV)不匹配这个算法的块大小。

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

我要去哪里错了?

感谢您

推荐答案

我因为他们是正确的已经upvoted每一个答案(清楚,在这里我之前的那些!)在这里。

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

但是有你正在做一个更大的错误(其中之一我也做v.early上) - 不使用字符串种子IV或钥匙!

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!!!

一个编译时字符串是Unicode字符串和,尽管事实上,你将不会得到字节值是无规或宽足够的利差(因为即使是一个随机字符串包含大量重复的字节由于可打印字符狭窄字节范围),这是很容易得到一个字符其中实际上需要2个字节,而不是1 - 尝试使用一些比较奇特的字符8键盘上,你会明白我的意思 - 当转换为字节可以超过8个字节结束

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.

好吧 - 让你在使用ASCII编码 - 但这并不解决非随机的问题。

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

相反,你应该使用RNGCryptoServiceProvider初始化您的IV和密钥,如果您需要捕获一个恒定值,这为将来使用,那么你还是应该使用这个类 - 但捕获结果作为十六进制字符串或Base-64编码值(我喜欢十六进制,虽然)。

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).

要实现这个简单,我已经写了,我在VS使用宏(绑定到键盘快捷键的 CTRL + SHIFT + G,Ctrl + Shift + H 的),它使用.NET PRNG产生一个十六进制字符串:

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.

这篇关于C#无法生成初始化向量IV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆