Rsa Key Pair Generator Java
Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upRSA example with random key generation. RSA example with PKCS #1 Padding. RSA example with OAEP Padding and random key generation. An example of using RSA to encrypt a single asymmetric key. Simple Digital Signature Example: 36.38.7. Creates a 1024 bit RSA key pair and stores it to the filesystem as two files. The following are top voted examples for showing how to use java.security.KeyPairGenerator.These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Branch:master
packageorg.jdamico.bc.openpgp.utils; |
importjava.io.IOException; |
importjava.io.OutputStream; |
importjava.security.InvalidKeyException; |
importjava.security.NoSuchProviderException; |
importjava.security.PrivateKey; |
importjava.security.PublicKey; |
importjava.security.SignatureException; |
importjava.security.interfaces.RSAPrivateCrtKey; |
importjava.util.Date; |
importorg.bouncycastle.bcpg.ArmoredOutputStream; |
importorg.bouncycastle.bcpg.HashAlgorithmTags; |
importorg.bouncycastle.bcpg.RSASecretBCPGKey; |
importorg.bouncycastle.openpgp.PGPEncryptedData; |
importorg.bouncycastle.openpgp.PGPException; |
importorg.bouncycastle.openpgp.PGPKeyPair; |
importorg.bouncycastle.openpgp.PGPPrivateKey; |
importorg.bouncycastle.openpgp.PGPPublicKey; |
importorg.bouncycastle.openpgp.PGPSecretKey; |
importorg.bouncycastle.openpgp.PGPSignature; |
importorg.bouncycastle.openpgp.operator.PGPDigestCalculator; |
importorg.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder; |
importorg.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder; |
importorg.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyConverter; |
importorg.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder; |
/** |
* A simple utility class that generates a RSA PGPPublicKey/PGPSecretKey pair. |
* <p> |
* usage: RSAKeyPairGenerator [-a] identity passPhrase |
* <p> |
* Where identity is the name to be associated with the public key. The keys are placed |
* in the files pub.[asc bpg] and secret.[asc bpg]. |
*/ |
publicclassRSAKeyPairGenerator |
{ |
publicvoidexportKeyPair( |
OutputStreamsecretOut, |
OutputStreampublicOut, |
PublicKeypublicKey, |
PrivateKeyprivateKey, |
Stringidentity, |
char[] passPhrase, |
booleanarmor) |
throwsIOException, InvalidKeyException, NoSuchProviderException, SignatureException, PGPException |
{ |
if (armor) |
{ |
secretOut =newArmoredOutputStream(secretOut); |
} |
PGPPublicKey a = (newJcaPGPKeyConverter().getPGPPublicKey(PGPPublicKey.RSA_GENERAL, publicKey, newDate())); |
RSAPrivateCrtKey rsK = (RSAPrivateCrtKey)privateKey; |
RSASecretBCPGKey privPk =newRSASecretBCPGKey(rsK.getPrivateExponent(), rsK.getPrimeP(), rsK.getPrimeQ()); |
PGPPrivateKey b =newPGPPrivateKey(a.getKeyID(), a.getPublicKeyPacket(), privPk); |
PGPDigestCalculator sha1Calc =newJcaPGPDigestCalculatorProviderBuilder().build().get(HashAlgorithmTags.SHA1); |
PGPKeyPair keyPair =newPGPKeyPair(a,b); |
PGPSecretKey secretKey =newPGPSecretKey(PGPSignature.DEFAULT_CERTIFICATION, keyPair, identity, sha1Calc, null, null, newJcaPGPContentSignerBuilder(keyPair.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1), newJcePBESecretKeyEncryptorBuilder(PGPEncryptedData.CAST5, sha1Calc).setProvider('BC').build(passPhrase)); |
secretKey.encode(secretOut); |
secretOut.close(); |
if (armor) |
{ |
publicOut =newArmoredOutputStream(publicOut); |
} |
PGPPublicKey key = secretKey.getPublicKey(); |
key.encode(publicOut); |
publicOut.close(); |
} |
} |
Copy lines Copy permalink
In order to be able to create a digital signature, you need a private key. (Its corresponding public key will be needed in order to verify the authenticity of the signature.)
In some cases the key pair (private key and corresponding public key) are already available in files. In that case the program can import and use the private key for signing, as shown in Weaknesses and Alternatives.
Rsa
In other cases the program needs to generate the key pair. A key pair is generated by using the KeyPairGenerator
class.
In this example you will generate a public/private key pair for the Digital Signature Algorithm (DSA). You will generate keys with a 1024-bit length.
Generating a key pair requires several steps:
Sims 3 generations download key. Create a Key Pair Generator
The first step is to get a key-pair generator object for generating keys for the DSA signature algorithm.
Rsa Key Pair Generator Java Code
As with all engine classes, the way to get a KeyPairGenerator
object for a particular type of algorithm is to call the getInstance
static factory method on the KeyPairGenerator
class. This method has two forms, both of which hava a String algorithm
first argument; one form also has a String provider
second argument.
A caller may thus optionally specify the name of a provider, which will guarantee that the implementation of the algorithm requested is from the named provider. The sample code of this lesson always specifies the default SUN provider built into the JDK.
Put the following statement after the
line in the file created in the previous step, Prepare Initial Program Structure:
Initialize the Key Pair Generator
The next step is to initialize the key pair generator. All key pair generators share the concepts of a keysize and a source of randomness. The KeyPairGenerator
class has an initialize
method that takes these two types of arguments.
The keysize for a DSA key generator is the key length (in bits), which you will set to 1024.
The source of randomness must be an instance of the SecureRandom
class that provides a cryptographically strong random number generator (RNG). For more information about SecureRandom
, see the SecureRandom API Specification and the Java Cryptography Architecture Reference Guide .
The following example requests an instance of SecureRandom
that uses the SHA1PRNG algorithm, as provided by the built-in SUN provider. The example then passes this SecureRandom
instance to the key-pair generator initialization method.
Some situations require strong random values, such as when creating high-value and long-lived secrets like RSA public and private keys. To help guide applications in selecting a suitable strong SecureRandom
implementation, starting from JDK 8 Java distributions include a list of known strong SecureRandom
implementations in the securerandom.strongAlgorithms
property of the java.security.Security
class. When you are creating such data, you should consider using SecureRandom.getInstanceStrong()
, as it obtains an instance of the known strong algorithms.
Generate the Pair of Keys
The final step is to generate the key pair and to store the keys in PrivateKey
and PublicKey
objects.