List of usage examples for java.security KeyPair getPublic
public PublicKey getPublic()
From source file:org.cesecore.certificates.certificate.CertificateDataSerializationTest.java
/** This test prints the serialized form of a CertificateData object, and was used to generated the data above. */ @Test//w w w .j a v a 2 s . com public void testSerializeCurrent() throws Exception { log.trace(">testSerializeCurrent"); final KeyPair kp = KeyTools.genKeys("1024", "RSA"); final Certificate cert = CertTools.genSelfCert("CN=certuser", 10 * 365, null, kp.getPrivate(), kp.getPublic(), "SHA256withRSA", false); final CertificateData certData = new CertificateData(cert, kp.getPublic(), "certuser", "1234567812345678", CertificateConstants.CERT_ACTIVE, CertificateConstants.CERTTYPE_ENDENTITY, CertificateProfileConstants.CERTPROFILE_FIXED_ENDUSER, EndEntityInformation.NO_ENDENTITYPROFILE, null, new Date().getTime(), false, true); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final Base64OutputStream b64os = new Base64OutputStream(baos); final ObjectOutputStream oos = new ObjectOutputStream(b64os); oos.writeObject(certData); oos.close(); b64os.close(); log.info("Base 64 of serialized CertData is: " + baos.toString("US-ASCII")); log.trace("<testSerializeCurrent"); }
From source file:org.diorite.impl.server.connection.ThreadPlayerLookupUUID.java
@Override public void run() { final GameProfileImpl oldProfile = this.loginListener.getGameProfile(); try {// w ww. java2 s. co m if (this.loginListener.getOnlineMode() != OnlineMode.TRUE) // TODO { this.loginListener.setUUID(UUID.nameUUIDFromBytes( ("OfflinePlayer:" + oldProfile.getName()).getBytes(StandardCharsets.UTF_8))); this.allow(); return; } final KeyPair serverKeyPair = this.getServer().getKeyPair(); final String hash = new BigInteger(MinecraftEncryption.combineKeys(this.loginListener.getServerID(), serverKeyPair.getPublic(), this.loginListener.getSecretKey())).toString(16); final GameProfileImpl newProfile = this.getServer().getSessionService().hasJoinedServer(oldProfile, hash); this.loginListener.setGameProfile(newProfile); if (newProfile == null) { this.loginListener.disconnect("Failed to verify username!"); this.loginListener.getLogger() .error("Username '" + oldProfile.getName() + "' tried to join with an invalid session"); GameProfiles.addEmptyEntry(oldProfile.getName(), UUID.nameUUIDFromBytes( ("OfflinePlayer:" + oldProfile.getName()).getBytes(StandardCharsets.UTF_8))); return; } GameProfiles.addToCache(newProfile); this.allow(); } catch (final AuthenticationUnavailableException serverEx) { this.loginListener.disconnect("Authentication servers are down. Please try again later, sorry!"); this.loginListener.getLogger().error("Couldn't verify username because servers are unavailable"); } catch (final Exception e) { e.printStackTrace(); this.loginListener.disconnect("Failed to verify username!"); this.loginListener.getLogger() .error("Username '" + oldProfile.getName() + "' tried to join with an invalid session"); } }
From source file:com.cellngine.crypto.RSACipher.java
@Override public void generateKeypair(final int keyLength) { if (keyLength <= 0) { throw new IllegalArgumentException("Key length must be positive and nonzero"); }/* ww w.j a va2 s . co m*/ final KeyPairGenerator generator; try { generator = KeyPairGenerator.getInstance(ALGORITHM); } catch (final NoSuchAlgorithmException e) { LOG.error("Unable to get key generator instance (" + ALGORITHM + ")", e); return; } try { generator.initialize(keyLength, this.random); } catch (final InvalidParameterException e) { throw new IllegalArgumentException("Unsupported key length"); } final KeyPair pair = generator.generateKeyPair(); this.publicKey = pair.getPublic(); this.privateKey = pair.getPrivate(); }
From source file:com.ibm.mobilefirstplatform.clientsdk.android.security.mca.internal.certificate.DefaultJSONSigner.java
@Override public String sign(KeyPair keyPair, JSONObject json) throws Exception { if (keyPair == null || json == null) { throw new IllegalArgumentException("parameter cannot be null"); }/* ww w .j av a2s . com*/ RSAPublicKey publicKey = ((RSAPublicKey) keyPair.getPublic()); PrivateKey privateKey = keyPair.getPrivate(); // create CSR Header (based on public key) JSONObject jwsHeaderJson = new JSONObject(); jwsHeaderJson.put(ALG, "RS256"); JSONObject publicKeyDataJson = new JSONObject(); publicKeyDataJson.put(ALG, "RSA"); String mod = encodeUrlSafe(publicKey.getModulus().toByteArray()); publicKeyDataJson.put("mod", mod); String exp = encodeUrlSafe(publicKey.getPublicExponent().toByteArray()); publicKeyDataJson.put("exp", exp); jwsHeaderJson.put("jpk", publicKeyDataJson); String jwsHeader = jwsHeaderJson.toString(); String payload = json.toString(); // concatenate JWS Header and payload. String csrHeaderAndPayload = encodeUrlSafe(jwsHeader.getBytes()) + "." + encodeUrlSafe(payload.getBytes()); // create CSR Signature String jwsSignature = encodeUrlSafe(signCsrData(csrHeaderAndPayload, privateKey)); // Concatenate them all, and return the result. return csrHeaderAndPayload + "." + jwsSignature; }
From source file:com.xk72.cocoafob.LicenseGenerator.java
private void initKeys(InputStream keyInputStream) throws IOException { Object readKey = readKey(keyInputStream); if (readKey instanceof KeyPair) { KeyPair keyPair = (KeyPair) readKey; privateKey = (DSAPrivateKey) keyPair.getPrivate(); publicKey = (DSAPublicKey) keyPair.getPublic(); } else if (readKey instanceof DSAPublicKey) { publicKey = (DSAPublicKey) readKey; } else {/*from w w w .java2s.c o m*/ throw new IllegalArgumentException( "The supplied key stream didn't contain a public or private key: " + readKey.getClass()); } }
From source file:SecureConnection.java
private byte[] getPublicKeyStep2(DHParameterSpec dhParamSpec) throws Exception { KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("DH"); keyPairGen.initialize(dhParamSpec);// w w w .ja va 2s .com KeyPair keyPair = keyPairGen.generateKeyPair(); this.keyAgree = KeyAgreement.getInstance("DH"); this.keyAgree.init(keyPair.getPrivate()); return keyPair.getPublic().getEncoded(); }
From source file:com.xinferin.licensing.LicenceGenerator.java
/** * Creates a new private and public key and at the same time encodes the public key as XML to be used by the .NET client * @param size/*w w w. ja v a 2 s .com*/ * @param productId * */ private void firstTimeInitialisation(int size) { try { // Get Key Pair Generator for RSA. KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(size); KeyPair keypair = keyGen.genKeyPair(); privateKey = keypair.getPrivate(); publicKey = keypair.getPublic(); // Get the bytes of the public and private keys byte[] privateKeyBytes = privateKey.getEncoded(); byte[] publicKeyBytes = publicKey.getEncoded(); // store temporarily witht he public key for the lifetime of this class. encodedPrivateKey = new Base64().encode(privateKeyBytes); // Generate the Private Key, Public Key and Public Key in XML format. KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(privateKeyBytes)); KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(publicKeyBytes)); RSAPublicKey rsaPublicKey = (RSAPublicKey) KeyFactory.getInstance("RSA") .generatePublic(new X509EncodedKeySpec(publicKeyBytes)); // Store the public key in XML string to make compatible .Net public key file encodedToXMLPublicKey = getRSAPublicKeyAsXMLString(rsaPublicKey); } catch (Exception ex) { System.out.println(ex.getMessage()); } }
From source file:org.nimbustools.ctxbroker.security.DefaultBootstrapFactory.java
public BootstrapInformation newBootstrap(String uuid, String ctxServiceURL, Calendar expires) throws ContextBrokerException { BootstrapInformation bootstrap = new BootstrapInformation(); KeyPair keypair = this.ca.createNewKeyPair(); X509Certificate cert;//w ww. j a va 2s .com try { cert = this.ca.signNewCertificate(uuid, keypair.getPublic(), expires); } catch (SignatureException e) { throw new ContextBrokerException(e.getMessage(), e); } catch (InvalidKeyException e) { throw new ContextBrokerException(e.getMessage(), e); } catch (CertificateException e) { throw new ContextBrokerException(e.getMessage(), e); } catch (IOException e) { throw new ContextBrokerException(e.getMessage(), e); } try { bootstrap.setX509Cert(cert); } catch (CertificateEncodingException e) { throw new ContextBrokerException(e.getMessage(), e); } try { bootstrap.setKeypair(keypair); } catch (IOException e) { throw new ContextBrokerException(e.getMessage(), e); } X500Principal subjectDN = cert.getSubjectX500Principal(); String DN = subjectDN.getName(X500Principal.RFC2253); String globusDN = CertUtil.toGlobusID(DN, false); bootstrap.setBootstrapDN(globusDN); return bootstrap; }
From source file:org.globus.gsi.gssapi.test.GlobusGSSCredentialTest.java
private X509Credential buildSelfSigned() throws GeneralSecurityException { KeyPair kp = kpg.generateKeyPair(); PrivateKey privateKey = kp.getPrivate(); certificateGenerator.setPublicKey(kp.getPublic()); X509Certificate certificate = certificateGenerator.generate(privateKey); X509Certificate[] certChain = new X509Certificate[] { certificate }; return new X509Credential(privateKey, certChain); }
From source file:mitm.common.security.certificate.GenerateKeyPairs.java
private void writeKeyPair(KeyPair keyPair) throws IOException { System.out.println("Keypair:"); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); System.out.println("Modulus:"); System.out.println(bigIntToString(privateKey.getModulus())); System.out.println();//from w w w . ja v a2s . co m System.out.println("Private exponent:"); System.out.println(bigIntToString(privateKey.getPrivateExponent())); System.out.println(); System.out.println("Public exponent:"); System.out.println(bigIntToString(publicKey.getPublicExponent())); System.out.println(); System.out.println("Encoded public key:"); System.out.println(bytesToHex(keyPair.getPublic().getEncoded())); System.out.println(); System.out.println("Encoded private key:"); System.out.println(bytesToHex(keyPair.getPrivate().getEncoded())); System.out.println(); System.out.println("Serial number:"); System.out.println(bigIntToString(serialNumberGenerator.generate())); }