Here's the equivalent code in C#:
using System;
using System.Security.Cryptography;
using System.Text;
namespace CryptoExample
{
public class Program
{
private static readonly int IV_LENGTH = 16;
private static readonly string serviceKey = "36dc4fc036dc4fc036dc4fc09635c998af336dc36dc4fc053c126f34ef4d62ecaab3eb0";
static void Main(string[] args)
{
var tokenBody = new
{
access_token = "",
iat = DateTimeOffset.Now.ToUnixTimeSeconds(),
exp = DateTimeOffset.Now.AddHours(1).ToUnixTimeSeconds(),
subject = "login_with_eljour",
payload = new { is_child = 0, ip = "123.123.123,123" }
};
string encrypted = Encrypt(JsonSerialize(tokenBody), serviceKey);
Console.WriteLine($"encrypted: {encrypted}");
Console.WriteLine($"decrypted: {Decrypt(encrypted, serviceKey)}");
}
static string Encrypt(string text, string key)
{
var encryptKeyAndSignKey = PrepareKeys(key);
var iv = new byte[IV_LENGTH];
using (var rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(iv);
}
using (var aes = new AesCryptoServiceProvider())
{
aes.Key = encryptKeyAndSignKey.Item1;
aes.IV = iv;
aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.CBC;
using (var encryptor = aes.CreateEncryptor())
{
byte[] encryptedBytes = null;
using (var ms = new System.IO.MemoryStream())
{
using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
byte[] textBytes = Encoding.UTF8.GetBytes(text);
cs.Write(textBytes, 0, textBytes.Length);
cs.FlushFinalBlock();
encryptedBytes = ms.ToArray();
}
}
var signature = Hmac(encryptedBytes, encryptKeyAndSignKey.Item2);
byte[] combinedBytes = new byte[signature.Length + iv.Length + encryptedBytes.Length];
Buffer.BlockCopy(signature, 0, combinedBytes, 0, signature.Length);
Buffer.BlockCopy(iv, 0, combinedBytes, signature.Length, iv.Length);
Buffer.BlockCopy(encryptedBytes, 0, combinedBytes, signature.Length + iv.Length, encryptedBytes.Length);
return Convert.ToBase64String(combinedBytes);
}
}
}
static string Decrypt(string text, string key)
{
var encryptKeyAndSignKey = PrepareKeys(key);
byte[] encryptedBytesWithSignature = Convert.FromBase64String(text);
var signature = new byte[32];
var iv = new byte[IV_LENGTH];
var encryptedText = new byte[encryptedBytesWithSignature.Length - signature.Length - iv.Length];
Buffer.BlockCopy(encryptedBytesWithSignature, 0, signature, 0, signature.Length);
Buffer.BlockCopy(encryptedBytesWithSignature, signature.Length, iv, 0, iv.Length);
Buffer.BlockCopy(encryptedBytesWithSignature, signature.Length + iv.Length, encryptedText, 0, encryptedText.Length);
if (!BufferEqual(Hmac(encryptedText, encryptKeyAndSignKey.Item2), signature))
{
throw new Exception("Signature does not match");
}
using (var aes = new AesCryptoServiceProvider())
{
aes.Key = encryptKeyAndSignKey.Item1;
aes.IV = iv;
aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.CBC;
using (var decryptor = aes.CreateDecryptor())
{
byte[] decryptedBytes = null;
using (var ms = new System.IO.MemoryStream())
{
using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
{
cs.Write(encryptedText, 0, encryptedText.Length);
cs.FlushFinalBlock();
decryptedBytes = ms.ToArray();
}
}
return Encoding.UTF8.GetString(decryptedBytes);
}
}
}
static Tuple<byte[], byte[]> PrepareKeys(string key)
{
using (SHA512 sha512 = SHA512.Create())
{
byte[] hashBytes = sha512.ComputeHash(Encoding.UTF8.GetBytes(key));
return Tuple.Create(hashBytes[..32], hashBytes[32..]);
}
}
static byte[] Hmac(byte[] data, byte[] key)
{
using (HMACSHA256 hmacSha256 = new HMACSHA256(key))
{
return hmacSha256.ComputeHash(data);
}
}
static bool BufferEqual(byte[] a, byte[] b)
{
if (a.Length != b.Length)
return false;
for (int i = 0; i < a.Length; i++)
{
if (a[i] != b[i])
return false;
}
return true;
}
static string JsonSerialize(object obj)
{
return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
}
}
}
Note: Make sure to add the necessary using statements and properly handle the dependencies (Newtonsoft.Json and System.Security.Cryptography).