将 C# AES 加密转换为 CryptoJS

问题描述 投票:0回答:1

我有以下 C# 代码,我需要使用 CryptoJS 复制代码。然而我得到了不同的结果。

C#:

private static byte[] IV = {58, 88, 123, 111, 98, 109, 112, 54, 35, 13, 63, 53, 234, 55, 44, 41};
private static string key = "MY_PRIVATE_KEY";

public static string Encrypt(string clearText)
{
    string encryptedText = "";

    byte[] clearBytes = Encoding.UTF8.GetBytes(clearText);
    byte[] keyBytes = Encoding.UTF8.GetBytes(key);

    using (Aes encryptor = Aes.Create())
    {
        encryptor.Key = keyBytes;
        encryptor.IV = IV;

        using (MemoryStream ms = new MemoryStream())
        {
            using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
            {
                cs.Write(clearBytes, 0, clearBytes.Length);
                cs.Close();
            }
            encryptedText = Convert.ToBase64String(ms.ToArray());
        }
    }
    return encryptedText;
}

JS:

var CryptoJS = require("crypto-js");

var iv = CryptoJS.lib.WordArray.create([58, 88, 123, 111, 98, 109, 112, 54, 35, 13, 63, 53, 234, 55, 44, 41]);
var key = "MY_PRIVATE_KEY";

function encrypt(clearText) {

    var clearBytes = CryptoJS.enc.Utf8.parse(clearText);
    var keyBytes = CryptoJS.enc.Utf8.parse(key);

    var encryptedText = CryptoJS.AES.encrypt(
        clearBytes, 
        keyBytes, 
        { iv: iv }
    ).toString();

    return encryptedText;
}

我已经尝试了我能想到的所有变体。我的 JS 中缺少什么才能使其与 C# 匹配?调用每个函数的值都不相同。

(是的,我知道对每个请求使用相同的 IV 并不是一个好主意 - 我接下来会处理这个问题)。

javascript c# aes cryptojs
1个回答
0
投票

对于c#中的端口加密js,您可以按照以下方式完成

公共静态字符串 OpenSSLEncrypt(字符串纯文本,字符串密码){

  // generate salt

        byte[] key, iv;

        byte[] salt = new byte[8];

        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

        rng.GetNonZeroBytes(salt);

        DeriveKeyAndIV(passphrase, salt, out key, out iv);

        // encrypt bytes

        byte[] encryptedBytes = EncryptStringToBytesAes(plainText, key, iv);

        // add salt as first 8 bytes

        byte[] encryptedBytesWithSalt = new byte[salt.Length + encryptedBytes.Length + 8];

        Buffer.BlockCopy(Encoding.ASCII.GetBytes("Salted__"), 0, encryptedBytesWithSalt, 0, 8);

        Buffer.BlockCopy(salt, 0, encryptedBytesWithSalt, 8, salt.Length);

        Buffer.BlockCopy(encryptedBytes, 0, encryptedBytesWithSalt, salt.Length + 8, encryptedBytes.Length);

        // base64 encode

        return Convert.ToBase64String(encryptedBytesWithSalt);

    }

采用第二种方法

公共静态字符串 OpenSSLDecrypt(加密字符串,字符串密码)

    {

        // base 64 decode

        byte[] encryptedBytesWithSalt = Convert.FromBase64String(encrypted);

        // extract salt (first 8 bytes of encrypted)

        byte[] salt = new byte[8];

        byte[] encryptedBytes = new byte[encryptedBytesWithSalt.Length - salt.Length - 8];

        Buffer.BlockCopy(encryptedBytesWithSalt, 8, salt, 0, salt.Length);

        Buffer.BlockCopy(encryptedBytesWithSalt, salt.Length + 8, encryptedBytes, 0, encryptedBytes.Length);

        // get key and iv

        byte[] key, iv;

        DeriveKeyAndIV(passphrase, salt, out key, out iv);

        return DecryptStringFromBytesAes(encryptedBytes, key, iv);

    }

我证明了 C# 中加密 js 端口的完整过程,请参阅此链接

类似于c Sharp的加密js

© www.soinside.com 2019 - 2024. All rights reserved.