我对整个加密/解密cr * p有一点问题; - )
是否可以解密在.NET桌面应用程序中加密的WinRt(Windows应用商店应用)中的数据,反之亦然?我无法更改桌面应用程序的代码,因为它已在使用中。
我已经在WinRT中尝试了一些CryptographicEngine的教程,但我从来没有得到与桌面应用程序相匹配的结果。
也许有人可以帮助我?我是.NET开发的新手,我从来没有真正对加密做任何事情所以我不知道我在做什么; - )
以下是桌面应用程序中使用的一些代码 - 我无法更改该代码!
private string pwd = "password";
private string salt = "salt";
public byte[] Encrypt(byte[] data)
{
PasswordDeriveBytes derivedPassword = new PasswordDeriveBytes(pwd, Encoding.ASCII.GetBytes(salt));
byte[] key = derivedPassword.GetBytes(16);
byte[] iv = Encoding.ASCII.GetBytes("1234567891234567");
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
byte[] cipherBytes = null;
using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(key, iv))
{
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();
cryptoStream.Close();
cipherBytes = ms.ToArray();
ms.Close();
}
}
}
symmetricKey.Clear();
return cipherBytes;
}
这是我在WinRT中尝试过的示例方法 - 但结果与桌面应用程序的结果不同 (大多数代码来自http://blog.lordinaire.fr/2012/12/winrt-encryption-and-decryption-with-aes-algorithm/)
删除了无法运行的代码 - 请参阅编辑
我真的会提供一些帮助
来自奥地利的问候
修改 我尝试了一些东西,但我仍然无法正常工作:( 我只是无法得到正确的钥匙。尽管如此,为了测试加密,我对密钥进行了硬编码 - 然后就可以了。
就像Nate Diamond建议的那样,我使用了KeyDerivationAlogrithm,其中包含空盐和已经盐渍的密码。一个问题是,我不知道如何“加盐”。我尝试将盐放在前面,最后,在中间并交替每个符号 - 仍然不是正确的键:( 这里是我使用的代码:
// password = 11112222333344445555 // salt = aaaabbbbccccddddeeee
private string password = "11112222333344445555aaaabbbbccccddddeeee";
private byte[] salt = new byte[20];
private uint iterationCount = 100;
private static byte[] keyBytes = null;
public static byte[] KeyBytes
{
get
{
//for (int i = 0; i < salt.Length; i++)
//{
// salt[i] = 0;
//}
// Setup KDF parameters for the desired salt and iteration count
KeyDerivationParameters kdfParameters = KeyDerivationParameters.BuildForPbkdf2(CryptographicBuffer.CreateFromByteArray(salt), iterationCount);
// Get a KDF provider for PBKDF2, and store the source password in a Cryptographic Key
KeyDerivationAlgorithmProvider kdf = KeyDerivationAlgorithmProvider.OpenAlgorithm(KeyDerivationAlgorithmNames.Pbkdf2Sha1);
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.
uint totalDataNeeded = 16;
IBuffer keyAndIv = CryptographicEngine.DeriveKeyMaterial(passwordSourceKey, kdfParameters, totalDataNeeded);
// Split the derived bytes into a seperate key and IV
keyBytes = keyAndIv.ToArray();
return keyBytes;
}
}
这里是我的加密方法的代码 - 它产生的结果与.NET桌面应用程序中的结果相同:)
private byte[] btVector = Encoding.UTF8.GetBytes("1234567891234567");
private byte[] keyBytes = Encoding.UTF8.GetBytes("123456789123456789123456");
public byte[] Encrypt(byte[] data)
{
// Get the key and iv and put all into IBuffers
IBuffer keyBuffer = WindowsRuntimeBuffer.Create(KeyBytes, 0, 16, 16); ;
IBuffer iv = WindowsRuntimeBuffer.Create(InitialVectorBytes, 0, 16, 16);
IBuffer plainText = CryptographicBuffer.CreateFromByteArray(data);
byte[] encryptedData;
// Setup an AES key, using AES in CBC mode and applying PKCS#7 padding on the input
SymmetricKeyAlgorithmProvider aesProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
CryptographicKey aesKeySymm = aesProvider.CreateSymmetricKey(keyBuffer);
// Encrypt the data and convert it to byte array
IBuffer encrypted = CryptographicEngine.Encrypt(aesKeySymm, plainText, iv);
CryptographicBuffer.CopyToByteArray(encrypted, out encryptedData);
return encryptedData;
}
答案 0 :(得分:1)
PBKDF [1/2]基本上是这样的:
1. Take a password.
2. Add a salt.
3. Hash the combined password and salt. Store in `result`
4. For (number of iterations)
1. Hash `result`, store in `result`.
PBKDF1和PBKDF2之间的最大区别是步骤4的第一部分。在PBKDF1中,它是打印的。在PBKDF2中,它更改为:
4. For (number of iterations)
1. Combine `result` and `salt`. Store in `result`
2. Hash `result`, store in `result`.
所以,你有几个选择。
选项1:
创建PBKDF1的自定义实现。 HashAlgorithmProvider
使重复散列结果非常容易。
选项2:
将PBKDF2与空salt
byte
数组一起使用,并将组合的salt和密码用作secret
。这应该与PBKDF1具有相同的效果。
应该注意的是,如果可能的话,你应该切换到使用PBKDF2。
希望这有助于编码!