C# 中的 AES 加密和 JS 中使用 crypto-js 的解密

发布于 2025-01-14 22:53:05 字数 3824 浏览 0 评论 0原文

用例: 我正在开发一个 C# 服务,我必须将一个加密字符串发送到另一个系统,该系统已经使用 CryptoJS 在 JS 中编码,这将解密我要发送的字符串。 接收系统不会对其实现进行任何更改。他们共享了用于解密的 JS 代码以及我加密所需的凭据。

接收方加密和解密的JS代码

var message = "hello world!!!";

function generateKey(salt, passPhrase, keySize, iterationCount) {
const key = CryptoJS.PBKDF2(passPhrase, CryptoJS.enc.Hex.parse(salt), {
keySize: keySize / 32,
iterations: iterationCount
});
return key;
}

function Encrypt(cipherText) {
const salt = "DUMMYSALT-VALUE-of-Size-32CHARACTERS";
const iv ="DUMMYIV-VALUE-of-Size-32CHARACTERS";
const passPhrase = "SomeSecretPassPhrase";
const keySize = 128
const iterationCount = 1
const key = generateKey(salt, passPhrase, keySize, iterationCount);
const encrypted = CryptoJS.AES.encrypt(cipherText, key, {
iv: CryptoJS.enc.Hex.parse(iv)
});
return encrypted.ciphertext.toString();
}

function Decrypt(cipherText, pass = "SomeSecretPassPhrase") {
const salt = "DUMMYSALT-VALUE-of-Size-32CHARACTERS";
const iv ="DUMMYIV-VALUE-of-Size-32CHARACTERS";
const passPhrase = "SomeSecretPassPhrase";
const keySize = 128
const iterationCount = 1
const key = generateKey(salt, passPhrase, keySize, iterationCount);

const cipherParams = CryptoJS.lib.CipherParams.create({
ciphertext: CryptoJS.enc.Hex.parse(cipherText)
});
const decrypted = CryptoJS.AES.decrypt(cipherParams, key, {
iv: CryptoJS.enc.Hex.parse(iv)
});
return decrypted.toString(CryptoJS.enc.Utf8);
}


var encrypted = Encrypt(message);
//equivalent to CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(message), key);
var decrypted = Decrypt(encrypted);
                                                        

$('#1').text("Encrypted: "+encrypted.toString(CryptoJS.enc.Utf8));
$('#2').text("Decrypted: "+decrypted.toString(CryptoJS.enc.Utf8));

输出 加密:24a935f11a3e0b35dd6604cd7dbee292 解密:你好世界!!!

问题:当我尝试通过 C# 移植它并尝试使用下面的代码加密字符串时,我的加密值不同,而且 JS 代码也无法解密它。

我要加密的代码:

var MyKey = GenerateKey("DUMMYSALT-VALUE-of-Size-32CHARACTERS", "SomeSecretPassPhrase");
var EncryptedString = EncryptString(MyKey, "hello world!!!");


   public byte[] GenerateKey(string saltVal, string passphrase)
        {
            byte[] salt = ConvertHexStringToByteArray("DUMMYSALT-VALUE-of-Size-32CHARACTERS");
            int iterations = 1;
            var rfc2898 = new Rfc2898DeriveBytes(passphrase, salt, iterations);
            byte[] key = rfc2898.GetBytes(16);
            
            return key;
        }

        public string EncryptString(byte[] key, string stringToEncrypt )
        {
            AesManaged aesCipher = new AesManaged();
            aesCipher.KeySize = 128;
            aesCipher.BlockSize = 128;
            aesCipher.Mode = CipherMode.CBC;
            aesCipher.Padding = PaddingMode.PKCS7;
            aesCipher.Key = key;
            aesCipher.IV = ConvertHexStringToByteArray("DUMMYPRESETIV-VALUE-of-Size-32CHARACTERS");
            byte[] b = System.Text.Encoding.UTF8.GetBytes(stringToEncrypt);
            ICryptoTransform encryptTransform = aesCipher.CreateEncryptor();
            byte[] ctext = encryptTransform.TransformFinalBlock(b, 0, b.Length);
            System.Console.WriteLine("IV:" + Convert.ToBase64String(aesCipher.IV));           
            System.Console.WriteLine("Cipher text: " + Convert.ToBase64String(ctext));
            return Convert.ToBase64String(ctext);
        }

        public byte[] ConvertHexStringToByteArray(string hexString)
        {
            int NumberChars = hexString.Length;
            byte[] bytes = new byte[NumberChars / 2];
            for (int i = 0; i < NumberChars; i += 2)
                bytes[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16);
            return bytes;
        }

我不确定我是否执行了与 JS 方法 CryptoJS.enc.Hex.parse() 等效的正确操作 ,

UseCase:
I am developing a service that is in C# and I have to send an encrypted string to a different system that is already coded in JS using CryptoJS which will be decrypting the string I am going to be sending.
The receiving system is not going to make any changes to its implementation. They have shared the JS code they are using to DECRYPT along with the required credentials that I would need for my encryption.

JS code of both Encrypt and Decrypt from the receiver

var message = "hello world!!!";

function generateKey(salt, passPhrase, keySize, iterationCount) {
const key = CryptoJS.PBKDF2(passPhrase, CryptoJS.enc.Hex.parse(salt), {
keySize: keySize / 32,
iterations: iterationCount
});
return key;
}

function Encrypt(cipherText) {
const salt = "DUMMYSALT-VALUE-of-Size-32CHARACTERS";
const iv ="DUMMYIV-VALUE-of-Size-32CHARACTERS";
const passPhrase = "SomeSecretPassPhrase";
const keySize = 128
const iterationCount = 1
const key = generateKey(salt, passPhrase, keySize, iterationCount);
const encrypted = CryptoJS.AES.encrypt(cipherText, key, {
iv: CryptoJS.enc.Hex.parse(iv)
});
return encrypted.ciphertext.toString();
}

function Decrypt(cipherText, pass = "SomeSecretPassPhrase") {
const salt = "DUMMYSALT-VALUE-of-Size-32CHARACTERS";
const iv ="DUMMYIV-VALUE-of-Size-32CHARACTERS";
const passPhrase = "SomeSecretPassPhrase";
const keySize = 128
const iterationCount = 1
const key = generateKey(salt, passPhrase, keySize, iterationCount);

const cipherParams = CryptoJS.lib.CipherParams.create({
ciphertext: CryptoJS.enc.Hex.parse(cipherText)
});
const decrypted = CryptoJS.AES.decrypt(cipherParams, key, {
iv: CryptoJS.enc.Hex.parse(iv)
});
return decrypted.toString(CryptoJS.enc.Utf8);
}


var encrypted = Encrypt(message);
//equivalent to CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(message), key);
var decrypted = Decrypt(encrypted);
                                                        

$('#1').text("Encrypted: "+encrypted.toString(CryptoJS.enc.Utf8));
$('#2').text("Decrypted: "+decrypted.toString(CryptoJS.enc.Utf8));

Output
Encrypted: 24a935f11a3e0b35dd6604cd7dbee292
Decrypted: hello world!!!

Question: While I try to port this over the C# and try to encrypt a string using the below, my encrypted value is different and also the JS code is unable to decrypt it.

my code to encrypt:

var MyKey = GenerateKey("DUMMYSALT-VALUE-of-Size-32CHARACTERS", "SomeSecretPassPhrase");
var EncryptedString = EncryptString(MyKey, "hello world!!!");


   public byte[] GenerateKey(string saltVal, string passphrase)
        {
            byte[] salt = ConvertHexStringToByteArray("DUMMYSALT-VALUE-of-Size-32CHARACTERS");
            int iterations = 1;
            var rfc2898 = new Rfc2898DeriveBytes(passphrase, salt, iterations);
            byte[] key = rfc2898.GetBytes(16);
            
            return key;
        }

        public string EncryptString(byte[] key, string stringToEncrypt )
        {
            AesManaged aesCipher = new AesManaged();
            aesCipher.KeySize = 128;
            aesCipher.BlockSize = 128;
            aesCipher.Mode = CipherMode.CBC;
            aesCipher.Padding = PaddingMode.PKCS7;
            aesCipher.Key = key;
            aesCipher.IV = ConvertHexStringToByteArray("DUMMYPRESETIV-VALUE-of-Size-32CHARACTERS");
            byte[] b = System.Text.Encoding.UTF8.GetBytes(stringToEncrypt);
            ICryptoTransform encryptTransform = aesCipher.CreateEncryptor();
            byte[] ctext = encryptTransform.TransformFinalBlock(b, 0, b.Length);
            System.Console.WriteLine("IV:" + Convert.ToBase64String(aesCipher.IV));           
            System.Console.WriteLine("Cipher text: " + Convert.ToBase64String(ctext));
            return Convert.ToBase64String(ctext);
        }

        public byte[] ConvertHexStringToByteArray(string hexString)
        {
            int NumberChars = hexString.Length;
            byte[] bytes = new byte[NumberChars / 2];
            for (int i = 0; i < NumberChars; i += 2)
                bytes[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16);
            return bytes;
        }

I am not sure if am doing the correct equivalent of the JS method CryptoJS.enc.Hex.parse()
,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文