生成现有验证的RSA

时间:2013-08-22 18:49:27

标签: c# rsa sha1

我需要为客户生成新的许可文件。 系统使用RSA密钥验证,其中publickey在DB中,并且有一个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-NEThttp://www.codeproject.com/Articles/38739/RSA-Private-Key-Encryption

有人可以帮我一把吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

我使用此代码创建了一个新的控制台应用程序,并且我的验证工作正常。 Using the RSACryptoServiceProvider to sign a hash from a server and then verifying the hash from client side

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();
}