I'm not good at cryptography.I received this code used on server rest for cripting a string and I need to replicate it on my blazor wasm pwa.
the key, for example: key = "4fh!31ay*36S#@w!$%";
public string PasswordEncrypt(string inText, string key){ byte[] bytesBuff = Encoding.Unicode.GetBytes(inText); using (Aes aes = Aes.Create()) { var crypto = new Rfc2898DeriveBytes(key, new byte[] { 0x43, 0x71, 0x61, 0x6E, 0x20, 0x4D, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); aes.Key = crypto.GetBytes(32); aes.IV = crypto.GetBytes(16); using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write)) { cStream.Write(bytesBuff, 0, bytesBuff.Length); cStream.Close(); } inText = Convert.ToBase64String(mStream.ToArray()); } } return inText;}But on blazor wasm pwa the Aes and the Rfc2898DeriveBytes not are implemented. So I need to translate using javascript or the Crypto library or wathever.
I tryed to do with javascript this but I don't receive the same result.
// wwwroot/js/cryptoHelper.jsasync function EncryptText(inText, key) { const encoder = new TextEncoder(); const data = encoder.encode(inText); const salt = new Uint8Array([0x43, 0x71, 0x61, 0x6E, 0x20, 0x4D, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76]); const iterations = 1000; const keyMaterial = await window.crypto.subtle.importKey('raw', encoder.encode(key), { name: 'PBKDF2' }, false, ['deriveKey'] ); const derivedKey = await window.crypto.subtle.deriveKey( { name: 'PBKDF2', salt: salt, iterations: iterations, hash: 'SHA-256' }, keyMaterial, { name: 'AES-CBC', length: 256 }, false, ['encrypt'] ); const iv = new Uint8Array(16); window.crypto.getRandomValues(iv); const encrypted = await window.crypto.subtle.encrypt( { name: 'AES-CBC', iv: iv }, derivedKey, data ); const encryptedArray = new Uint8Array(encrypted); const ivBase64 = btoa(String.fromCharCode(...iv)); const encryptedBase64 = btoa(String.fromCharCode(...encryptedArray)); return `${ivBase64}:${encryptedBase64}`;}What I'm making wrong?Thanks