C#, how to use decryption

I had a programmer write an encryption routine, but they are now not available, so need help to decrypt.
Below is encryption:

      public static string HashPw(string Str)
            {
                  CryptoLib.cCrypto crypto = new CryptoLib.cCrypto();
                  System.Text.StringBuilder sb = new StringBuilder("0x");
                  char[] PwdChars = crypto.EncryptTripleDES(Str, "").ToCharArray();
                  foreach (char part in PwdChars)
                  {
                        sb.Append(String.Format("{0:x}", Convert.ToByte(part)));
                  }

                  return sb.ToString();
            }


We tried:            public static string HashPwBck(string Str)
            {
                  CryptoLib.cCrypto crypto = new CryptoLib.cCrypto();
                  int strLen = Str.Length;
                  string sStr = Str.Substring(2,strLen-2);
                  System.Text.StringBuilder sb = new StringBuilder("");
                  //sb.Append(crypto.DecryptTripleDES(sStr,"0:x"));
                  char[] PwdChars = crypto.DecryptTripleDES(sStr,"").ToCharArray();
                  foreach (char part in PwdChars)
                  {
                        sb.Append(String.Format("", Convert.ToByte(part)));
                  }
                  return sb.ToString();
            }
But it bombs.
dcassAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
I think this block:
                  foreach (char part in PwdChars)
                  {
                        sb.Append(String.Format("{0:x}", Convert.ToByte(part)));
                  }

should be:
                  foreach (char part in PwdChars)
                  {
                        sb.Append(String.Format("{0:X2}", Convert.ToByte(part)));
                  }

or even shorter and clearer:
                  foreach (char part in PwdChars)
                  {
                        sb.AppendFormat("{0:X2}", (byte)part);
                  }

notice X2 will align the bytes to be always 2 positions wide.
0
Jaime OlivaresSoftware ArchitectCommented:
about the decryption, it should be something like:

            public static string HashPwBck(string Str)
            {
                  CryptoLib.cCrypto crypto = new CryptoLib.cCrypto();
                  int strLen = Str.Length;

                  // Convert hex string to a pure string
                  System.Text.StringBuilder sb = new StringBuilder();
                  for (int i = 1; i<strLen; i++)
                  {
                        sb.Append((char)byte.Parse(Str.SubString(i*2,2), NumberStyles.HexNumber));
                  }

                  return crypto.DecryptTripleDES(sb.ToString(),"");
          }
0
gnoonCommented:
The code above is using a custom class CryptoLib.cCrypto, and doesn't tell anything about decrypt problem.

You encrypt string and return it as a HEX string. Thus, you should transform the HEX back to original format before decrypt it.

string strEncryptedHex = HashPw("Plain text");
string strPlain = HashPwBck(strEncryptedHex);  <----- within HashPwBck needs to transform HEX string -> original format before decrypt it

Another thing is

String.Format("", Convert.ToByte(part))   always return empty string

Code below is snippet from here http://www.csharper.net/blog/library_encrypt_and_decrypt_methods_using_tripledes_and_md5.aspx.
You can use it or learn with it how to encrypt/decrypt using Triple DES.

Use it like this

string plain = "Secret plain text";
string key = "mypassword";

string strEnc = Encrypt(plain, key, true);
string strDec = Decrypt(strEnc, key, true);
public static string Encrypt(string toEncrypt, string key, bool useHashing)
{
    byte[] keyArray;
    byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
 
    if(useHashing)
    {
        MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
        keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
    }
    else
        keyArray = UTF8Encoding.UTF8.GetBytes(key);
 
    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    tdes.Key = keyArray;
    tdes.Mode = CipherMode.ECB;
    tdes.Padding = PaddingMode.PKCS7;
 
    ICryptoTransform cTransform = tdes.CreateEncryptor();
    byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
 
    return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
 
public static string Decrypt(string toDecrypt, string key, bool useHashing)
{
    byte[] keyArray;
    byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
 
    if(useHashing)
    {
        MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
        keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
    }
    else
        keyArray = UTF8Encoding.UTF8.GetBytes(key);
 
    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    tdes.Key = keyArray;
    tdes.Mode = CipherMode.ECB;
    tdes.Padding = PaddingMode.PKCS7;
 
    ICryptoTransform cTransform = tdes.CreateDecryptor();
    byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
 
    return UTF8Encoding.UTF8.GetString(resultArray);
}

Open in new window

0
dcassAuthor Commented:
Am no using a different kind of encryption as could not get hashing to work.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.