Forum: Ruby Triple DES In Ruby

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
10fdfc5ff7145271176a3321827afde9?d=identicon&s=25 Srinivas M. (srinivas_m)
on 2017-03-17 10:16
With the help of
article, we have implemented the triple des encryption in C#.
Implementation is as follows.

public static void Main()
    string key = "SecretKey";
    string data = "Test Data";

    byte[] keyArray;
    byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(data);

    //If hashing use get hashcode regards to your key
    MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
    keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));

    //Always release the resources and flush data
    // of the Cryptographic service provide. Best Practice

    TripleDESCryptoServiceProvider tdes = new

    //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

    string final_result = Convert.ToBase64String(resultArray, 0,

    //Printing the encrypted data into unreadable string format


Now We need to implement the same kind of encryption in Ruby. Below is
the code we have tried.

def triple_des_xml_payload(data)
    # Get xml payload data and apply triple-des algorithm on it and
convert it to base64
    md5 = Digest::MD5.hexdigest(@private_key) # Create a md5 Hash

    cipher ='DES-EDE3')
    cipher.key = md5
    output = cipher.update(data)
    output <<

    encrypted_data = Base64.encode64(output)

    puts encrypted_data

    return encrypted_data

Ruby implementation is giving different results.

Can someone help to make this work properly in Ruby as in C#.
This topic is locked and can not be replied to.