使用JavaScript解密C# AES加密字符串的技术问询
Decrypt C# AES-Encrypted String in JavaScript
First, let's recap the key details from your C# encryption implementation and map them to a compatible JavaScript solution:
C# Encryption Code (Recap)
public class AesCrypt { public static string IV = @"!QAZ2WSX#EDC4RFV"; public static string Key = @"5TGB&YHN7UJM(IK<5TGB&YHN7UJM(IK<"; public static string Encrypt(string decrypted) { byte[] textbytes = ASCIIEncoding.ASCII.GetBytes(decrypted); AesCryptoServiceProvider encdec = new AesCryptoServiceProvider(); encdec.BlockSize = 128; encdec.KeySize = 256; encdec.Key = ASCIIEncoding.ASCII.GetBytes(Key); encdec.IV = ASCIIEncoding.ASCII.GetBytes(IV); // Default settings: CBC mode, PKCS7 padding // (Rest of encryption logic would convert ciphertext to Base64 string) } }
Critical Parameters to Match in JavaScript
- Algorithm: AES-CBC (default for
AesCryptoServiceProvider) - Key Size: 256 bits (32 bytes, matches your ASCII-encoded Key string)
- IV: 128 bits (16 bytes, matches your ASCII-encoded IV string)
- Padding: PKCS7 (default padding scheme for C#'s AES implementation)
- Encoding: ASCII (used for all string-to-byte conversions in your C# code)
- Encrypted Format: Base64 (standard way to return ciphertext as a string in C#)
JavaScript Decryption Implementation
We'll use the browser's Web Crypto API (secure, standard, and widely supported) to mirror the C# behavior:
async function decryptAesCbc(encryptedBase64) { // Convert Base64 ciphertext to ArrayBuffer const encryptedBuffer = base64ToArrayBuffer(encryptedBase64); // Convert ASCII key and IV to ArrayBuffers const keyBuffer = stringToArrayBuffer('5TGB&YHN7UJM(IK<5TGB&YHN7UJM(IK<', 'ascii'); const ivBuffer = stringToArrayBuffer('!QAZ2WSX#EDC4RFV', 'ascii'); // Import the AES key for decryption const key = await crypto.subtle.importKey( 'raw', keyBuffer, { name: 'AES-CBC', length: 256 }, false, ['decrypt'] ); // Perform decryption const decryptedBuffer = await crypto.subtle.decrypt( { name: 'AES-CBC', iv: ivBuffer }, key, encryptedBuffer ); // Convert decrypted buffer back to ASCII string return arrayBufferToString(decryptedBuffer, 'ascii'); } // Helper conversion functions function base64ToArrayBuffer(base64) { const binary = atob(base64); const bytes = new Uint8Array(binary.length); for (let i = 0; i < binary.length; i++) { bytes[i] = binary.charCodeAt(i); } return bytes.buffer; } function stringToArrayBuffer(str, encoding = 'utf8') { const encoder = new TextEncoder(encoding); return encoder.encode(str).buffer; } function arrayBufferToString(buffer, encoding = 'utf8') { const decoder = new TextDecoder(encoding); return decoder.decode(buffer); }
How to Use
// Replace with your actual encrypted Base64 string const encryptedString = 'YOUR_ENCRYPTED_VALUE_HERE'; decryptAesCbc(encryptedString) .then(decryptedText => console.log('Decrypted result:', decryptedText)) .catch(err => console.error('Decryption failed:', err));
Important Notes
- For Node.js environments, use
crypto.webcrypto(modern versions) instead of the browser'scryptoobject. - Your C# code uses a static IV, which is not secure for production. In real-world scenarios, generate a random IV per encryption and prepend it to the ciphertext (then extract it before decryption in JS).
- Double-check that your C# encryption code actually converts the ciphertext to Base64 (this is the standard approach, but confirm if it's what you're doing).
内容的提问来源于stack exchange,提问作者riv
相关产品推荐
相关产品推荐

