• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 611
  • Last Modified:

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.
0
dcass
Asked:
dcass
  • 2
1 Solution
 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now