2

I am trying to send some data from Unity to Node.js express server and vice versa.

Here is my Unity code in C#:

public 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);      
    }

    var tdes = new TripleDESCryptoServiceProvider();
    tdes.Key = keyArray;
    //tdes.Mode = CipherMode.CBC;  // which is default     
    //tdes.Padding = PaddingMode.PKCS7;  // which is default

    ICryptoTransform cTransform = tdes.CreateEncryptor();     
    byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
    tdes.Clear();
    return Convert.ToBase64String(resultArray, 0, resultArray.Length); 

}

    Dictionary<string, string> data = new Dictionary<string, string>();
    data.Add("Test01", "Tanmoy");
    data.Add("Test02", "Mitra");
    string json = Json.Serialize(data);

    byte[] postData = Encoding.UTF8.GetBytes(Encrypt(json, "12345", true));

    StartCoroutine(HttpPost("test", postData, delegate(string requestError, IDictionary rData) {
        cb(null,rData);
    }));

And here is my Node.js code:

var alg = 'des-ede3-cbc';
var key = new Buffer('123456789012345678901234', 'utf-8');
var iv = new Buffer(m_strApiPass, 'base64');

var encrypted = new Buffer(reqString, 'base64');

var decipher = crypto.createDecipheriv(alg, key, iv);
var decoded = decipher.update(encrypted, 'binary', 'ascii');
decoded += decipher.final('ascii');

I am getting this error:

TypeError: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length

What I have noticed, in C#, I got following string,

pySqIdAiJpDOW7XkOQDoblLOtZ382J1G1F1UE16W0Ulg+x5X0Bocjg==

but when I send this string to Node.js, it becomes:

pySqIdAiJpDOW7XkOQDoblLOtZ382J1G1F1UE16W0Ulg x5X0Bocjg  

What could the issue be?

1
  • Doesn't have to do much with Unity3D in itself I guess. It's good that you document the defaults, but I would just set them. It is unlikely to cost you any performance, and relying on defaults in crypto is a dangerous game. Don't forget to add a MAC as this is vulnerable to padding oracle attacks. Commented Jan 21, 2014 at 11:57

1 Answer 1

2

The reason is simple: the returned ciphertext consists of random looking bytes to any observer. This means that there are bytes in there which do not map to a character. The information within those bytes is lost.

If you require a string during transmission, base 64 encode your ciphertext instead, and decode it again at the receiving side.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.