Windows 应用商店 8.1 AES 加密

本文关键字:AES 加密 应用 Windows | 更新日期: 2023-09-27 17:57:03

我是加密新手,我已经尝试了很多东西,但我的要求是这样的

key1 = CryptoJS.enc.Utf8.parse('abc');
ivKey = CryptoJS.enc.Utf8.parse('xyz');
pwdval = 'username';
function encyptionFun(pwdval) {
    var encyptedval = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(pwdval), key1,
        {
            keySize: 128 / 8,
            iv: ivKey,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
    encyptedval = encodeURIComponent(encyptedval);
    return encyptedval;
}

这是JavaScript代码,现在我必须转换为本机代码,就像我在C# AES算法中所做的那样。

我有我的代码,例如:

为它添加更多的算法。

 public static string Encrypt(string dataToEncrypt, string password, string salt)
    {
        // Generate a key and IV from the password and salt
        IBuffer aesKeyMaterial;
        IBuffer iv;
        uint iterationCount = 10000;
        GenerateKeyMaterial(password, salt, iterationCount, out aesKeyMaterial, out iv);
        IBuffer plainText = CryptographicBuffer.ConvertStringToBinary(dataToEncrypt, BinaryStringEncoding.Utf8);
        // Setup an AES key, using AES in CBC mode and applying PKCS#7 padding on the input
        SymmetricKeyAlgorithmProvider aesProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
        CryptographicKey aesKey = aesProvider.CreateSymmetricKey(aesKeyMaterial);
        // Encrypt the data and convert it to a Base64 string
        IBuffer encrypted = CryptographicEngine.Encrypt(aesKey, plainText, iv);
        return CryptographicBuffer.EncodeToBase64String(encrypted);
    }
    private static void GenerateKeyMaterial(string password, string salt, uint iterationCount, out IBuffer keyMaterial, out IBuffer iv)
    {
        // Setup KDF parameters for the desired salt and iteration count
        IBuffer saltBuffer = CryptographicBuffer.ConvertStringToBinary(salt, BinaryStringEncoding.Utf8);
        KeyDerivationParameters kdfParameters = KeyDerivationParameters.BuildForPbkdf2(saltBuffer, iterationCount);
        // Get a KDF provider for PBKDF2, and store the source password in a Cryptographic Key
        KeyDerivationAlgorithmProvider kdf = KeyDerivationAlgorithmProvider.OpenAlgorithm(KeyDerivationAlgorithmNames.Pbkdf2Sha256);
        IBuffer passwordBuffer = CryptographicBuffer.ConvertStringToBinary(password, BinaryStringEncoding.Utf8);
        CryptographicKey passwordSourceKey = kdf.CreateKey(passwordBuffer);
        // Generate key material from the source password, salt, and iteration count.  Only call DeriveKeyMaterial once,
        // since calling it twice will generate the same data for the key and IV.
        int keySize = 128 / 8;
        int ivSize = 128 / 8;
        uint totalDataNeeded = (uint)(keySize + ivSize);
        IBuffer keyAndIv = CryptographicEngine.DeriveKeyMaterial(passwordSourceKey, kdfParameters, totalDataNeeded);
        // Split the derived bytes into a seperate key and IV
        byte[] keyMaterialBytes = keyAndIv.ToArray();
        keyMaterial = WindowsRuntimeBuffer.Create(keyMaterialBytes, 0, keySize, keySize);
        iv = WindowsRuntimeBuffer.Create(keyMaterialBytes, keySize, ivSize, ivSize);
    }

Windows 应用商店 8.1 AES 加密

您的 JavaScript 代码使用 CBC 模式,而 C# 代码使用 ECB 模式(两个选项)。它们必须相同:最好是带有随机IV的CBC模式。

此外,C# 代码还会对密码进行哈希处理以获取密钥。这在 JavaScript 中不会发生(也许你只是忘记显示它)。