Ric*_*Ric 1 c# encryption cryptography windows-runtime windows-store-apps
我被要求在一个涉及 Windows 8 加密的项目中执行一项任务。
场景是这样的:
我byte[]从服务器得到一个,前 16 个字节是IV,接下来的 128 个是Salt,其余的是文件本身。
然后用户提供一个密码,并使用该密码和盐,我应该创建一个 PKCS5 密钥,迭代 40 次,密钥长度应为 32 字节。
现在我已经拆分了byte[]我需要的 3 个,但我不知道其余的在 Windows C# 中是如何完成的。
小智 6
我在加密/解密方面做了一些工作,但让我给你我用于 AES 256 位加密的资源。希望这能让您了解如何将其切换到 PKCS5,但我很确定其他所有内容都是相同的。这有点冗长,但如果这适用于您的情况,请告诉我。我很好奇 PKCS5 而不是 AES256 会有多大不同。
编辑:因为他们发布的代码在迭代中不清楚,所以迭代由var key = Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);使用 1000 次迭代的行控制。
http://www.codeproject.com/Articles/769741/Csharp-AES-bits-Encryption-Library-with-Salt
核心加密代码
using System.Security.Cryptography;
using System.IO;
Run Code Online (Sandbox Code Playgroud)
加密
public byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
{
byte[] encryptedBytes = null;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
cs.Close();
}
encryptedBytes = ms.ToArray();
}
}
return encryptedBytes;
}
Run Code Online (Sandbox Code Playgroud)
解密
public byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
{
byte[] decryptedBytes = null;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
cs.Close();
}
decryptedBytes = ms.ToArray();
}
}
return decryptedBytes;
}
Run Code Online (Sandbox Code Playgroud)
使用 Salt 获得随机加密结果
如果我们对同一个上下文(即“Hello World”的字符串)加密10次,加密的结果是一样的。如果我们希望每次加密的结果都不同怎么办?
我所做的是在加密前在原始字节前面附加一个随机盐字节,并在解密后将其删除。
在加密字符串之前附加随机盐的示例
public string Encrypt(string text, string pwd)
{
byte[] originalBytes = Encoding.UTF8.GetBytes(text);
byte[] encryptedBytes = null;
byte[] passwordBytes = Encoding.UTF8.GetBytes(pwd);
// Hash the password with SHA256
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
// Generating salt bytes
byte[] saltBytes = GetRandomBytes();
// Appending salt bytes to original bytes
byte[] bytesToBeEncrypted = new byte[saltBytes.Length + originalBytes.Length];
for (int i = 0; i < saltBytes.Length; i++)
{
bytesToBeEncrypted[i] = saltBytes[i];
}
for (int i = 0; i < originalBytes.Length; i++)
{
bytesToBeEncrypted[i + saltBytes.Length] = originalBytes[i];
}
encryptedBytes = AES_Encrypt(bytesToBeEncrypted, passwordBytes);
return Convert.ToBase64String(encryptedBytes);
}
Run Code Online (Sandbox Code Playgroud)
解密后去盐示例
public string Decrypt(string decryptedText, string pwd)
{
byte[] bytesToBeDecrypted = Convert.FromBase64String(decryptedText);
byte[] passwordBytes = Encoding.UTF8.GetBytes(pwd);
// Hash the password with SHA256
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
byte[] decryptedBytes = AES_Decrypt(bytesToBeDecrypted, passwordBytes);
// Getting the size of salt
int _saltSize = 4;
// Removing salt bytes, retrieving original bytes
byte[] originalBytes = new byte[decryptedBytes.Length - _saltSize];
for (int i = _saltSize; i < decryptedBytes.Length; i++)
{
originalBytes[i - _saltSize] = decryptedBytes[i];
}
return Encoding.UTF8.GetString(originalBytes);
}
Run Code Online (Sandbox Code Playgroud)
获取随机字节的代码
public byte[] GetRandomBytes()
{
int _saltSize = 4;
byte[] ba = new byte[_saltSize];
RNGCryptoServiceProvider.Create().GetBytes(ba);
return ba;
}
Run Code Online (Sandbox Code Playgroud)