使用Crypto-JS的C#返回不同的结果

时间:2019-03-04 09:49:26

标签: c# encryption cryptography aes cryptojs

我正在尝试在JS中加密,并且也使用AesManaged C#类和Crypto-JS JS库在C#中进行相同的加密。应该是使用AES-256。

结果变得截然不同,花了数小时试图找出可能出了什么问题,我也看过类似的问题,但是在弄清楚我的代码的具体问题时根本没有运气...

任何想法代码可能有什么问题吗?

JS代码如下:

    // Function to encrypt
function encrypt(text) {
    var iv = CryptoJS.enc.Utf8.parse(encryption_key);
    var cipherText = CryptoJS.AES.encrypt(text, encryption_key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    }).toString();

    return cipherText;
}

// Function to decrypt
function decrypt(cipherText) {
    var iv = CryptoJS.enc.Utf8.parse(encryption_key);
    var bytes = CryptoJS.AES.decrypt(cipherText, encryption_key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });
    var originalText = bytes.toString(CryptoJS.enc.Utf8);

    return originalText;
}

C#代码如下:

public static string EncryptData(string text)
    {
        // Create a new instance of the AesManaged
        // class.  This generates a new key and initialization 
        // vector (IV).
        using (AesManaged myAes = new AesManaged())
        {
            myAes.BlockSize = 128;
            myAes.Key = Encoding.UTF8.GetBytes(ENCRYPTION_KEY);
            myAes.IV = Encoding.UTF8.GetBytes(ENCRYPTION_KEY);
            myAes.Mode = CipherMode.CBC;
            myAes.Padding = PaddingMode.PKCS7;

            // Encrypt the string to an array of bytes.
            byte[] encrypted = EncryptStringToBytes_Aes(text, myAes);

            return Convert.ToBase64String(encrypted, 0, encrypted.Length);
        }
    }

    private static byte[] EncryptStringToBytes_Aes(string plainText, AesManaged aesManaged)
    {
        byte[] encrypted;

        // Create an encryptor to perform the stream transform.
        ICryptoTransform encryptor = aesManaged.CreateEncryptor(aesManaged.Key, aesManaged.IV);

        // Create the streams used for encryption.
        using (MemoryStream msEncrypt = new MemoryStream())
        {
            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                {
                    //Write all data to the stream.
                    swEncrypt.Write(plainText);
                }
                encrypted = msEncrypt.ToArray();
            }
        }

        // Return the encrypted bytes from the memory stream.
        return encrypted;
    }

    private static string DecryptStringFromBytes_Aes(byte[] cipherText, AesManaged aesManaged)
    {
        // Declare the string used to hold
        // the decrypted text.
        string plaintext = null;

        // Create a decryptor to perform the stream transformaesManaged
        ICryptoTransform decryptor = aesManaged.CreateDecryptor(aesManaged.Key, aesManaged.IV);

        // Create the streams used for decryption.
        using (MemoryStream msDecrypt = new MemoryStream(cipherText))
        {
            using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
            {
                using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                {
                    // Read the decrypted bytes from the decrypting stream
                    // and place them in a string.
                    plaintext = srDecrypt.ReadToEnd();
                }
            }
        }

        return plaintext;
    }

0 个答案:

没有答案