Java Code Examples for java.security.KeyPairGenerator#genKeyPair()
The following examples show how to use
java.security.KeyPairGenerator#genKeyPair() .
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: TestCipherKeyWrapperTest.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
private void wrapperPublicPriviteKeyTest(Provider p, String[] algorithms) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException { for (String algo : algorithms) { // Key pair generated System.out.println("Generate key pair (algorithm: " + algo + ", provider: " + p.getName() + ")"); KeyPairGenerator kpg = KeyPairGenerator.getInstance(algo); kpg.initialize(512); KeyPair kp = kpg.genKeyPair(); // key generated String algoWrap = "DES"; KeyGenerator kg = KeyGenerator.getInstance(algoWrap, p); Key key = kg.generateKey(); wrapTest(algo, algoWrap, key, kp.getPrivate(), Cipher.PRIVATE_KEY, false); wrapTest(algo, algoWrap, key, kp.getPublic(), Cipher.PUBLIC_KEY, false); } }
Example 2
Source File: JCATestCase.java From quarkus with Apache License 2.0 | 6 votes |
@Test public void testDecodeRSAKey() throws Exception { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(4096); KeyPair keyPair = keyPairGenerator.genKeyPair(); PublicKey publicKey = keyPair.getPublic(); byte[] encoded = publicKey.getEncoded(); byte[] pemEncoded = Base64.getEncoder().encode(encoded); String pemString = new String(pemEncoded, "UTF-8"); RestAssured.given() .queryParam("pemEncoded", pemString) .when() .get("/jca/decodeRSAKey") .then() .statusCode(200) .body(is("RSA")); }
Example 3
Source File: JCATestCase.java From quarkus with Apache License 2.0 | 6 votes |
@Test public void testVerifyRSASig() throws Exception { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(4096); KeyPair keyPair = keyPairGenerator.genKeyPair(); PublicKey publicKey = keyPair.getPublic(); byte[] encoded = publicKey.getEncoded(); byte[] pemEncoded = Base64.getEncoder().encode(encoded); String pemString = new String(pemEncoded, "UTF-8"); RestAssured.given() .queryParam("msg", "Hello verifyRSASig") .queryParam("publicKey", pemString) .queryParam("sig", "") .when() .get("/jca/verifyRSASig") .then() .statusCode(200) .body(is("true")); }
Example 4
Source File: GenerateKeypair.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws Exception { KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA"); kpg.initialize(512); // test generateKeyPair KeyPair kpair = kpg.generateKeyPair(); if (kpair == null) { throw new Exception("no keypair generated"); } // test genKeyPair kpair = kpg.genKeyPair(); if (kpair == null) { throw new Exception("no keypair generated"); } }
Example 5
Source File: Utils.java From java-11-examples with Apache License 2.0 | 5 votes |
/** * Generate one-time server keypair. * @return */ public static Iterable<KeyPair> generateKeyPairs() { try { List<KeyPair> result = new ArrayList<>(); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(1024); KeyPair keyPair = keyGen.genKeyPair(); result.add(keyPair); return Collections.unmodifiableCollection(result); } catch (NoSuchAlgorithmException e) { return Collections.emptyList(); } }
Example 6
Source File: AdbCrypto.java From SoloPi with Apache License 2.0 | 5 votes |
/** * Creates a new AdbCrypto object by generating a new key pair. * @param base64 Implementation of base 64 conversion interface required by ADB * @return A new AdbCrypto object * @throws NoSuchAlgorithmException If an RSA key factory cannot be found */ public static AdbCrypto generateAdbKeyPair(AdbBase64 base64) throws NoSuchAlgorithmException { AdbCrypto crypto = new AdbCrypto(); KeyPairGenerator rsaKeyPg = KeyPairGenerator.getInstance("RSA"); rsaKeyPg.initialize(KEY_LENGTH_BITS); crypto.keyPair = rsaKeyPg.genKeyPair(); crypto.base64 = base64; return crypto; }
Example 7
Source File: ClearTextKeyGenerator.java From data-transfer-project with Apache License 2.0 | 5 votes |
@Override public WorkerKeyPair generate() { KeyPairGenerator kpg = null; try { kpg = KeyPairGenerator.getInstance(ALGORITHM); } catch (NoSuchAlgorithmException e) { monitor.severe(() -> "NoSuchAlgorithmException for: " + ALGORITHM, e); throw new RuntimeException("NoSuchAlgorithmException generating key", e); } kpg.initialize(1024); KeyPair keyPair = kpg.genKeyPair(); return new WorkerKeyPair() { @Override public String getInstanceId() { return UUID.randomUUID().toString(); } @Override public byte[] getEncodedPublicKey() { return keyPair.getPublic().getEncoded(); } @Override public byte[] getEncodedPrivateKey() { return keyPair.getPrivate().getEncoded(); } }; }
Example 8
Source File: GameServerManager.java From L2jOrg with GNU General Public License v3.0 | 5 votes |
private void loadRSAKeys() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(512, RSAKeyGenParameterSpec.F4); keyGen.initialize(spec); _keyPairs = new KeyPair[KEYS_SIZE]; for (int i = 0; i < KEYS_SIZE; i++) { _keyPairs[i] = keyGen.genKeyPair(); } LOGGER.info("Cached {} RSA keys for Game Server communication.", _keyPairs.length); }
Example 9
Source File: AdbCrypto.java From TVRemoteIME with GNU General Public License v2.0 | 5 votes |
/** * Creates a new AdbCrypto object by generating a new key pair. * @param base64 Implementation of base 64 conversion interface required by ADB * @return A new AdbCrypto object * @throws NoSuchAlgorithmException If an RSA key factory cannot be found */ public static AdbCrypto generateAdbKeyPair(AdbBase64 base64) throws NoSuchAlgorithmException { AdbCrypto crypto = new AdbCrypto(); KeyPairGenerator rsaKeyPg = KeyPairGenerator.getInstance("RSA"); rsaKeyPg.initialize(KEY_LENGTH_BITS); crypto.keyPair = rsaKeyPg.genKeyPair(); crypto.base64 = base64; return crypto; }
Example 10
Source File: JWEKeyGenerator.java From data-transfer-project with Apache License 2.0 | 5 votes |
@Override public WorkerKeyPair generate() { monitor.debug(() -> "JWEKeyGenerator generate"); KeyPairGenerator kpg = null; try { kpg = KeyPairGenerator.getInstance(ALGORITHM); } catch (NoSuchAlgorithmException e) { monitor.severe(() -> "NoSuchAlgorithmException for: " + ALGORITHM, e); throw new RuntimeException("NoSuchAlgorithmException generating key", e); } kpg.initialize(1024); KeyPair keyPair = kpg.genKeyPair(); monitor.debug(() -> "JWEKeyGenerator generated WorkerKeyPair"); return new WorkerKeyPair() { @Override public String getInstanceId() { return UUID.randomUUID().toString(); } @Override public byte[] getEncodedPublicKey() { return keyPair.getPublic().getEncoded(); } @Override public byte[] getEncodedPrivateKey() { return keyPair.getPrivate().getEncoded(); } }; }
Example 11
Source File: Copy.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
public static void main(String args[]) throws Exception { KeyPairGenerator kg = KeyPairGenerator.getInstance(DSA); kg.initialize(KEY_SIZE); KeyPair kp = kg.genKeyPair(); Signature signature = Signature.getInstance(DSA); Test original = new Test(); SignedObject so = new SignedObject(original, kp.getPrivate(), signature); System.out.println("Signature algorithm: " + so.getAlgorithm()); signature = Signature.getInstance(DSA, "SUN"); if (!so.verify(kp.getPublic(), signature)) { throw new RuntimeException("Verification failed"); } kg = KeyPairGenerator.getInstance(DSA); kg.initialize(KEY_SIZE); kp = kg.genKeyPair(); if (so.verify(kp.getPublic(), signature)) { throw new RuntimeException("Unexpected success"); } Object copy = so.getObject(); if (!original.equals(copy)) { throw new RuntimeException("Signed object is not equal " + "to original one: " + copy); } /* * The signed object is a copy of an original one. * Once the copy is made, further manipulation * of the original object shouldn't has any effect on the copy. */ original.set(MAGIC - 1); copy = so.getObject(); if (original.equals(copy)) { throw new RuntimeException("Signed object is not a copy " + "of original one: " + copy); } System.out.println("Test passed"); }
Example 12
Source File: XMLDSigWithSecMgr.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
XMLDSigWithSecMgr() throws Exception { setup(); Document doc = db.newDocument(); Element envelope = doc.createElementNS ("http://example.org/envelope", "Envelope"); envelope.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "http://example.org/envelope"); doc.appendChild(envelope); KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); KeyPair kp = kpg.genKeyPair(); // the policy only grants this test SocketPermission to accept, resolve // and connect to localhost so that it can dereference 2nd reference System.setProperty("java.security.policy", System.getProperty("test.src", ".") + File.separator + "policy"); System.setSecurityManager(new SecurityManager()); try { // generate a signature with SecurityManager enabled ArrayList refs = new ArrayList(); refs.add(fac.newReference ("", sha1, Collections.singletonList (fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)), null, null)); refs.add(fac.newReference("http://localhost:" + ss.getLocalPort() + "/anything.txt", sha1)); SignedInfo si = fac.newSignedInfo(withoutComments, fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), refs); XMLSignature sig = fac.newXMLSignature(si, null); DOMSignContext dsc = new DOMSignContext(kp.getPrivate(), envelope); sig.sign(dsc); // validate a signature with SecurityManager enabled DOMValidateContext dvc = new DOMValidateContext (kp.getPublic(), envelope.getFirstChild()); // disable secure validation mode so that http reference will work dvc.setProperty("org.jcp.xml.dsig.secureValidation", Boolean.FALSE); sig = fac.unmarshalXMLSignature(dvc); if (!sig.validate(dvc)) { throw new Exception ("XMLDSigWithSecMgr signature validation FAILED"); } } catch (SecurityException se) { throw new Exception("XMLDSigWithSecMgr FAILED", se); } ss.close(); }
Example 13
Source File: Copy.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
public static void main(String args[]) throws Exception { KeyPairGenerator kg = KeyPairGenerator.getInstance(DSA); kg.initialize(KEY_SIZE); KeyPair kp = kg.genKeyPair(); Signature signature = Signature.getInstance(DSA); Test original = new Test(); SignedObject so = new SignedObject(original, kp.getPrivate(), signature); System.out.println("Signature algorithm: " + so.getAlgorithm()); signature = Signature.getInstance(DSA, "SUN"); if (!so.verify(kp.getPublic(), signature)) { throw new RuntimeException("Verification failed"); } kg = KeyPairGenerator.getInstance(DSA); kg.initialize(KEY_SIZE); kp = kg.genKeyPair(); if (so.verify(kp.getPublic(), signature)) { throw new RuntimeException("Unexpected success"); } Object copy = so.getObject(); if (!original.equals(copy)) { throw new RuntimeException("Signed object is not equal " + "to original one: " + copy); } /* * The signed object is a copy of an original one. * Once the copy is made, further manipulation * of the original object shouldn't has any effect on the copy. */ original.set(MAGIC - 1); copy = so.getObject(); if (original.equals(copy)) { throw new RuntimeException("Signed object is not a copy " + "of original one: " + copy); } System.out.println("Test passed"); }
Example 14
Source File: RSACrypto.java From wind-im with Apache License 2.0 | 4 votes |
public static KeyPair buildRSAKeyPair() throws NoSuchAlgorithmException { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(1024); return keyPairGenerator.genKeyPair(); }
Example 15
Source File: JwtAuthenticationProviderTest.java From auth0-spring-security-api with MIT License | 4 votes |
private KeyPair RSAKeyPair() throws NoSuchAlgorithmException { KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(2048); return kpg.genKeyPair(); }
Example 16
Source File: RSACrypto.java From openzaly with Apache License 2.0 | 4 votes |
public static KeyPair buildRSAKeyPair() throws NoSuchAlgorithmException { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(1024); return keyPairGenerator.genKeyPair(); }
Example 17
Source File: KeyUtils.java From smallrye-jwt with Apache License 2.0 | 4 votes |
public static KeyPair generateKeyPair(int keySize, SignatureAlgorithm algo) throws NoSuchAlgorithmException { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(keyFactoryAlgorithm(algo)); keyPairGenerator.initialize(keySize); return keyPairGenerator.genKeyPair(); }
Example 18
Source File: RSACrypto.java From openzaly with Apache License 2.0 | 4 votes |
public static KeyPair buildRSAKeyPair() throws NoSuchAlgorithmException { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(1024); return keyPairGenerator.genKeyPair(); }
Example 19
Source File: TokenUtils.java From quarkus-quickstarts with Apache License 2.0 | 2 votes |
/** * Generate a new RSA keypair. * * @param keySize - the size of the key * @return KeyPair * @throws NoSuchAlgorithmException on failure to load RSA key generator */ public static KeyPair generateKeyPair(final int keySize) throws NoSuchAlgorithmException { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(keySize); return keyPairGenerator.genKeyPair(); }
Example 20
Source File: TokenUtils.java From Hands-On-Enterprise-Java-Microservices-with-Eclipse-MicroProfile with MIT License | 2 votes |
/** * Generate a new RSA keypair. * * @param keySize - the size of the key * @return KeyPair * @throws NoSuchAlgorithmException on failure to load RSA key generator */ public static KeyPair generateKeyPair(final int keySize) throws NoSuchAlgorithmException { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(keySize); return keyPairGenerator.genKeyPair(); }