importjava.security.KeyPairGenerator; |
importjava.security.KeyPair; |
importjava.security.PrivateKey; |
importjava.security.PublicKey; |
importjava.security.KeyFactory; |
importjava.security.spec.EncodedKeySpec; |
importjava.security.spec.PKCS8EncodedKeySpec; |
importjava.security.spec.X509EncodedKeySpec; |
importjava.security.spec.InvalidKeySpecException; |
importjava.security.NoSuchAlgorithmException; |
importcom.sun.jersey.core.util.Base64; |
publicclassGeneratePublicPrivateKeys { |
privatestaticvoidgenerateKeys(StringkeyAlgorithm, intnumBits) { |
try { |
// Get the public/private key pair |
KeyPairGenerator keyGen =KeyPairGenerator.getInstance(keyAlgorithm); |
keyGen.initialize(numBits); |
KeyPair keyPair = keyGen.genKeyPair(); |
PrivateKey privateKey = keyPair.getPrivate(); |
PublicKey publicKey = keyPair.getPublic(); |
System.out.println('n'+'Generating key/value pair using '+ privateKey.getAlgorithm() +' algorithm'); |
// Get the bytes of the public and private keys |
byte[] privateKeyBytes = privateKey.getEncoded(); |
byte[] publicKeyBytes = publicKey.getEncoded(); |
// Get the formats of the encoded bytes |
String formatPrivate = privateKey.getFormat(); // PKCS#8 |
String formatPublic = publicKey.getFormat(); // X.509 |
System.out.println('Private Key : '+Base64.encode(String.valueOf(privateKeyBytes))); |
System.out.println('Public Key : '+Base64.encode(String.valueOf(publicKeyBytes))); |
// The bytes can be converted back to public and private key objects |
KeyFactory keyFactory =KeyFactory.getInstance(keyAlgorithm); |
EncodedKeySpec privateKeySpec =newPKCS8EncodedKeySpec(privateKeyBytes); |
PrivateKey privateKey2 = keyFactory.generatePrivate(privateKeySpec); |
EncodedKeySpec publicKeySpec =newX509EncodedKeySpec(publicKeyBytes); |
PublicKey publicKey2 = keyFactory.generatePublic(publicKeySpec); |
// The original and new keys are the same |
System.out.println(' Are both private keys equal? '+ privateKey.equals(privateKey2)); |
System.out.println(' Are both public keys equal? '+ publicKey.equals(publicKey2)); |
} catch (InvalidKeySpecException specException) { |
System.out.println('Exception'); |
System.out.println('Invalid Key Spec Exception'); |
} catch (NoSuchAlgorithmException e) { |
System.out.println('Exception'); |
System.out.println('No such algorithm: '+ keyAlgorithm); |
} |
} |
publicstaticvoidmain(String[] args) { |
// Generate a 1024-bit Digital Signature Algorithm (DSA) key pair |
generateKeys('DSA', 1024); |
// Generate a 576-bit DH key pair |
generateKeys('DH', 576); |
// Generate a 1024-bit RSA key pair |
generateKeys('RSA', 1024); |
} |
} |
Each key pair consists of a private key and a corresponding public key. When you use SSH key for Git, you inform Git that this PC is authenticated for that Github account, and it will never ask you about any access again because you have already given it your SSH key. Linux git SSH key Related Resources ¶.
Hi You post is interestnig , is there away I can create a privatre key instance via a signature given stiring? I have pub/private keys generated already KeyPairGenerator keyPairGenerator is going to createa key pair, but in my case I alrady have it and then further want to use them for signign. Git ssh key windows generate. e.g //ecdsaSign.initSign(keyPair.getPrivate()); |
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.
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.
To create a key pair, at a command prompt, type the following command:
sn –k <file name>
In this command, file name is the name of the output file containing the key pair.
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:
Next, extract the public key from the key pair and copy it to a separate file:
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.
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: