Triple DES (DES 3)

61 views Asked by At

I'm trying to do an encrypt using 3DES the value from MD5 and key but the result always different than the online tool like this tool ( Online Domain Tools ) and always the length bigger than 32 characters so what the problem

Note: Value from md5 and key should be in HEX

public static class MacInGenerator
        {
            public static string GenerateMacIn(string customerId, string refid, string pAmount)
            {
                string macInInput = customerId + refid + pAmount;
                //log("macInInput ----> " + macInInput);

                string digitalSignature = ComputeMD5Hash(macInInput);
                //log("digitalSignature ----> " + digitalSignature);

                string encryptedMacIn = Encrypt3DES(digitalSignature, "key of 32 char and digit");
                //log("encryptedMacIn ----> " + encryptedMacIn);

                return encryptedMacIn;
            }


            static string ComputeMD5Hash(string input)
            {
                using (MD5 md5 = MD5.Create())
                {
                    byte[] inputBytes = Encoding.UTF8.GetBytes(input);
                    byte[] hashBytes = md5.ComputeHash(inputBytes);

                    StringBuilder stringBuilder = new StringBuilder();
                    for (int i = 0; i < hashBytes.Length; i++)
                    {
                        stringBuilder.Append(hashBytes[i].ToString("x2")); // Convert to hexadecimal
                    }

                    return stringBuilder.ToString();
                }
            }
            static string Encrypt3DES(string data, string keyHex)
            {
                using (TripleDESCryptoServiceProvider des3 = new TripleDESCryptoServiceProvider())
                {
                    des3.Key = Encoding.ASCII.GetBytes(keyHex);
                    des3.Mode = CipherMode.ECB;
                    des3.Padding = PaddingMode.None;

                    ICryptoTransform encryptor = des3.CreateEncryptor();
                    byte[] inputBytes = Encoding.ASCII.GetBytes(data);
                    byte[] encryptedBytes = encryptor.TransformFinalBlock(inputBytes, 0, inputBytes.Length);
                    var result = BitConverter.ToString(encryptedBytes).Replace("-", "").ToLower();
                    return result;
                }
            }
        }
0

There are 0 answers