1
votes

My structure for the c# encrypt/decrypt is as follows:

  • 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

I'm using input Player for both inputs as a test, however it is not returning the correct MD5 hash output, and also there's a small issue with my Decrypt function for byte[] toEncryptArray = Convert.FromBase64String (toDecrypt);.

Incorrect hash output and Error

playerID is: Player encrypted is: ZCKgr4veKtCDrD6mL+P6Yg==
FormatException: Invalid length. System.Convert.FromBase64String (System.String s) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System/Convert.cs:146) APIConnector.Decrypt (System.String toDecrypt) (at Assets/APIConnector.cs:122)

Any ideas on what I can do to 1) fix this error and 2) get my hash output correct based on ym structure above? Thanks!

void submit(){

        Debug.Log ("playerID is: " + firstName + " encrypted is: " + Encrypt(firstName));
        Debug.Log ("password is: " + password + " decrypted is: " + Decrypt(password));

    }


    public static string Encrypt (string toEncrypt)
    {
        byte[] keyArray  = UTF8Encoding.UTF8.GetBytes ("SecretPassphrase");

        // 256-AES key
        byte[] toEncryptArray   = UTF8Encoding.UTF8.GetBytes (toEncrypt);

        RijndaelManaged rDel  = new RijndaelManaged ();
        rDel.Key              = keyArray;
        rDel.IV              = keyArray;
        rDel.Mode             = CipherMode.CBC;
        rDel.BlockSize        = 128;

        // 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);
    }

    // 
    public static string Decrypt (string toDecrypt)
    {
        byte[] keyArray  = UTF8Encoding.UTF8.GetBytes ("SecretPassphrase");

        // AES-256 key
        byte[] toEncryptArray   = Convert.FromBase64String (toDecrypt);

        RijndaelManaged rDel  = new RijndaelManaged ();
        rDel.Key              = keyArray;
        rDel.IV              = keyArray;
        rDel.Mode             = CipherMode.CBC;
        rDel.BlockSize        = 128;

        // http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
        rDel.Padding    = PaddingMode.PKCS7;

        // better lang support
        ICryptoTransform cTransform  = rDel.CreateDecryptor ();

        byte[] resultArray   = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);

        return UTF8Encoding.UTF8.GetString (resultArray);
    }
2
Do you realize that you are trying to decode "Player" string? - Sergey Krusch
Sorry should've decoded password string, updating... - DT.DTDG

2 Answers

0
votes

You should pass encrypted value to Decrypt, not firstname. Base64 convertion failing because firstname is in clear.

    Debug.Log ("playerID is: " + firstName + " encrypted is: " + Encrypt(firstName));
    Debug.Log ("password is: " + password + " decrypted is: " + Decrypt(firstName));

It should be

string enc = Encrypt(firstName);
Debug.Log ("playerID is: " + firstName + " encrypted is: " + enc);
Debug.Log ("password is: " + password + " decrypted is: " + Decrypt(enc));
0
votes

Your submit method is not decrypting your encrypted string, it's trying to decrypt your un-encrypted original string. Changing your submit method to the below makes the program execut without error;

static void submit(){
    string password = Encrypt(firstName);
    Console.WriteLine ("playerID is: " + firstName + 
                       " encrypted is: " + password);
    Console.WriteLine ("password is: " + password + 
                       " decrypted is: " + Decrypt(password));
}

Also, since you're not actually MD5'ing your key, you're lucky that "SecretPassphrase " is actually 16 characters which is the exact length of key MD5 would have produced. Changing to a shorter or longer string will break the program.