Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat would this C# crypto code look like in NodeJS?
    primarykey
    data
    text
    <p><a href="https://stackoverflow.com/questions/2467419/tripledes-encryption-not-yielding-same-results-in-php-and-c-sharp">tripledes encryption not yielding same results in PHP and C#</a></p> <pre><code> 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); } </code></pre> <p>The above post and code has a bit of pretty straight forward (or so I thought) logic to encrypt/decrypt a string using 3DES and a private key.</p> <p>I've found a few examples of how to implement something similar in nodejs using the crypt library but everything I've tried thus far has produced garbage (and rightfully so).</p> <p>Thoughts?</p> <p><strong>UPDATE1:</strong></p> <p>Here is a bit of C# code to generate the data I'm working with:</p> <pre><code> String key = "abcdefghijklmnop"; String text = "12345"; String encrypted = Encrypt(text, key, false); //Returns "QI3I65+aWSk=" </code></pre> <p>And here's the latest revision of the nodejs code I was working with:</p> <pre><code> var crypto = require('crypto'); var key = 'abcdefghijklmnop'; var encrypted = 'QI3I65+aWSk='; var expected = '12345'; var algs = [ 'des3', 'des-ede', 'des-ede3', 'des-ecb', 'aes-128-ecb']; for(var i in algs) { var decipher = crypto.createDecipher(algs[i], key); var result = '' result += decipher.update(encrypted, 'hex', 'binary'); result += decipher.final('binary'); console.log('Algorithm: ' + algs[i] + ', Matched Expected: ' + (result === expected)); } </code></pre> <p>.. which returns</p> <pre><code> Algorithm: des3, Matched Expected: false Algorithm: des-ede, Matched Expected: false Algorithm: des-ede3, Matched Expected: false Algorithm: des-ecb, Matched Expected: false Algorithm: aes-128-ecb, Matched Expected: false </code></pre> <p>I wasn't clear as to exactly which algorithm to use (my list was significantly longer in previous attempts) now was I sure as to what encoding combination (binary/hex) to use.</p> <p>Thanks again.</p> <p><strong>UPDATE2:</strong> Copied over the Encrypt method from the referenced post:</p> <p><a href="https://stackoverflow.com/questions/2467419/tripledes-encryption-not-yielding-same-results-in-php-and-c-sharp">tripledes encryption not yielding same results in PHP and C#</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload