AES-GCM encryption in JavaScript, decrypted in C#

submited by
Style Pass
2021-05-20 18:00:10

If you must encrypt in the browser, JavaScript with Web Crypto API is an option. If you need to decrypt the results in a different language, it takes some time to find out how. Here’s one approach with AES-GCM.

Crypto is complex - there is always a lot to consider. Choosing the right technology for your current purpose can be challenging and requires a lot of know-how and experience in nearly every case.

Disclaimer: This article neither claims to show the right, nor the only way to do encryption across technology boundaries. It is just an example that should help you to understand one possible approach - you may adopt it to your needs.

The Web Crypto API is very powerful and fast enough to perform realtime file-encryption in the browser. However, in this example we are going to use symmetric text message encryption using Galois/Counter Mode (GCM) including a Nonce (also called IV) and a Tag for verification. Let’s dive right into the code:

You see that it is totally possible to encrypt in JavaScript and decrypt in other langauges (e.g. C#). The clue is to find out where the different parts of the encryption headers and trailers are being stored by different technologies (IV/Nonce, Tag, CipherData) - often you have to adjust or add them manually.

Leave a Comment