/** * Generates a self-signed certificate. These are used to identify servers. * * @return A matched pair of public and private certificates. */ public CertificatePair generateSelfSigned() { keyPairGenerator.initialize(KEY_SIZE); KeyPair kp = keyPairGenerator.genKeyPair(); try { RSAPublicKeySpec pub = keyFactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class); RSAPrivateKeySpec priv = keyFactory.getKeySpec(kp.getPrivate(), RSAPrivateKeySpec.class); String uuid = UUID.randomUUID().toString(); signer.initSign(kp.getPrivate(), new SecureRandom()); signer.update(uuid.getBytes(Charsets.UTF_8)); signer.update(pub.getModulus().toByteArray()); signer.update(pub.getPublicExponent().toByteArray()); byte[] rawSig = signer.sign(); BigInteger signature = new BigInteger(rawSig); PublicIdentityCertificate publicCert = new PublicIdentityCertificate(uuid, pub.getModulus(), pub.getPublicExponent(), signature); PrivateIdentityCertificate privateCert = new PrivateIdentityCertificate(priv.getModulus(), priv.getPrivateExponent()); return new CertificatePair(publicCert, privateCert); } catch (InvalidKeySpecException | SignatureException | InvalidKeyException e) { throw new RuntimeException("Unexpected exception generating certificate", e); } }
signer.initSign(signingKey, new SecureRandom()); signer.update(uuid.getBytes(Charsets.UTF_8)); signer.update(pub.getModulus().toByteArray()); signer.update(pub.getPublicExponent().toByteArray()); byte[] rawSig = signer.sign(); BigInteger signature = new BigInteger(rawSig); PublicIdentityCertificate publicCert = new PublicIdentityCertificate(uuid, pub.getModulus(), pub.getPublicExponent(), signature); PrivateIdentityCertificate privateCert = new PrivateIdentityCertificate(priv.getModulus(), priv.getPrivateExponent()); return new CertificatePair(publicCert, privateCert);
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(2048); KeyPair kp = kpg.genKeyPair(); KeyFactory fact = KeyFactory.getInstance("RSA"); RSAPublicKeySpec pub = fact.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class); saveToFile(PUBLIC_KEY_FILE, pub.getModulus(), pub.getPublicExponent()); RSAPrivateKeySpec priv = fact.getKeySpec(kp.getPrivate(), RSAPrivateKeySpec.class); saveToFile(PRIVATE_KEY_FILE, priv.getModulus(), priv.getPrivateExponent());
BCRSAPublicKey( RSAPublicKeySpec spec) { this.modulus = spec.getModulus(); this.publicExponent = spec.getPublicExponent(); }
JCERSAPublicKey( RSAPublicKeySpec spec) { this.modulus = spec.getModulus(); this.publicExponent = spec.getPublicExponent(); }
BCRSAPublicKey( RSAPublicKeySpec spec) { this.modulus = spec.getModulus(); this.publicExponent = spec.getPublicExponent(); }
JCERSAPublicKey( RSAPublicKeySpec spec) { this.modulus = spec.getModulus(); this.publicExponent = spec.getPublicExponent(); }
JCERSAPublicKey( RSAPublicKeySpec spec) { this.modulus = spec.getModulus(); this.publicExponent = spec.getPublicExponent(); }
BCRSAPublicKey( RSAPublicKeySpec spec) { this.algorithmIdentifier = DEFAULT_ALGORITHM_IDENTIFIER; this.modulus = spec.getModulus(); this.publicExponent = spec.getPublicExponent(); }
BCRSAPublicKey( RSAPublicKeySpec spec) { this.modulus = spec.getModulus(); this.publicExponent = spec.getPublicExponent(); }
/** * @return true if the keypair has the same fingerprint as supplied */ public static boolean publicKeyHasFingerprint(RSAPublicKeySpec publicKey, String fingerprint) { return fingerprint(publicKey.getPublicExponent(), publicKey.getModulus()).equals(fingerprint); }
/** * @return true if the keypair has the same fingerprint as supplied */ public static boolean publicKeyHasFingerprint(RSAPublicKeySpec publicKey, String fingerprint) { return fingerprint(publicKey.getPublicExponent(), publicKey.getModulus()).equals(fingerprint); }
/** * @return true if the keypair has the same fingerprint as supplied */ public static boolean publicKeyHasFingerprint(RSAPublicKeySpec publicKey, String fingerprint) { return fingerprint(publicKey.getPublicExponent(), publicKey.getModulus()).equals(fingerprint); }
/** * @return true if the keypairs match */ public static boolean privateKeyMatchesPublicKey(RSAPrivateCrtKeySpec privateKey, RSAPublicKeySpec publicKey) { return privateKey.getPublicExponent().equals(publicKey.getPublicExponent()) && privateKey.getModulus().equals(publicKey.getModulus()); }
/** * @return true if the keypairs match */ public static boolean privateKeyMatchesPublicKey(RSAPrivateCrtKeySpec privateKey, RSAPublicKeySpec publicKey) { return privateKey.getPublicExponent().equals(publicKey.getPublicExponent()) && privateKey.getModulus().equals(publicKey.getModulus()); }
/** * @param publicKeyOpenSSH * RSA public key in OpenSSH format * @return fingerprint ex. {@code 2b:a9:62:95:5b:8b:1d:61:e0:92:f7:03:10:e9:db:d9} */ public static String fingerprintPublicKey(String publicKeyOpenSSH) { RSAPublicKeySpec publicKeySpec = publicKeySpecFromOpenSSH(publicKeyOpenSSH); return fingerprint(publicKeySpec.getPublicExponent(), publicKeySpec.getModulus()); }
/** * @param publicKeyOpenSSH * RSA public key in OpenSSH format * @return fingerprint ex. {@code 2b:a9:62:95:5b:8b:1d:61:e0:92:f7:03:10:e9:db:d9} */ public static String fingerprintPublicKey(String publicKeyOpenSSH) { RSAPublicKeySpec publicKeySpec = publicKeySpecFromOpenSSH(publicKeyOpenSSH); return fingerprint(publicKeySpec.getPublicExponent(), publicKeySpec.getModulus()); }
/** * @param publicKeyOpenSSH * RSA public key in OpenSSH format * @return fingerprint ex. {@code 2b:a9:62:95:5b:8b:1d:61:e0:92:f7:03:10:e9:db:d9} */ public static String fingerprintPublicKey(String publicKeyOpenSSH) { RSAPublicKeySpec publicKeySpec = publicKeySpecFromOpenSSH(publicKeyOpenSSH); return fingerprint(publicKeySpec.getPublicExponent(), publicKeySpec.getModulus()); }
KeyFactory fact = KeyFactory.getInstance("RSA"); RSAPublicKeySpec pub = fact.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class); RSAPrivateKeySpec priv = fact.getKeySpec(kp.getPrivate(), RSAPrivateKeySpec.class); saveToFile("public.key", pub.getModulus(), pub.getPublicExponent()); saveToFile("private.key", priv.getModulus(), priv.getPrivateExponent());
@Test public void testCanReadRsaAndCompareFingerprintOnPublicRSAKey() throws IOException { String pubKey = Strings2.toStringAndClose(getClass().getResourceAsStream("/test.pub")); RSAPublicKeySpec key = SshKeys.publicKeySpecFromOpenSSH(pubKey); String fingerPrint = fingerprint(key.getPublicExponent(), key.getModulus()); assertEquals(fingerPrint, expectedFingerprint); }