【发布时间】:2023-03-20 20:25:01
【问题描述】:
我收到此验证码,以证明 AES 密钥是否合规:
类 Cypher.cs
public bool ValidateCheckSumAESKey(string strAESKey)
{
string base64AESKey = strAESKey;
const int N = 3;
const string valSumCalc = "qx6p";
SHA256Managed h = new SHA256Managed();
byte[] base64AESKeyBytes = System.Text.Encoding.ASCII.GetBytes(base64AESKey);
byte[] sha256hash = h.ComputeHash(base64AESKeyBytes);
byte[] sha256hashNbytes = new byte[N];
Array.Copy(sha256hash, 0, sha256hashNbytes, 0, sha256hashNbytes.Length);
string base64sha256hashNbytes = Convert.ToBase64String(sha256hashNbytes);
string result = base64sha256hashNbytes.Replace("=", "");
if (0 == string.Compare(result, valSumCalc))
{
return true;
}
else
{
return false;
}
}
因此,给定“cWhay3H4asTvRzXzXGZQ3KyBEu9BZaIxl8J+L4Bhr5A=”的示例密钥通过了校验和。
我正在尝试使用
生成 AES 密钥 public string Encrypt()
{
AesManaged aes = new AesManaged();
aes.KeySize = 256;
return Convert.ToBase64String(aes.Key);
}
但即使我在主课上做
Cypher cf = new Cypher();
bool boolValid = false;
while (!boolValid)
{
string strKey = cf.Encrypt();
boolValid = cf.ValidateCheckSumAESKey(strKey);
}
...它可以运行几个小时而没有找到一个有效的密钥。如何缩短循环以更快地找到符合要求的密钥?
提前谢谢你。
【问题讨论】:
-
诚然,大幅截断 SHA 256 输出应该会减少搜索空间,但归根结底,您仍在尝试对 SHA256 执行原像攻击。它应该在计算上很昂贵。
-
首先这样做有什么安全优势?
-
不知道有什么好处。这只是根据请求建立“有效”AES 密钥的强制性规则。
-
似乎是由不知道从什么安全开始的人任意强加的,或者是故意使用已知密钥破坏加密的后门。 ANY 256 位数字是一个完全有效的 AES 密钥,它越随机越好。
-
非常了解客户,我知道这是一种破解代码的方法。但我对此无能为力。仍然运行验证 6 小时,没有找到一个密钥。我开始考虑将代码运行数周并使用我找到的任何有效结果创建一个池......
标签: c# encryption aes