C# SHA256被认为不安全吗?-SonarQube质量门-我如何修复它?
我有下面一段代码C# SHA256被认为不安全吗?-SonarQube质量门-我如何修复它?,c#,security,cryptography,sonarqube,md5,C#,Security,Cryptography,Sonarqube,Md5,我有下面一段代码 private static string sensitiveKey = "<REPLACE_WITH_KEY>" public static string Encrypt(string input) { // Get the bytes of the string byte[] passwordBytes = Encoding.UTF8.Get
private static string sensitiveKey = "<REPLACE_WITH_KEY>"
public static string Encrypt(string input)
{
// Get the bytes of the string
byte[] passwordBytes = Encoding.UTF8.GetBytes(sensitiveKey);
// Hash the password with SHA256
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
byte[] bytesEncrypted = EncryptStringToBytes_Aes(input, passwordBytes);
string result = Convert.ToBase64String(bytesEncrypted);
return result;
}
private static string sensitiveKey=“”
公共静态字符串加密(字符串输入)
{
//获取字符串的字节数
byte[]passwordBytes=Encoding.UTF8.GetBytes(sensitiveKey);
//用SHA256散列密码
passwordBytes=SHA256.Create().ComputeHash(passwordBytes);
byte[]bytesEncrypted=EncryptStringToBytes_Aes(输入,密码字节);
字符串结果=Convert.ToBase64String(字节加密);
返回结果;
}
库贝说
加密散列函数用于唯一标识信息,而不存储其原始形式。如果操作不当,攻击者可以通过猜测来窃取原始信息(例如:使用彩虹表),或者用具有相同哈希的另一个数据替换原始数据 也 只使用目前已知的强散列算法。避免在安全上下文中完全使用诸如MD5和SHA1之类的算法 所以问题是,我如何改进我的代码以使其安全
您可以尝试在散列密码中添加一个salt,因为未添加salt的散列更容易受到字典和彩虹表攻击
hash("hello") =
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
hash("hello" + "QxLUF1bgIAdeQX") =
9e209040c863f84a31e719795b2577523954739fe5ed3b58a75cff2127075ed1
hash("hello" + "bv5PehSMfV11Cd") =
d1d3ec2e6f20fd420d50e2642992841d8338a314b8ea157c9e18477aaef226ab
上面的代码是盐腌通常如何工作的一个例子。将固定字符串连接到密码,然后散列结果。盐应为固定长度,且不得重复使用。在您的案例中可能是这样的:
private static string salt = "<REPLACE_WITH_FIXED_LENGTH_SALT>";
private static string sensitiveKey = "<REPLACE_WITH_KEY>";
private static string salted_key = salt + sensitiveKey;
public static string Encrypt(string input)
{
// Get the bytes of the string
byte[] passwordBytes = Encoding.UTF8.GetBytes(salted_key);
// Hash the password with SHA256
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
byte[] bytesEncrypted = EncryptStringToBytes_Aes(input, passwordBytes);
string result = Convert.ToBase64String(bytesEncrypted);
return result;
}
私有静态字符串salt=”“;
私有静态字符串sensitiveKey=“”;
私有静态字符串salt_key=salt+sensitiveKey;
公共静态字符串加密(字符串输入)
{
//获取字符串的字节数
byte[]passwordBytes=Encoding.UTF8.GetBytes(salted_键);
//用SHA256散列密码
passwordBytes=SHA256.Create().ComputeHash(passwordBytes);
byte[]bytesEncrypted=EncryptStringToBytes_Aes(输入,密码字节);
字符串结果=Convert.ToBase64String(字节加密);
返回结果;
}
您可以尝试在散列密码中添加一个salt,因为未添加salt的散列更容易受到字典和彩虹表攻击
hash("hello") =
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
hash("hello" + "QxLUF1bgIAdeQX") =
9e209040c863f84a31e719795b2577523954739fe5ed3b58a75cff2127075ed1
hash("hello" + "bv5PehSMfV11Cd") =
d1d3ec2e6f20fd420d50e2642992841d8338a314b8ea157c9e18477aaef226ab
上面的代码是盐腌通常如何工作的一个例子。将固定字符串连接到密码,然后散列结果。盐应为固定长度,且不得重复使用。在您的案例中可能是这样的:
private static string salt = "<REPLACE_WITH_FIXED_LENGTH_SALT>";
private static string sensitiveKey = "<REPLACE_WITH_KEY>";
private static string salted_key = salt + sensitiveKey;
public static string Encrypt(string input)
{
// Get the bytes of the string
byte[] passwordBytes = Encoding.UTF8.GetBytes(salted_key);
// Hash the password with SHA256
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
byte[] bytesEncrypted = EncryptStringToBytes_Aes(input, passwordBytes);
string result = Convert.ToBase64String(bytesEncrypted);
return result;
}
私有静态字符串salt=”“;
私有静态字符串sensitiveKey=“”;
私有静态字符串salt_key=salt+sensitiveKey;
公共静态字符串加密(字符串输入)
{
//获取字符串的字节数
byte[]passwordBytes=Encoding.UTF8.GetBytes(salted_键);
//用SHA256散列密码
passwordBytes=SHA256.Create().ComputeHash(passwordBytes);
byte[]bytesEncrypted=EncryptStringToBytes_Aes(输入,密码字节);
字符串结果=Convert.ToBase64String(字节加密);
返回结果;
}
该代码不使用MD5或SHA1。正如Fildor指出的,您的代码不使用MD5,而是使用SHA256。这并不比MD5 as好多少。相反,您应该使用专门为密码哈希或更新问题标题而设计的函数。让我尝试使用bcrypt或Argon2。有些示例将不胜感激。@Fildor你推荐其他代码扫描程序而不是sonarqube吗?“如果操作不当,攻击者可以通过猜测来窃取原始信息(例如:使用彩虹表),或者用另一个具有相同哈希值的数据替换原始数据。”最后一部分只适用于像MD5这样的坏散列函数,仅仅“不正确地”应用一个已知的好散列并不能使您达到目的。实际上,整个声纳的描述都低于标准。但是,是的,在这个用例中使用密码散列。该代码不使用MD5或SHA1。正如Fildor指出的,您的代码不使用MD5,而是使用SHA256。这并不比MD5 as好多少。相反,您应该使用专门为密码哈希或更新问题标题而设计的函数。让我尝试使用bcrypt或Argon2。有些示例将不胜感激。@Fildor你推荐其他代码扫描程序而不是sonarqube吗?“如果操作不当,攻击者可以通过猜测来窃取原始信息(例如:使用彩虹表),或者用另一个具有相同哈希值的数据替换原始数据。”最后一部分只适用于像MD5这样的坏散列函数,仅仅“不正确地”应用一个已知的好散列并不能使您达到目的。实际上,整个声纳的描述都低于标准。但是,对这个用例使用密码散列。