简体   繁体   English

将 JavaScript 解密重构为 C# - AES 128

[英]Refactor JavaScript decryption into C# - AES 128

I need to take working JavaScript that decrypts a message and convert it into C#.我需要使用 JavaScript 来解密消息并将其转换为 C#。 I have the decryption information (the "decrypt" variable below) which looks like: AES-128:<salt>:<iv>:<key> .我有解密信息(下面的“解密”变量),如下所示: AES-128:<salt>:<iv>:<key> Here's the JavaScript:这是 JavaScript:

function decodeString(message, decrypt) {
    var parts = decrypt.split(':', 4);
    var salt = CryptoJS.enc.Hex.parse(parts[1]);
    var iv = CryptoJS.enc.Hex.parse(parts[2]);
    var key = CryptoJS.PBKDF2(parts[3], salt, { keySize: 128/32, iterations: 100 });
    try {
        message = message.replace(/\s+/g, '');
        var d = CryptoJS.AES.decrypt(message, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
        message = d.toString(CryptoJS.enc.Utf8);
    } catch (e) {
        console.error("Encountered a problem decrypting and encrypted page!");
        console.log(e);
    }
    
    return(message);       
}

Here's what I have in C#, but I get an exception on the CreateDecryptor call.这是我在 C# 中的内容,但在 CreateDecryptor 调用中出现异常。

using System.Security.Cryptography;

private string DecodeString(string message, string decrypt)
{
    string[] parts = decrypt.ToString().Split(':');
    byte[] salt = Encoding.UTF8.GetBytes(parts[1]);
    byte[] iv = Encoding.UTF8.GetBytes(parts[2]);
    var pbkdf2 = new Rfc2898DeriveBytes(parts[3], salt, 100);
    int numKeyBytes = 128;  // Not sure this is correct
    byte[] key = pbkdf2.GetBytes(numKeyBytes);

    string plainText = null;
    using (AesManaged aes = new AesManaged())
    {
        aes.KeySize = numKeyBytes;  // Not sure if this is correct
        aes.BlockSize = 128;    // Defaults to 128, but not sure this is correct
        aes.Mode = CipherMode.CBC;
        aes.Padding = PaddingMode.PKCS7;

        try
        {
            // The below line has the following exception:
            //    The specified key is not a valid size for this algorithm.
            //    Parameter name: key
            using (var decrypter = aes.CreateDecryptor(key, iv))
            using (var plainTextStream = new MemoryStream())
            {
                using (var decrypterStream = new CryptoStream(plainTextStream, decrypter, CryptoStreamMode.Write))
                using (var binaryWriter = new BinaryWriter(decrypterStream))
                {
                    string encryptedText = Regex.Replace(message, @"\s+", "");
                    binaryWriter.Write(encryptedText);
                }
                byte[] plainTextBytes = plainTextStream.ToArray();
                plainText = Encoding.UTF8.GetString(plainTextBytes);
            }
        }
        catch (Exception ex)
        {
            log.Error("Unable to decrypt message.", ex);
        }

        return plainText;
    }
}

Any suggestions would be appreciated!任何建议,将不胜感激!

In the C# code there are the following issues:在 C# 代码中存在以下问题:

  • Salt and IV must be hex decoded (and not UTF8 encoded). Salt 和 IV 必须经过十六进制解码(而不是 UTF8 编码)。
  • numKeyBytes specifies the key size in bytes and is therefore 16 (and not 128) for AES-128. numKeyBytes以字节为单位指定密钥大小,因此对于 AES-128 为 16(而不是 128)。
  • aes.KeySize specifies the key size in bits and is therefore numKeyBytes * 8 (and not numKeyBytes ), but can alternatively be omitted. aes.KeySize以位为单位指定密钥大小,因此是numKeyBytes * 8 (而不是numKeyBytes ),但也可以省略。
  • For aes.BlockSize , aes.Mode and aes.Padding the default values are used (128, CBC, PKCS7), so they do not need to be specified explicitly.对于aes.BlockSizeaes.Modeaes.Padding使用默认值(128、CBC、PKCS7),因此不需要明确指定它们。
  • encryptedText must be Base64 decoded. encryptedText必须经过 Base64 解码。

A possible implementation is:一个可能的实现是:

private string Decrypt(string message, string decrypt)
{
    string[] parts = decrypt.ToString().Split(':');
    byte[] salt = StringToByteArray(parts[1]);                                                              // Hex decode salt                                            
    byte[] iv = StringToByteArray(parts[2]);                                                                // Hex dedoce IV                                           
    var pbkdf2 = new Rfc2898DeriveBytes(parts[3], salt, 100);
    int numKeyBytes = 16;                                                                                   // AES-128 key size in bytes: 16
    byte[] key = pbkdf2.GetBytes(numKeyBytes);

    string plainText = null;
    using (AesManaged aes = new AesManaged())
    {
        aes.KeySize = 192;
        try
        {
            string encryptedText = Regex.Replace(message, @"\s+", "");
            using (var decrypter = aes.CreateDecryptor(key, iv))
            using (MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(encryptedText)))      // Base64 decode ciphertext
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decrypter, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        plainText = srDecrypt.ReadToEnd();
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Unable to decrypt message.", ex);
        }

        return plainText;
    }
}

where StringToByteArray() is from here . StringToByteArray()来自这里

I changed the Stream part to be analogous to this example , but the original implementation works as well, so this change is optional.我将 Stream 部分更改为与此示例类似,但原始实现也可以,因此此更改是可选的。

Test: Both codes return for the data测试:两个代码都返回数据

message = "YhyXEjjNAnRUUONwVzlha59tRoWkeEwTkOtSKOicRd/iBKkGgIp+DeWmvEXxAU53";
decrypt = "AES-128:30313233343536373839303132333435:35343332313039383736353433323130:my passphrase";

the plaintext:明文:

The quick brown fox jumps over the lazy dog

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM