List of usage examples for java.security KeyPair getPublic
public PublicKey getPublic()
From source file:cloudeventbus.cli.Certs.java
private static void createCertificate(TrustStore trustStore, Certificate.Type type, AbstractCreateClientServerCommand createCommand) throws Exception { final Certificate issuerCertificate = trustStore.get(createCommand.issuer); if (issuerCertificate == null) { throw new IllegalArgumentException( "No certificate found in trust store with serial number " + createCommand.issuer); }// ww w . j a v a 2 s . c o m final PrivateKey issuerPrivateKey = CertificateUtils.loadPrivateKey(createCommand.issuerPrivateKey); final KeyPair keyPair = CertificateUtils.generateKeyPair(); CertificateUtils.savePrivateKey(keyPair.getPrivate(), createCommand.privateKey); final Certificate certificate = CertificateUtils.generateSignedCertificate(issuerCertificate, issuerPrivateKey, keyPair.getPublic(), type, getExpirationDate(createCommand.expirationDate), Subject.list(createCommand.subscribePermissions), Subject.list(createCommand.publishPermissions), createCommand.comment); final CertificateChain chain = new CertificateChain(certificate); CertificateUtils.saveCertificates(createCommand.certificate, chain); }
From source file:com.glaf.core.security.RSAUtils.java
/** ? */ public static RSAPublicKey getDefaultPublicKey() { KeyPair keyPair = getKeyPair(); if (keyPair != null) { return (RSAPublicKey) keyPair.getPublic(); }/*from ww w . j a va 2 s. co m*/ return null; }
From source file:Main.java
public static String getJwkPublic(KeyPair kp) { try {/*from w w w .j a va2 s . c om*/ JSONObject jk = new JSONObject(); jk.put("kty", "RSA"); // generate random kid SecureRandom random = new SecureRandom(); String kid = new BigInteger(130, random).toString(32); jk.put("kid", kid); jk.put("e", "AQAB"); KeyFactory kfactory = KeyFactory.getInstance("RSA"); RSAPublicKeySpec kspec = (RSAPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class); jk.put("n", encodeB64(kspec.getModulus().toByteArray())); JSONArray ja = new JSONArray(); ja.put(jk); JSONObject jo = new JSONObject(); jo.put("keys", ja); // Log.d("getJwkPublic key: ",pubkey.toString()); // Log.d("getJwkPublic jwk: ",jo.toString()); return jo.toString(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:gemlite.core.util.RSAUtils.java
/** * <p>//from ww w .ja v a 2 s. com * ?(?) * </p> * * @return * @throws Exception */ public static Map<String, Object> genKeyPair() throws Exception { KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM); keyPairGen.initialize(512); KeyPair keyPair = keyPairGen.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); Map<String, Object> keyMap = new HashMap<String, Object>(2); keyMap.put(PUBLIC_KEY, publicKey); keyMap.put(PRIVATE_KEY, privateKey); return keyMap; }
From source file:com.glaf.core.security.RSAUtils.java
/** * // w w w. j av a 2 s. co m * <p /> * {@code plaintext} {@code null} {@code null} * * @param plaintext * * @return */ public static String encryptString(String plaintext) { if (plaintext == null) { return null; } byte[] data = plaintext.getBytes(); KeyPair keyPair = getKeyPair(); try { byte[] en_data = encrypt((RSAPublicKey) keyPair.getPublic(), data); return new String(Hex.encodeHex(en_data)); } catch (Exception ex) { LOGGER.error(ex.getCause().getMessage()); } return null; }
From source file:cloudeventbus.pki.CertificateUtils.java
public static Certificate generateSelfSignedCertificate(KeyPair keyPair, long expirationDate, List<Subject> subscribePermissions, List<Subject> publishPermissions, String comment) { final long serialNumber = secureRandom.get().nextLong(); final Certificate certificate = new Certificate(Certificate.Type.AUTHORITY, serialNumber, serialNumber, expirationDate, keyPair.getPublic(), subscribePermissions, publishPermissions, comment, null); return signCertificate(certificate, keyPair.getPrivate(), certificate); }
From source file:org.kde.kdeconnect.Helpers.SecurityHelpers.RsaHelper.java
public static void initialiseRsaKeys(Context context) { SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); if (!settings.contains("publicKey") || !settings.contains("privateKey")) { KeyPair keyPair; try {//from w w w .j a v a2s .co m KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048); keyPair = keyGen.genKeyPair(); } catch (Exception e) { e.printStackTrace(); Log.e("KDE/initializeRsaKeys", "Exception"); return; } byte[] publicKey = keyPair.getPublic().getEncoded(); byte[] privateKey = keyPair.getPrivate().getEncoded(); SharedPreferences.Editor edit = settings.edit(); edit.putString("publicKey", Base64.encodeToString(publicKey, 0).trim() + "\n"); edit.putString("privateKey", Base64.encodeToString(privateKey, 0)); edit.apply(); } }
From source file:netinf.common.security.impl.CryptographyTest.java
@BeforeClass public static void classSetUp() throws Exception { final Properties properties = Utils.loadProperties(NETINFNODE_PROPERTIES); injector = Guice.createInjector(new LogModule(properties), new DatamodelImplModule(), new CommunicationModule(), new SecurityModule(), new AbstractModule() { @Override//w w w .ja v a 2 s . com protected void configure() { bind(NetInfNodeConnection.class).annotatedWith(SecurityModule.Security.class) .to(RemoteNodeConnection.class).in(Singleton.class); Names.bindProperties(binder(), properties); } }); factory = injector.getInstance(DatamodelFactory.class); identityObject = factory.createIdentityObject(); Identifier id = factory.createIdentifier(); IdentifierLabel label = factory.createIdentifierLabel(); label.setLabelName(DefinedLabelName.UNIQUE_LABEL.getLabelName()); label.setLabelValue("Test-Identity"); id.addIdentifierLabel(label); identityObject.setIdentifier(id); try { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(1024); KeyPair pair = keyPairGenerator.generateKeyPair(); privateKey = pair.getPrivate(); publicKey = pair.getPublic(); String keyName = identityObject.getIdentifier().toString() + "?" + DefinedAttributeIdentification.PUBLIC_KEY.getURI(); publicKeys.put(keyName, publicKey); identityObject.setPublicMasterKey(pair.getPublic()); } catch (Exception e) { throw new NetInfUncheckedException("error creating keys"); } convenienceCommunicator = EasyMock.createMock(RemoteNodeConnection.class); convenienceCommunicator.setHostAndPort("localhost", 5000); EasyMock.expectLastCall().anyTimes(); convenienceCommunicator.setSerializeFormat(SerializeFormat.JAVA); EasyMock.expectLastCall().anyTimes(); EasyMock.expect(convenienceCommunicator.getIO((Identifier) EasyMock.anyObject())).andReturn(identityObject) .anyTimes(); EasyMock.replay(convenienceCommunicator); identityManager = EasyMock.createMock(IdentityManager.class); EasyMock.expect(identityManager.getPrivateKey((String) EasyMock.anyObject())).andReturn(privateKey) .anyTimes(); EasyMock.expect(identityManager.hasPrivateKey((String) EasyMock.anyObject())).andReturn(true).anyTimes(); EasyMock.expect(identityManager.getPrivateKey(((String) EasyMock.anyObject()), (String) EasyMock.anyObject(), (String) EasyMock.anyObject())).andReturn(privateKey).anyTimes(); EasyMock.expect(identityManager.hasPrivateKey(((String) EasyMock.anyObject()), (String) EasyMock.anyObject(), (String) EasyMock.anyObject())).andReturn(true).anyTimes(); EasyMock.replay(identityManager); crypto = new CryptographyImpl(identityManager, algorithm, factory, convenienceCommunicator); }
From source file:net.link.util.test.pkix.PkiTestUtils.java
public static X509Certificate generateSelfSignedCertificate(KeyPair keyPair, String dn, DateTime notBefore, DateTime notAfter, @Nullable String signatureAlgorithm, boolean includeAuthorityKeyIdentifier, boolean caCert, boolean timeStampingPurpose) throws IllegalStateException, IOException, CertificateException, OperatorCreationException { return generateCertificate(keyPair.getPublic(), dn, keyPair.getPrivate(), null, notBefore, notAfter, signatureAlgorithm, includeAuthorityKeyIdentifier, caCert, timeStampingPurpose, null); }
From source file:com.google.jenkins.plugins.credentials.oauth.P12ServiceAccountConfigTestUtil.java
private static X509Certificate generateCertificate(KeyPair keyPair) throws OperatorCreationException, CertificateException { Calendar endCalendar = Calendar.getInstance(); endCalendar.add(Calendar.YEAR, 10); X509v3CertificateBuilder x509v3CertificateBuilder = new X509v3CertificateBuilder( new X500Name("CN=localhost"), BigInteger.valueOf(1), Calendar.getInstance().getTime(), endCalendar.getTime(), new X500Name("CN=localhost"), SubjectPublicKeyInfo.getInstance(keyPair.getPublic().getEncoded())); ContentSigner contentSigner = new JcaContentSignerBuilder("SHA1withRSA").build(keyPair.getPrivate()); X509CertificateHolder x509CertificateHolder = x509v3CertificateBuilder.build(contentSigner); return new JcaX509CertificateConverter().setProvider("BC").getCertificate(x509CertificateHolder); }