簡體   English   中英

Windows Store 8.1 AES加密

[英]Windows Store 8.1 AES Encryption

我是加密的新手,我嘗試了很多事情,但是我的要求是

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);
    }

您的JavaScript代碼使用CBC模式,而您的C#代碼使用ECB模式(兩個選項)。 它們必須相同:最好是具有隨機IV的CBC模式。

同樣,您的C#代碼會散列密碼以獲取密鑰。 這在JavaScript中不會發生(也許您只是忘記顯示它了)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM