1

I have the following structure which I am using for my Encrypt function. I'm possibly missing something from the structure in my code as I'm a PHP guy rather than a C#.

What's happening is that each time in my log the hash changes, which it shouldn't and should only equal one specific hash for the input. This ties in with this next issue..

What also happens is when I use my test passphrase, which is "MysecretPassPhrase", I have a byte length issue. I don't want to change this passphrase as it is the exact length of my true passphrase so is there anything I can do code-wise to fix the issue?

When I used a longer passphrase such as "MysecretPassPhrase123456" with "David" as the input, it will output the different hashes each time: CJ+mgAeL9x+qMLId+nHvXw==, Ladj1D+LJgZCrwPatsQsEQ==, etc.

Structure required

  • Cipher Rijndael (AES)
  • Block Size 128 bits (16 bytes)
  • Mode CBC (Cipher Block Chaining)
  • Key MD5 hash passphrase
  • IV Same as the key
  • Data Encoding Base64
  • Character UTF-8 Encoding

Error

CryptographicException: Key size not supported by algorithm System.Security.Cryptography.SymmetricAlgorithm.set_Key (System.Byte[] value) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Security.Cryptography/SymmetricAlgorithm.cs:176) APIConnector.Encrypt (System.String toEncrypt) (at Assets/APIConnector.cs:59)

Code

using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
using System.IO;

void submit() {
  Debug.Log ("first name is: " + firstName + " encrypted is: " + Encrypt(firstName));
}


public static string Encrypt (string toEncrypt) {
  byte[] keyArray = UTF8Encoding.UTF8.GetBytes ("MysecretPassPhrase");
  // 256-AES key
  int numBytes = System.Text.Encoding.UTF8.GetBytes(toEncrypt).Length;
  Debug.Log ("Bytes: " + numBytes);
  byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes (toEncrypt);
  RijndaelManaged rDel = new RijndaelManaged ();
  rDel.Key = keyArray;
  rDel.BlockSize = 128;
  rDel.Mode = CipherMode.CBC;
  // http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
  rDel.Padding = PaddingMode.PKCS7;
  // better lang support
  ICryptoTransform cTransform = rDel.CreateEncryptor ();
  byte[] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
  return Convert.ToBase64String (resultArray, 0, resultArray.Length);
}
DT.DTDG
  • 765
  • 1
  • 13
  • 31
  • @JonathonReinhart I didn't add in the `firstName` input which is submitted as it's additional code that will take away from the issue. But I used `Encrypt(firstName)` to hash the input. – DT.DTDG May 21 '14 at 23:46
  • The error given above is "Key size not supported by algorithm". Your key array is 18 bytes (UTF8) which is 144 bits, not 128 bits as indicated by your BlockSize. [See this article.](http://stackoverflow.com/questions/2919228/specified-key-is-not-a-valid-size-for-this-algorithm) – Peter Gluck May 21 '14 at 23:49
  • Thanks @PeterGluck but even changing the block size to 144 still produces the same error. – DT.DTDG May 21 '14 at 23:52
  • 1
    No, you cannot change the block size to an arbitrary value [(see this article)](http://stackoverflow.com/questions/2919228/specified-key-is-not-a-valid-size-for-this-algorithm). You must change the key to a valid size. – Peter Gluck May 21 '14 at 23:53
  • @PeterGluck Ahhh thanks for pointing that out! +1 for you, sir. Aside from that do you see anything else with regards to my required structure that I'm missing in code? Thanks again. – DT.DTDG May 21 '14 at 23:56
  • Hashing and Encrypted are somewhat related although **very different** things. Are you trying to encrypt data so the plaintext can be recovered later? Or are you trying to actually generate a *one-way* hash from a set of inputs? – Jonathon Reinhart May 22 '14 at 03:17

1 Answers1

0

The error given above is "Key size not supported by algorithm". Your key array is 18 bytes (UTF8) which is 144 bits, not 128 bits as indicated by your BlockSize.

You must change the key to a valid size. See this article for more information.

Community
  • 1
  • 1
Peter Gluck
  • 8,168
  • 1
  • 38
  • 37