Я новый для шифрования я попытался со многими вещами, но мое требование какWindows Store 8.1 Шифрование AES
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);
}
Привет, Artjom, I javaScript «iv: ivKey», это строковый ключ, как бы я мог достигнуть в int keySize = 128/8; int ivSize = 128/8; uint totalDataNeeded = (uint) (keySize + ivSize); этот формат. –
Вместо того, чтобы добавлять больше кода, который вы нашли в Интернете, вы должны показать свой полный код CryptoJS. Я не знаю, что такое ivKey. Предполагаемый ключ также называется pwdval, что также неверно, поскольку ключи не являются паролями. –
Да, я скорректировал свой код, пожалуйста, проверьте сейчас. –