根据现有验证生成RSA

本文关键字:RSA 验证 | 更新日期: 2023-09-27 18:28:50

我需要为客户生成一个新的许可证文件。该系统使用RSA密钥验证,其中公钥在数据库中,并且有一个license.lic文件,其中包含域、过期日期等信息,最后一个字段是另一个密钥。

license file: 87b6fe89-c0b6-431d-96ad-449d055755eb#2020/11/08#True#True#True#True#True#True#True#True#True#True#False#True#True#True#True#True#http://sitesample.com,http://www.sitesample.com,http://localhost#SiIQ6U50tHrfuZD8yCTDq1VJ3teSYnCEDzdJ4RpsLOBMEUcvgn5WiphHFKwYDrKlGUfgnO2iyMRVf2pkFY0c0yuMklBfd31TGlEdZ1uMEFG+WTY42+K5UZzxEs8Y16sNSXKok5fRtF3WRAqckkT3Xkm893zJcr+vcXqdk6rK3r4=
DB row: <RSAKeyValue>Modulus>s0TTPk5n7yS+7gp7VDNZCOnAPYbb24sLGmIviZpayS5aCpxsPKR/R53gbNMII3vBUXyLXg4V99rPffvmTuJuAxw4OPFcGc4cF5w2wiPd7uSCkkjuY9wbyK8YeF4Ge18Zmsl/1ewFM5nVS95XYKQJNR5SY1PAyRVwIfrdtBWTqWk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>

验证代码为:

byte[] bytes = Encoding.ASCII.GetBytes(string.Concat(new string[]
                {
                    values[0],
                    "#",
                    values[1],
                    "#",
                    values[2],
                    "#",
                    values[3],
                    "#",
                    values[4],
                    "#",
                    values[5],
                    "#",
                    values[6],
                    "#",
                    values[7],
                    "#",
                    values[8],
                    "#",
                    values[9],
                    "#",
                    values[10],
                    "#",
                    values[11],
                    "#",
                    values[12],
                    "#",
                    values[13],
                    "#",
                    values[14],
                    "#",
                    values[15],
                    "#",
                    values[16],
                    "#",
                    values[17],
                    "#",
                    values[18],
                    "#"
                }));
                SHA1Managed sHA1Managed = new SHA1Managed();
                byte[] rgbHash = sHA1Managed.ComputeHash(bytes);
                RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider();
                rSACryptoServiceProvider.FromXmlString(registration.public_key);
                byte[] rgbSignature = Convert.FromBase64String(values[19]);
                bool flag = rSACryptoServiceProvider.VerifyHash(rgbHash, CryptoConfig.MapNameToOID("SHA1"), rgbSignature);
                return flag;

这个验证工作正常,但我需要在许可证文件中插入一个新的域,所以我必须生成新的密钥。

我不会更改验证代码,因为它是实时的,而且我只有dll文件(使用ILSpy获得此代码),所以对我来说最好的方法是创建一个新的许可证文件并在数据库中插入。

我下载了这些互联网样本,并尝试使用生成的密钥,但验证总是返回false,我认为我的代码中的SHA1可能是问题所在。

http://www.codeproject.com/Articles/10877/Public-Key-RSA-Encryption-in-C-NET和http://www.codeproject.com/Articles/38739/RSA-Private-Key-Encryption

有人能帮我一把吗?

谢谢!

根据现有验证生成RSA

我使用这些代码创建了一个新的控制台应用程序,并且可以很好地进行验证。使用RSACryptoServiceProvider对来自服务器的哈希进行签名,然后验证来自客户端的哈希

public string EncryptString( string inputString, int dwKeySize, string xmlString )
{
        string name = inputString;
        FileStream fs = File.Create(@"license.lic");
        RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
        UnicodeEncoding ByteConverter = new UnicodeEncoding();
        StreamWriter write = new StreamWriter(fs);
        write.Write(name + "'r'n");
        string pkey = RSA.ToXmlString(false);
        write.Write(pkey + "'r'n");
        SHA1Managed Sha = new SHA1Managed();
        byte[] hashed = Sha.ComputeHash(Encoding.UTF8.GetBytes(name));
        byte[] signature = RSA.SignHash(hashed, CryptoConfig.MapNameToOID("SHA1"));
        write.Write(Convert.ToBase64String(signature));
        write.Close();
        fs.Close();
        return Convert.ToBase64String(hashed);    
    }
    public string DecryptString( string inputString, int dwKeySize, string xmlString )
    {
        FileStream fsSource = new FileStream(@"license.lic", FileMode.Open, FileAccess.Read);
        StreamReader reader = new StreamReader(fsSource);
        string name = reader.ReadLine();
        string pkey = reader.ReadLine();
        string signed = reader.ReadLine();
        byte[] bytes = Encoding.ASCII.GetBytes(name);
        SHA1Managed sHA1Managed = new SHA1Managed();
        byte[] rgbHash = sHA1Managed.ComputeHash(bytes);
        RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider();
        rSACryptoServiceProvider.FromXmlString(pkey);
        byte[] rgbSignature = Convert.FromBase64String(signed);
        bool flag = rSACryptoServiceProvider.VerifyHash(rgbHash, CryptoConfig.MapNameToOID("SHA1"), rgbSignature);
        return flag.ToString();
}