以下加密和解密算法通过powershell在sharepoint应用程序页面中调用:
public static string Encrypt(string dataToEncrypt, string password, string salt)
{
AesManaged aes = null;
MemoryStream memoryStream = null;
CryptoStream cryptoStream = null;
try
{
Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), 10000);
aes = new AesManaged();
aes.Key = rfc2898.GetBytes(32);
aes.IV = rfc2898.GetBytes(16);
memoryStream = new MemoryStream();
cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
byte[] data = Encoding.UTF8.GetBytes(dataToEncrypt);
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();
return Convert.ToBase64String(memoryStream.ToArray());
}
finally
{
if (cryptoStream != null)
cryptoStream.Close();
if (memoryStream != null)
memoryStream.Close();
if (aes != null)
aes.Clear();
}
}为什么加密字符串会发生变化?是关于应用程序域的吗?
发布于 2012-05-10 04:28:13
加密字符串因$字符的不同而不同。在通过powershell调用函数时,应该对$进行转义。
发布于 2012-05-04 21:41:50
当我使用相同的数据、密码和盐运行问题代码时,每次都会产生相同的结果。您应该确保dataToEncrypt和Salt每次都是相同的,即使只有一个字节发生变化,其余的字节也会发生变化。
然而,对于语义安全来说,这并不是您想要的。您需要一个随机的盐,以使暴力破解密码变得更加困难,以及一个随机的非机密IV集,以便两个相同的明文不具有相同的密文。
以下是encrypting and decrypting string的最佳实践示例,在设计时使用了加密算法的安全功能。SimpleEncryptWithPassword类似于您正在做的事情,尽管在本例中派生密钥的迭代次数是可变的,并且出于性能原因,您可能希望对其进行硬编码。
https://stackoverflow.com/questions/10447739
复制相似问题