C# and Javascript Interchangeable Encryption / Decryption

105 Views Asked by At

I have an application that I'm developing that needs to be able to encrypt a string solely on the front end with a key. I can get the encryption and decryption working in the front end completely fine using CryptoJS. I've used this Gist as an example. https://gist.github.com/dudepare/99f4d682c3db90ea86a4331e32f0006a

The complication is that I need to be able to decrypt it on a server running C#. I am currently trying with the following code:

using System.Text;

namespace anApplication
{
    public class DecryptionService : IDecryptionService
    {
        public string DecryptString(string encryptedMessage, string key)
        {
            // Convert the key to bytes
            byte[] keyBytes = Encoding.UTF8.GetBytes(key);

            // Get the IV (first 16 bytes of the encrypted message)
            byte[] iv = Convert.FromBase64String(encryptedMessage).Take(16).ToArray();

            // Get the ciphertext (the rest of the encrypted message)
            byte[] ciphertext = Convert.FromBase64String(encryptedMessage).Skip(16).ToArray();

            // Create an AES decryptor
            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = keyBytes;
                aesAlg.IV = iv;

                // Create a decryptor to perform the stream transform
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for decryption
                using (MemoryStream msDecrypt = new MemoryStream(ciphertext))
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                {
                    // Read the decrypted bytes from the decrypting stream
                    return srDecrypt.ReadToEnd();
                }
            }
        }
    }
}

I have tried matching up all of the specific values for the IV, padding etc (including specifying the defaults to ensure they are all set correctly) but still cannot come back with the same value from both the front end encryption -> front end decryption and front end encryption -> back end decryption.

My question is not so much how to do it (although if someone has this working and can share that code then I'd happily receive that example and worship that individual as a deity) but more whether it is correct that multiple implementations of the same cryptographic algorithm should produce the same output values and be interchangeable for decryption (given that all of the variables are set the same obviously)?

Bonus points for suggestions on implementations that are compatible in C# and JS if the above answer is that they should be interchangeable and megapoints giveaway for someone who can share a working example of the three methods (JSEncryption, JSDecryption, C#Decryption)

Thanks in advance for any information.

0

There are 0 best solutions below