none
Help With Hash / Encryption

    Question

  • I'm newb to hashing and encryption. I created this function to encrypt it and now I'm trying to decrypt it. My Create Hash works but the revert hash is crashing. It wants a string base64[] but it should be a string. What did I do wrong?

    public string CreateHash(string sHash)
            {
                string str = "";
                string[] strArray = new string[16];
                byte[] hash = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(sHash));
                for (int index = 0; index < hash.Length; ++index)
                {
                    strArray[index] = hash[index].ToString("x");
                    str += strArray[index];
                }
                return str;
            }
    
            public string RevertHash(string HashToRevert)
            {
                byte[] inputBuffer = Convert.FromBase64String(HashToRevert);
                byte[] hash = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(HashToRevert));
    
                TripleDESCryptoServiceProvider cryptoServiceProvider = new TripleDESCryptoServiceProvider();
                cryptoServiceProvider.Key = hash;
                cryptoServiceProvider.Mode = CipherMode.ECB;
    
                return Encoding.ASCII.GetString(cryptoServiceProvider.CreateDecryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length));
            }

    Error:

    Invalid length for a Base-64 char array or string."}

    Tuesday, May 2, 2017 4:04 PM

Answers


  • Hi old_School,

    I try the following code. It is working. You can refer it.

            private void button1_Click(object sender, EventArgs e)
            {
                label1.Text = Encrypt("Hello word", true, "XXC");
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                label2.Text = Decrypt((label1.Text.Trim()), true, "XXC");
            }
    
            public static string Encrypt(string toEncrypt, bool useHashing ,string SecurityKey)
            {
                byte[] keyArray;
                byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
                string key = SecurityKey;
                //If hashing use get hashcode regards to your key
                if (useHashing)
                {
                    MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                    keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                    hashmd5.Clear();
                }
                else
                    keyArray = UTF8Encoding.UTF8.GetBytes(key);
    
                TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
                //set the secret key for the tripleDES algorithm
                tdes.Key = keyArray;
                //mode of operation. there are other 4 modes. We choose ECB(Electronic code Book)
                tdes.Mode = CipherMode.ECB;
                //padding mode(if any extra byte added)
                tdes.Padding = PaddingMode.PKCS7;
    
                ICryptoTransform cTransform = tdes.CreateEncryptor();
                //transform the specified region of bytes array to resultArray
                byte[] resultArray = cTransform.TransformFinalBlock
                        (toEncryptArray, 0, toEncryptArray.Length);
                //Release resources held by TripleDes Encryptor
                tdes.Clear();
                //Return the encrypted data into unreadable string format
                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
    
    
            public static string Decrypt(string cipherString, bool useHashing, string SecurityKey)
            {
                byte[] keyArray;
     
                byte[] toEncryptArray = Convert.FromBase64String(cipherString);
                string key = SecurityKey;
    
                if (useHashing)
                {
                    //if hashing was used get the hash code with regards to your key
                    MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                    keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
       
                    hashmd5.Clear();
                }
                else
                {
                    //if hashing was not implemented get the byte code of the key
                    keyArray = UTF8Encoding.UTF8.GetBytes(key);
                }
    
                TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
                //set the secret key for the tripleDES algorithm
                tdes.Key = keyArray;
                //mode of operation. there are other 4 modes.
                tdes.Mode = CipherMode.ECB;
                //padding mode(if any extra byte added)
                tdes.Padding = PaddingMode.PKCS7;
    
                ICryptoTransform cTransform = tdes.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock
                        (toEncryptArray, 0, toEncryptArray.Length);
                //Release resources held by TripleDes Encryptor
                tdes.Clear();
                //return the Clear decrypted TEXT
                return UTF8Encoding.UTF8.GetString(resultArray);
            }


    TripleDESCryptoServiceProvider Class:
    https://msdn.microsoft.com/en-us/library/system.security.cryptography.tripledescryptoserviceprovider(v=vs.110).aspx


    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by old_School Wednesday, May 31, 2017 7:29 PM
    Thursday, May 25, 2017 8:48 AM
    Moderator

All replies

  • Tried this:

    public string RevertHash(string HashToRevert)
            {
                try
                {
                    byte[] inputBuffer = Encoding.ASCII.GetBytes(HashToRevert);
                    //byte[] inputBuffer = Convert.FromBase64String(HashToRevert);
                    byte[] hash = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(HashToRevert));
    
                    TripleDESCryptoServiceProvider cryptoServiceProvider = new TripleDESCryptoServiceProvider();
                    cryptoServiceProvider.Key = hash;
                    cryptoServiceProvider.Mode = CipherMode.ECB;
    
                    return Encoding.ASCII.GetString(cryptoServiceProvider.CreateDecryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length));
                }
                catch(Exception ex)
                {
                    return string.Empty;
                }            
            }

    get this error:

    Length of the data to decrypt is invalid.

    Tuesday, May 2, 2017 4:49 PM

  • Hi old_School,

    I try the following code. It is working. You can refer it.

            private void button1_Click(object sender, EventArgs e)
            {
                label1.Text = Encrypt("Hello word", true, "XXC");
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                label2.Text = Decrypt((label1.Text.Trim()), true, "XXC");
            }
    
            public static string Encrypt(string toEncrypt, bool useHashing ,string SecurityKey)
            {
                byte[] keyArray;
                byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
                string key = SecurityKey;
                //If hashing use get hashcode regards to your key
                if (useHashing)
                {
                    MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                    keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                    hashmd5.Clear();
                }
                else
                    keyArray = UTF8Encoding.UTF8.GetBytes(key);
    
                TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
                //set the secret key for the tripleDES algorithm
                tdes.Key = keyArray;
                //mode of operation. there are other 4 modes. We choose ECB(Electronic code Book)
                tdes.Mode = CipherMode.ECB;
                //padding mode(if any extra byte added)
                tdes.Padding = PaddingMode.PKCS7;
    
                ICryptoTransform cTransform = tdes.CreateEncryptor();
                //transform the specified region of bytes array to resultArray
                byte[] resultArray = cTransform.TransformFinalBlock
                        (toEncryptArray, 0, toEncryptArray.Length);
                //Release resources held by TripleDes Encryptor
                tdes.Clear();
                //Return the encrypted data into unreadable string format
                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
    
    
            public static string Decrypt(string cipherString, bool useHashing, string SecurityKey)
            {
                byte[] keyArray;
     
                byte[] toEncryptArray = Convert.FromBase64String(cipherString);
                string key = SecurityKey;
    
                if (useHashing)
                {
                    //if hashing was used get the hash code with regards to your key
                    MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                    keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
       
                    hashmd5.Clear();
                }
                else
                {
                    //if hashing was not implemented get the byte code of the key
                    keyArray = UTF8Encoding.UTF8.GetBytes(key);
                }
    
                TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
                //set the secret key for the tripleDES algorithm
                tdes.Key = keyArray;
                //mode of operation. there are other 4 modes.
                tdes.Mode = CipherMode.ECB;
                //padding mode(if any extra byte added)
                tdes.Padding = PaddingMode.PKCS7;
    
                ICryptoTransform cTransform = tdes.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock
                        (toEncryptArray, 0, toEncryptArray.Length);
                //Release resources held by TripleDes Encryptor
                tdes.Clear();
                //return the Clear decrypted TEXT
                return UTF8Encoding.UTF8.GetString(resultArray);
            }


    TripleDESCryptoServiceProvider Class:
    https://msdn.microsoft.com/en-us/library/system.security.cryptography.tripledescryptoserviceprovider(v=vs.110).aspx


    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by old_School Wednesday, May 31, 2017 7:29 PM
    Thursday, May 25, 2017 8:48 AM
    Moderator