使用.NET中的私钥对文件进行签名

时间:2021-03-15 18:23:25

How do I sign a file using .NET and how do I then validate that the generated key is a valid one once I read it?

如何使用.NET对文件进行签名,如何在读取后验证生成的密钥是否为有效密钥?

My goal is to have a text file with some values and a generated key. I then want to be able to check that generated key in the file to make no one has tampered with the values in the file once I sent the file to a customer.

我的目标是拥有一个带有一些值和生成密钥的文本文件。然后,我希望能够检查文件中生成的密钥,以便在我将文件发送给客户后,没有人篡改文件中的值。

Update: Found it here with some help from the answers.

更新:在这里找到答案的一些帮助。

3 个解决方案

#1


Try this:

class Program
{
    static byte[] Sign(string message, RSAParameters key)
    {
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        rsa.ImportParameters(key);

        byte[] toSign = Encoding.Unicode.GetBytes(message);
        return rsa.SignData(toSign, "SHA1");
    }

    static bool Verify(string message, byte[] signature, RSAParameters key)
    {
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        rsa.ImportParameters(key);

        byte[] toVerify = Encoding.Unicode.GetBytes(message);
        return rsa.VerifyData(toVerify, "SHA1", signature);
    }

    static void Main(string[] args)
    {
        string message = "Let's sign this message.";

        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); // Creates a new RANDOM key.
        RSAParameters privatekey = rsa.ExportParameters(true);
        RSAParameters publickey = rsa.ExportParameters(false);

        byte[] signature = Sign(message, privatekey);

        if (Verify(message, signature, publickey))
        {
            Console.WriteLine("It worked!");
        }
    }
}

It's important to note that a new public/private keypair is generated everytime you start this program. In order to do what you want, you'll want to save the public/private keypair before using it at both ends. Your public key is the only thing you need to verify, so your private key will not be published to the client.

重要的是要注意每次启动此程序时都会生成一个新的公钥/私钥对。为了做你想做的事,你需要在两端使用之前保存公钥/私钥对。您的公钥是唯一需要验证的内容,因此您的私钥不会发布到客户端。

You might want to take a look at ExportParameters or ExportCspBlob to accomplish saving/loading the public/private keypair.

您可能需要查看ExportParameters或ExportCspBlob以完成保存/加载公钥/私钥对。

#2


If you want to sign a assembly, jaloplo gave the solution.

如果你想签署一个集会,jaloplo给出了解决方案。

If you want to sign some files created by your program, have a look at the DSACryptoServiceProvider class in the System.Security.Cryptography namespace.

如果要对程序创建的某些文件进行签名,请查看System.Security.Cryptography命名空间中的DSACryptoServiceProvider类。

#3


You have two possibilities:

你有两种可能性:

  • Once is on project properties on the Sign option.
  • 一旦在Sign选项上的项目属性上。

  • The second is modify the AssemblyInfo.cs file and add the commands to use the sn generated: [assembly: AssemblyKeyFile("..\\..\\SN_Generated.snk")]
  • 第二个是修改AssemblyInfo.cs文件并添加命令以使用生成的sn:[assembly:AssemblyKeyFile(“.. \\ .. \\ SN_Generated.snk”)]

#1


Try this:

class Program
{
    static byte[] Sign(string message, RSAParameters key)
    {
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        rsa.ImportParameters(key);

        byte[] toSign = Encoding.Unicode.GetBytes(message);
        return rsa.SignData(toSign, "SHA1");
    }

    static bool Verify(string message, byte[] signature, RSAParameters key)
    {
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        rsa.ImportParameters(key);

        byte[] toVerify = Encoding.Unicode.GetBytes(message);
        return rsa.VerifyData(toVerify, "SHA1", signature);
    }

    static void Main(string[] args)
    {
        string message = "Let's sign this message.";

        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); // Creates a new RANDOM key.
        RSAParameters privatekey = rsa.ExportParameters(true);
        RSAParameters publickey = rsa.ExportParameters(false);

        byte[] signature = Sign(message, privatekey);

        if (Verify(message, signature, publickey))
        {
            Console.WriteLine("It worked!");
        }
    }
}

It's important to note that a new public/private keypair is generated everytime you start this program. In order to do what you want, you'll want to save the public/private keypair before using it at both ends. Your public key is the only thing you need to verify, so your private key will not be published to the client.

重要的是要注意每次启动此程序时都会生成一个新的公钥/私钥对。为了做你想做的事,你需要在两端使用之前保存公钥/私钥对。您的公钥是唯一需要验证的内容,因此您的私钥不会发布到客户端。

You might want to take a look at ExportParameters or ExportCspBlob to accomplish saving/loading the public/private keypair.

您可能需要查看ExportParameters或ExportCspBlob以完成保存/加载公钥/私钥对。

#2


If you want to sign a assembly, jaloplo gave the solution.

如果你想签署一个集会,jaloplo给出了解决方案。

If you want to sign some files created by your program, have a look at the DSACryptoServiceProvider class in the System.Security.Cryptography namespace.

如果要对程序创建的某些文件进行签名,请查看System.Security.Cryptography命名空间中的DSACryptoServiceProvider类。

#3


You have two possibilities:

你有两种可能性:

  • Once is on project properties on the Sign option.
  • 一旦在Sign选项上的项目属性上。

  • The second is modify the AssemblyInfo.cs file and add the commands to use the sn generated: [assembly: AssemblyKeyFile("..\\..\\SN_Generated.snk")]
  • 第二个是修改AssemblyInfo.cs文件并添加命令以使用生成的sn:[assembly:AssemblyKeyFile(“.. \\ .. \\ SN_Generated.snk”)]

相关文章