开发者

How to use 3DES encryption results to calculate the MAC value

I have a project to do for work to do in C#. I have the requirements for a project and this part is just a small piece in the entire project. I was given test data and the result. I need to code it so that I get the correct results. And at the moment I'm not getting the final result.

Please don't question or criticise the requirements, it's what I have and need to sort out and code it.

I was told to take input string "abc" and compute the SHA-1 hash for this. I got this part to work, here is the code:

private string CalculateSHA1Hash(string text, Encoding characterEncoding)
{
     byte[] buffer = characterEncoding.GetBytes(text);
     SHA1CryptoServiceProvider sha1CryptoServiceProvider = new SHA1CryptoServiceProvider();
     byte[] s = sha1CryptoServiceProvider.ComputeHash(buffer);
     string hash = BitConverter.ToString(sha1CryptoServiceProvider.ComputeHash(buffer)).Replace("-", "");

     return hash;
}

I used UTF8Encoding because none was specified in the requirements doc. The result that I got from this is A9993E364706816ABA3E25717850C26C9CD0D89D.

I was then told to break up this string into 3 string blocks, of 16 characters each, and use just the 1st block. This is what I got:

block1: A9993E364706816A

I was also given 2 keys:

K1: 0123456789ABCDEF
K2: FEDCBA9876543210

Block1 is to be used as input string to a 3DES encryption using the 2 keys.

The result of the cipher text must be 6E5271A3F3F5C418, I am not getting this.

Below is my calculations. Can someone please see if I am doing this correctly and where I am doing this wrong. Chris (on SO) gave me some articles to read but I still can't get the results that I need to get. Is there something that cater for this already, I'm I just totally confused, or what?

public string Encrypt(string message)
{
     string result开发者_Go百科 = string.Empty;

     // Calculate the SHA1 hash
     UTF8Encoding characterEncoding = new UTF8Encoding();
     string sha1HashResult = CalculateSHA1Hash(message, characterEncoding);

     block1 = sha1HashResult.Substring(0, 16);

     byte[] block1ByteArray = characterEncoding.GetBytes(block1);

     string key = "0x" + accessKey1 + accessKey2 + accessKey1;
     byte[] keyByteArray = StringToByteArray(key).ToArray();

     byte[] enc = ComputeTripleDesEncryption(block1ByteArray, keyByteArray);
     result = ByteArrayToString(enc);

     return result;
}

public byte[] ComputeTripleDesEncryption(byte[] plainText, byte[] key)
{
     TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();

     des.Key = key;
     des.GenerateIV();
     des.Mode = CipherMode.ECB;
     des.Padding = PaddingMode.None;

     ICryptoTransform ic = des.CreateEncryptor();

     byte[] enc = ic.TransformFinalBlock(plainText, 0, plainText.Length);

     return enc;
}

private byte[] StringToByteArray(String hex)
{
     if (hex.Substring(0, 2) == "0x")
     {
          hex = hex.Substring(2);
     }

     int NumberChars = hex.Length;
     byte[] bytes = new byte[NumberChars / 2];
     for (int i = 0; i < NumberChars; i += 2)
     {
          bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
     }

     return bytes;
}

private string ByteArrayToString(byte[] ba)
{
     string hex = BitConverter.ToString(ba);

     return hex.Replace("-", "");
}

I really do not know what to do further.


There's a few things wrong with what you have right now:

  1. You're specifying that you need to use an IV, but you're using ECB (which doesn't use an IV)
  2. You're generating the IV randomly, using GenerateIV(). This will cause the result to be different every time, if you're not using ECB.
  3. You're only performing the transform on the final block, instead of the whole data.

See the following code sample for a decent idea of how to use 3DES in C#:
http://msdn.microsoft.com/en-us/library/system.security.cryptography.tripledescryptoserviceprovider.aspx

I'd guess that since you're specifying an IV, you're actually meant to be using CBC instead of ECB. Try it out and see what you get.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜