0

I have a sample code in .NET C# which I need to convert to RUBY.

 public static string Encrypt(string pstrText)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(pstrText);
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(secKey));
                hashmd5.Clear();
            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);
            tdes.Clear();
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

I have tried using OPENSSL:CIPHER and got thus far -

def encrypt data
secret = "******************"
md5 = Digest::MD5.hexdigest(secret)
des = OpenSSL::Cipher::Cipher.new 'DES-EDE3'
des.encrypt
des.key = md5
update_value = des.update(data)
up_final = update_value + des.final
puts Base64.encode64(up_final).gsub(/\n/, "")
end

The results in C# and RUBY do not match. Where am I going wrong?

Rajat Suneja
  • 494
  • 2
  • 10
  • 27
  • Could you provide both common inputs and different outputs for both methods to help understand what is the expected behaviour? From what I see in the code, PKCS7 is not configured in the Ruby version of your implementation, maybe try specifying padding. – wteuber Aug 22 '18 at 21:36
  • Common Input - "Hello" C# Output - "7dFl8i/7XvA=" Ruby Output - "yI4i1E49dAQ=" – Rajat Suneja Aug 23 '18 at 07:56
  • Hm, I get `"tTTahnSXXW8="` when running your code with `data = "Hello"` and `secret = "******************"`. Have you maybe used another secret? Also, Ruby uses PKCS7 padding by default, so your issue should not be related to that. – wteuber Aug 23 '18 at 08:45
  • Sorry, correction - Common Input - "Hello" C# Output - "w4yjgRJriP4=" Ruby Output - "tTTahnSXXW8=" . And I know that the C# output is correct. – Rajat Suneja Aug 23 '18 at 09:11
  • Ok, I think all configuration looks right. So you will need to do some debugging. I got curious in `Digest::MD5.hexdigest(secret)` (`"28306617aacbf026bf9a4e0783b50ff9"`) vs. `hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(secKey))` (Could you post this line's return value here?) – wteuber Aug 24 '18 at 08:01
  • Ok, I got the C# value... `Digest::MD5.hexdigest(secret)` is `"28306617aacbf026bf9a4e0783b50ff9"` (which gives you `[50, 56, 51, 48, 54, 54, 49, 55, 97, 97, 99, 98, 102, 48, 50, 54, 98, 102, 57, 97, 52, 101, 48, 55, 56, 51, 98, 53, 48, 102, 102, 57]` when calling `.bytes.to_a`) but `hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(secKey))` gives you `{ 40, 48, 102, 23, 170, 203, 240, 38, 191, 154, 78, 7, 131, 181, 15, 249 }`. That looks very different to me. It's probably worth digging deeper here. – wteuber Aug 24 '18 at 08:14
  • You are correct about the RUBY Values, but the interesting thing is, doing a BitConverter.ToString on hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(secKey)) gives me - "28-30-66-17-AA-CB-F0-26-BF-9A-4E-07-83-B5-0F-F9" which is same as given by Digest::MD5.hexdigest(secret) in RUBY – Rajat Suneja Aug 24 '18 at 09:54
  • Hm, that's confusing. I'm afraid I won't be able to figure this out. Hopefully someone else will be able to help you. – wteuber Aug 25 '18 at 14:01

0 Answers0