Java Code Examples for org.bouncycastle.crypto.generators.RSAKeyPairGenerator#init()
The following examples show how to use
org.bouncycastle.crypto.generators.RSAKeyPairGenerator#init() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SslClientCertificateImplTest.java From hivemq-community-edition with Apache License 2.0 | 7 votes |
private KeyPair createKeyPair() throws InvalidKeySpecException, NoSuchAlgorithmException { final RSAKeyPairGenerator gen = new RSAKeyPairGenerator(); gen.init(new RSAKeyGenerationParameters(BigInteger.valueOf(3), new SecureRandom(), 1024, 80)); final AsymmetricCipherKeyPair keypair = gen.generateKeyPair(); final RSAKeyParameters publicKey = (RSAKeyParameters) keypair.getPublic(); final RSAPrivateCrtKeyParameters privateKey = (RSAPrivateCrtKeyParameters) keypair.getPrivate(); final PublicKey pubKey = KeyFactory.getInstance("RSA").generatePublic( new RSAPublicKeySpec(publicKey.getModulus(), publicKey.getExponent())); final PrivateKey privKey = KeyFactory.getInstance("RSA").generatePrivate( new RSAPrivateCrtKeySpec(publicKey.getModulus(), publicKey.getExponent(), privateKey.getExponent(), privateKey.getP(), privateKey.getQ(), privateKey.getDP(), privateKey.getDQ(), privateKey.getQInv())); return new KeyPair(pubKey, privKey); }
Example 2
Source File: BouncyCastleV1CryptoProvider.java From paseto with MIT License | 6 votes |
@Override public KeyPair rsaGenerate() { RSAKeyPairGenerator keyGen = new RSAKeyPairGenerator(); keyGen.init(new RSAKeyGenerationParameters(E, new SecureRandom(), RSA_KEY_SIZE, PrimeCertaintyCalculator.getDefaultCertainty(RSA_KEY_SIZE))); AsymmetricCipherKeyPair pair = keyGen.generateKeyPair(); RSAKeyParameters pub = (RSAKeyParameters) pair.getPublic(); RSAPrivateCrtKeyParameters priv = (RSAPrivateCrtKeyParameters) pair.getPrivate(); // As in BCRSAPrivateKey / BCRSAPublicKey AlgorithmIdentifier algo = new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE); byte[] publicKey = KeyUtil.getEncodedSubjectPublicKeyInfo(algo, new RSAPublicKey(pub.getModulus(), pub.getExponent())); byte[] privateKey = KeyUtil.getEncodedPrivateKeyInfo(algo, new RSAPrivateKey(priv.getModulus(), priv.getPublicExponent(), priv.getExponent(), priv.getP(), priv.getQ(), priv.getDP(), priv.getDQ(), priv.getQInv())); return new KeyPair(privateKey, publicKey); }
Example 3
Source File: CryptographicUtilities.java From openemm with GNU Affero General Public License v3.0 | 5 votes |
public static AsymmetricCipherKeyPair generateRsaKeyPair(int keyStrength) throws Exception { Security.addProvider(new BouncyCastleProvider()); try { RSAKeyPairGenerator generator = new RSAKeyPairGenerator(); generator.init(new RSAKeyGenerationParameters(RSAKeyGenParameterSpec.F4, SecureRandom.getInstance("SHA1PRNG"), keyStrength, 80)); AsymmetricCipherKeyPair keyPair = generator.generateKeyPair(); return keyPair; } catch (Exception e) { throw new Exception("Cannot create RSA keypair", e); } }
Example 4
Source File: EncryptionUtilTest.java From Hive2Hive with MIT License | 5 votes |
@Test @Ignore public void testPureLightweightBouncyCastle() throws IOException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, DataLengthException, IllegalStateException, InvalidCipherTextException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidAlgorithmParameterException { long startTime = System.currentTimeMillis(); Security.addProvider(new BouncyCastleProvider()); // generate RSA keys RSAKeyPairGenerator gen = new RSAKeyPairGenerator(); gen.init(new RSAKeyGenerationParameters(new BigInteger("10001", 16), new SecureRandom(), 2048, 80)); AsymmetricCipherKeyPair keyPair = gen.generateKeyPair(); // some data where first entry is 0 byte[] data = { 10, 122, 12, 127, 35, 58, 87, 56, -6, 73, 10, -13, -78, 4, -122, -61 }; // encrypt data asymmetrically AsymmetricBlockCipher cipher = new RSAEngine(); cipher = new PKCS1Encoding(cipher); cipher.init(true, keyPair.getPublic()); byte[] rsaEncryptedData = cipher.processBlock(data, 0, data.length); Assert.assertFalse(Arrays.equals(data, rsaEncryptedData)); // decrypt data asymmetrically cipher.init(false, keyPair.getPrivate()); byte[] dataBack = cipher.processBlock(rsaEncryptedData, 0, rsaEncryptedData.length); assertTrue(Arrays.equals(data, dataBack)); long stopTime = System.currentTimeMillis(); long elapsedTime = stopTime - startTime; logger.debug("elapsed time = {}", elapsedTime); }
Example 5
Source File: PGPEncryptionUtil.java From peer-os with Apache License 2.0 | 4 votes |
private static PGPKeyRingGenerator generateKeyRingGenerator( String id, char[] pass, int s2kcount, int keySize, KeyPair keyPair ) throws PGPException { // This object generates individual key-pairs. RSAKeyPairGenerator kpg = new RSAKeyPairGenerator(); // Boilerplate RSA parameters, no need to change anything // except for the RSA key-size (2048). You can use whatever // key-size makes sense for you -- 4096, etc. kpg.init( new RSAKeyGenerationParameters( BigInteger.valueOf( 0x10001 ), new SecureRandom(), keySize, 12 ) ); // First create the master (signing) key with the generator. PGPKeyPair rsakp_sign = new BcPGPKeyPair( PGPPublicKey.RSA_GENERAL, kpg.generateKeyPair(), new Date() ); // Then an encryption subkey. PGPKeyPair rsakp_enc = new BcPGPKeyPair( PGPPublicKey.RSA_GENERAL, kpg.generateKeyPair(), new Date() ); keyPair.setPrimaryKeyId( Long.toHexString( rsakp_sign.getKeyID() ) ); keyPair.setPrimaryKeyFingerprint( BytesToHex( rsakp_sign.getPublicKey().getFingerprint() ) ); keyPair.setSubKeyId( Long.toHexString( rsakp_enc.getKeyID() ) ); keyPair.setSubKeyFingerprint( BytesToHex( rsakp_enc.getPublicKey().getFingerprint() ) ); // Add a self-signature on the id PGPSignatureSubpacketGenerator signhashgen = new PGPSignatureSubpacketGenerator(); // Add signed metadata on the signature. // 1) Declare its purpose signhashgen.setKeyFlags( false, KeyFlags.SIGN_DATA | KeyFlags.CERTIFY_OTHER ); // 2) Set preferences for secondary crypto algorithms to use // when sending messages to this key. signhashgen.setPreferredSymmetricAlgorithms( false, new int[] { SymmetricKeyAlgorithmTags.AES_256, SymmetricKeyAlgorithmTags.AES_192, SymmetricKeyAlgorithmTags.AES_128, SymmetricKeyAlgorithmTags.CAST5, SymmetricKeyAlgorithmTags.TRIPLE_DES } ); signhashgen.setPreferredHashAlgorithms( false, new int[] { HashAlgorithmTags.SHA256, HashAlgorithmTags.SHA1, HashAlgorithmTags.SHA384, HashAlgorithmTags.SHA512, HashAlgorithmTags.SHA224, } ); signhashgen.setPreferredCompressionAlgorithms( false, new int[] { CompressionAlgorithmTags.ZLIB, CompressionAlgorithmTags.BZIP2, CompressionAlgorithmTags.ZIP } ); // 3) Request senders add additional checksums to the // message (useful when verifying unsigned messages.) signhashgen.setFeature( false, Features.FEATURE_MODIFICATION_DETECTION ); // Create a signature on the encryption subkey. PGPSignatureSubpacketGenerator enchashgen = new PGPSignatureSubpacketGenerator(); // Add metadata to declare its purpose enchashgen.setKeyFlags( false, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE ); // Objects used to encrypt the secret key. PGPDigestCalculator sha1Calc = new BcPGPDigestCalculatorProvider().get( HashAlgorithmTags.SHA1 ); // bcpg 1.48 exposes this API that includes s2kcount. Earlier // versions use a default of 0x60. PBESecretKeyEncryptor pske = ( new BcPBESecretKeyEncryptorBuilder( PGPEncryptedData.CAST5, sha1Calc, s2kcount ) ).build( pass ); // Finally, create the keyring itself. The constructor // takes parameters that allow it to generate the self // signature. PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator( PGPSignature.POSITIVE_CERTIFICATION, rsakp_sign, id, sha1Calc, signhashgen.generate(), null, new BcPGPContentSignerBuilder( rsakp_sign.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1 ), pske ); // Add our encryption subkey, together with its signature. keyRingGen.addSubKey( rsakp_enc, enchashgen.generate(), null ); return keyRingGen; }
Example 6
Source File: NativeRSAVectors.java From jna-gmp with Apache License 2.0 | 4 votes |
private static AsymmetricCipherKeyPair generateKeyPair(int rsaKeyBits) throws Exception { RSAKeyPairGenerator generator = new RSAKeyPairGenerator(); generator.init(new RSAKeyGenerationParameters(SIGNING_EXPONENT, SECURE_RANDOM, rsaKeyBits, 12)); return generator.generateKeyPair(); }
Example 7
Source File: RSAGEN.java From warp10-platform with Apache License 2.0 | 3 votes |
@Override public Object apply(WarpScriptStack stack) throws WarpScriptException { Object top = stack.pop(); if (!(top instanceof Long)) { throw new WarpScriptException(getName() + " expects a key length."); } int strength = ((Number) top).intValue(); top = stack.pop(); BigInteger exponent = new BigInteger(top.toString()); RSAKeyPairGenerator gen = new RSAKeyPairGenerator(); // For explanation of 'certainty', refer to http://bouncy-castle.1462172.n4.nabble.com/Questions-about-RSAKeyGenerationParameters-td1463186.html RSAKeyGenerationParameters params = new RSAKeyGenerationParameters(exponent, CryptoHelper.getSecureRandom(), strength, 64); gen.init(params); final AsymmetricCipherKeyPair keypair = gen.generateKeyPair(); Map<String,String> keyparams = new HashMap<String,String>(); keyparams.put(Constants.KEY_MODULUS, ((RSAKeyParameters) keypair.getPrivate()).getModulus().toString()); keyparams.put(Constants.KEY_ALGORITHM, "RSA"); keyparams.put(Constants.KEY_EXPONENT, ((RSAKeyParameters) keypair.getPrivate()).getExponent().toString()); stack.push(keyparams); keyparams = new HashMap<String,String>(); keyparams.put(Constants.KEY_MODULUS, ((RSAKeyParameters) keypair.getPublic()).getModulus().toString()); keyparams.put(Constants.KEY_ALGORITHM, "RSA"); keyparams.put(Constants.KEY_EXPONENT, ((RSAKeyParameters) keypair.getPublic()).getExponent().toString()); stack.push(keyparams); return stack; }