使用C#中的Bouncy Castle在CCM中加密AES密码

时间:2012-08-20 12:40:14

标签: c# encryption aes bouncycastle

我想使用Bouncy Castle进行AES / CCM加密,我不知道我在下面的代码中做错了什么(键和iv是从他们的HEXA表示中获得的):

using System;
using System.Linq;
using System.Text;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Parameters;

namespace Crypto.Tests {
    class MainClass {
        public static void Main (string[] args) {
            var key = "D44EDA90DDCFA90216BDAC1559D704D336D371A3DA943E935315964D27CC91DC".ToByteArray();
            var iv = "7B13E1A17861356401A3C15F4F0525C3".ToByteArray();
            var nonSecretPayload = new byte[] { };
            var plainText = Encoding.UTF8.GetBytes("Secret message!!!");

            var cipher = new CcmBlockCipher(new AesFastEngine());
            var parameters = new CcmParameters(
                new KeyParameter(key), 64, iv, nonSecretPayload);
            cipher.Init(true, parameters);

            var cipherText = new byte[cipher.GetOutputSize(plainText.Length)];
            var len = cipher.ProcessBytes(plainText, 0, plainText.Length, cipherText, 0);
            cipher.DoFinal(cipherText, len);

            Console.WriteLine(Convert.ToBase64String(cipherText));

            Console.Read();
        }
    }

    public static class StringExtentions {
        public static byte[] ToByteArray(this string toTransform) {
            return Enumerable
                .Range(0, toTransform.Length / 2)
                .Select(i => Convert.ToByte(toTransform.Substring(i * 2, 2), 16))
                .ToArray();
        }
    }
}

执行上述代码会导致以下异常:

Unhandled Exception: System.ArgumentException: Destination array was not long enough. 
Check destIndex and length, and the array's lower bounds.
  at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
  at Org.BouncyCastle.Crypto.Modes.CcmBlockCipher.ProcessPacket(Byte[] input, Int32 inOff, Int32 inLen)
  at Org.BouncyCastle.Crypto.Modes.CcmBlockCipher.DoFinal(Byte[] outBytes, Int32 outOff)
  at Crypto.Tests.MainClass.Main(String[] args) in c:\Users\Emi\Documents\Projects\Crypto\Crypto.Tests\Main.cs:line 23

2 个答案:

答案 0 :(得分:1)

当它试图从你的随机数生成一个计数器IV时,它正在充满弹性的城堡中崩溃,因为它期望一个nonce是(BlockSize - 1)或更少,如增量数。如果从iv中删除一个字节,则可以正常工作。

   var iv = "7B13E1A17861356401A3C15F4F0525".ToByteArray();

答案 1 :(得分:-1)

我不相信这些行符合你的想法,

var key = "D44EDA90DDCFA90216BDAC1559D704D336D371A3DA943E935315964D27CC91DC".ToByteArray(); 
var iv = "7B13E1A17861356401A3C15F4F0525C3".ToByteArray(); 

你可能想要的东西更像是,

var key =
{
    0xD4, 0x4E, 0xDA, 0x90, 0xDD, 0xCF, 0xA9, 0x02,
    0x16, 0xBD, 0xAC, 0x55, 0x9D, 0x70, 0x4D, 0x33,
    0x6D, 0x37, 0x1A, 0x3D, 0xA9, 0x43, 0xE9, 0x35,
    0x31, 0x59, 0x64, 0xD2, 0x7C, 0xC9, 0x1D. 0x0C
}; 

var iv =
{
    0x7B, 0x13, 0xE1, 0xA1, 0x78, 0x61, 0x35, 0x64,
    0x01, 0xA3, 0xC1, 0x5F, 0x4F, 0x05, 0x25, 0xC3
}; 

你的密钥也不是有效的长度(我在结尾添加了0)。