Jun 17, 2014  RSACryptoServiceProvider is class which is used to generate public/private key pairs. This class provides several methods to generate keys and do encryption and decryption. Here is one sample code in C# which is generating public/private key pair and public ley. Set the Type of key to generate option to SSH-2 RSA. In the Number of bits in a generated key box, enter 2048. Click Generate to generate a public/private key pair. As the key is being generated, move the mouse around the blank area as directed. First of all we need a class which will generate a public and private key. We will create a class named SecurityUtil which will generate those based on DSA (we can use RSA or any other algorithm as long as its available) and we will generate a cryptographically strong pseudo-random number generator (PRNG) which can be clubbed along with DSA.

  1. Generate Public Key And Private Key Using Rsa Key
  2. Generate Rsa Public Key
  3. Advantages Of Private Key Encryption
  4. Generate Public Key From Private Key Rsa
  5. Private Key Definition
-->

To sign an assembly with a strong name, you must have a public/private key pair. This public and private cryptographic key pair is used during compilation to create a strong-named assembly. You can create a key pair using the Strong Name tool (Sn.exe). Key pair files usually have an .snk extension.

Generate public key and private key using rsa code

Note

In Visual Studio, the C# and Visual Basic project property pages include a Signing tab that enables you to select existing key files or to generate new key files without using Sn.exe. In Visual C++, you can specify the location of an existing key file in the Advanced property page in the Linker section of the Configuration Properties section of the Property Pages window. The use of the AssemblyKeyFileAttribute attribute to identify key file pairs was made obsolete beginning with Visual Studio 2005.

Create a key pair

To create a key pair, at a command prompt, type the following command:

sn –k <file name>

Generate Public Key And Private Key Using Rsa Key

In this command, file name is the name of the output file containing the key pair.

Generate private key rsa

The following example creates a key pair called sgKey.snk.

If you intend to delay sign an assembly and you control the whole key pair (which is unlikely outside test scenarios), you can use the following commands to generate a key pair and then extract the public key from it into a separate file. First, create the key pair:

Generate Rsa Public Key

Generate Public Key And Private Key Using Rsa

Next, extract the public key from the key pair and copy it to a separate file:

Advantages Of Private Key Encryption

Once you create the key pair, you must put the file where the strong name signing tools can find it.

When signing an assembly with a strong name, the Assembly Linker (Al.exe) looks for the key file relative to the current directory and to the output directory. When using command-line compilers, you can simply copy the key to the current directory containing your code modules.

Generate Public Key From Private Key Rsa

If you are using an earlier version of Visual Studio that does not have a Signing tab in the project properties, the recommended key file location is the project directory with the file attribute specified as follows:

Private Key Definition

See also