); } catch (InvalidKeySpecException i) { throw new IOException(i.getMessage(), i); ); } catch (InvalidKeySpecException e) { throw new IOException(e.getMessage(), e);
/** * Decodes base64 encoded public key to PublicKey * @param publicKey encoded public key string * @return returns PublicKey */ public static PublicKey decodePublicKey(String publicKey) { byte[] sigBytes = org.bouncycastle.util.encoders.Base64.decode(publicKey); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(sigBytes); KeyFactory keyFactory = CertUtils.getKeyFactory(); if (keyFactory == null) return null; try { return keyFactory.generatePublic(x509KeySpec); } catch (InvalidKeySpecException e) { s_logger.error("Unable to create PublicKey from PublicKey string:" + e.getMessage()); } return null; }
/** * Decodes base64 encoded private key to PrivateKey * @param privateKey encoded private key string * @return returns PrivateKey */ public static PrivateKey decodePrivateKey(String privateKey) { byte[] sigBytes = org.bouncycastle.util.encoders.Base64.decode(privateKey); PKCS8EncodedKeySpec pkscs8KeySpec = new PKCS8EncodedKeySpec(sigBytes); KeyFactory keyFactory = CertUtils.getKeyFactory(); if (keyFactory == null) return null; try { return keyFactory.generatePrivate(pkscs8KeySpec); } catch (InvalidKeySpecException e) { s_logger.error("Unable to create PrivateKey from privateKey string:" + e.getMessage()); } return null; }
/** * Returns base64 encoded PublicKey * @param key PublicKey * @return public key encoded string */ public static String encodePublicKey(PublicKey key) { try { KeyFactory keyFactory = CertUtils.getKeyFactory(); if (keyFactory == null) return null; X509EncodedKeySpec spec = keyFactory.getKeySpec(key, X509EncodedKeySpec.class); return new String(org.bouncycastle.util.encoders.Base64.encode(spec.getEncoded()), Charset.forName("UTF-8")); } catch (InvalidKeySpecException e) { s_logger.error("Unable to get KeyFactory:" + e.getMessage()); } return null; }
/** * Returns base64 encoded PrivateKey * @param key PrivateKey * @return privatekey encoded string */ public static String encodePrivateKey(PrivateKey key) { try { KeyFactory keyFactory = CertUtils.getKeyFactory(); if (keyFactory == null) return null; PKCS8EncodedKeySpec spec = keyFactory.getKeySpec(key, PKCS8EncodedKeySpec.class); return new String(org.bouncycastle.util.encoders.Base64.encode(spec.getEncoded()), Charset.forName("UTF-8")); } catch (InvalidKeySpecException e) { s_logger.error("Unable to get KeyFactory:" + e.getMessage()); } return null; }
public static RSAPrivateKey decodePrivateKey(String base64){ PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(Base64.getMimeDecoder().decode(base64)); try { return (RSAPrivateKey)f.generatePrivate(spec); } catch (InvalidKeySpecException e) { throw new RuntimeException(e.getMessage(),e); } }
public static RSAPublicKey decodePublicKey(String base64){ X509EncodedKeySpec spec = new X509EncodedKeySpec(Base64.getMimeDecoder().decode(base64)); try { return (RSAPublicKey)f.generatePublic(spec); } catch (InvalidKeySpecException e) { throw new RuntimeException(e.getMessage(),e); } }
public static PublicKey getPublicKey(final byte[] value) { if (value == null || value.length != 64) { throw new IllegalArgumentException("Invalid public key."); } byte[] encodedKey = new byte[p256Head.length + value.length]; System.arraycopy(p256Head, 0, encodedKey, 0, p256Head.length); System.arraycopy(value, 0, encodedKey, p256Head.length, value.length); KeyFactory eckf; try { eckf = KeyFactory.getInstance("EC"); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException( "EC key factory not present in runtime"); } try { X509EncodedKeySpec ecpks = new X509EncodedKeySpec(encodedKey); return eckf.generatePublic(ecpks); } catch (InvalidKeySpecException e) { throw new RuntimeException(e.getMessage()); } }
/** * Loads the public key used for encrypting statistical data. * * @param file * @return */ public PublicKey loadPublicKey(File file) { try { byte[] encodedPublicKey = loadBytesFromFile(file); return publicKeyFromBytes(encodedPublicKey); } catch (InvalidKeySpecException e) { throw new IllegalStateException("Unable to create key from encoded specification in " + file.getAbsolutePath() + "; " + e.getMessage(), e); } }
/** * Loads the public key used for encrypting statistical data. * * @param file * @return */ public PublicKey loadPublicKey(File file) { try { byte[] encodedPublicKey = loadBytesFromFile(file); return publicKeyFromBytes(encodedPublicKey); } catch (InvalidKeySpecException e) { throw new IllegalStateException("Unable to create key from encoded specification in " + file.getAbsolutePath() + "; " + e.getMessage(), e); } }
/** * Decodes the provided DER-encoded ECDSA public key. * * @param encodedKey DER-encoded ECDSA public key * @return ECDSA public key * @throws VException if the public key could not be decoded */ public static ECPublicKey decodeECPublicKey(byte[] encodedKey) throws VException { try { X509EncodedKeySpec spec = new X509EncodedKeySpec(encodedKey); KeyFactory factory = KeyFactory.getInstance(PK_ALGORITHM); return (ECPublicKey)factory.generatePublic(spec); } catch (NoSuchAlgorithmException e) { throw new VException( "Java runtime doesn't support " + PK_ALGORITHM + " algorithm: " + e.getMessage()); } catch (InvalidKeySpecException e) { throw new VException("Encoded key is incompatible with " + PK_ALGORITHM + " algorithm: " + e.getMessage()); } }
/** * Returns an ECPublicKey from bytes obtained using ECPublicKey.getEncoded() * @param data * @return ECPublicKey or null if it fails */ public static ECPublicKey getPublicKey(byte[] data, Curves curve) { ECPublicKey remotePublicKey = null; try { X509EncodedKeySpec ks = new X509EncodedKeySpec(data); KeyFactory kf = KeyFactory.getInstance("EC", curve.kfProvider); remotePublicKey = (ECPublicKey)kf.generatePublic(ks); } catch (NoSuchAlgorithmException e) { Logger.error(ECDH.class, "NoSuchAlgorithmException : "+e.getMessage(),e); e.printStackTrace(); } catch (InvalidKeySpecException e) { Logger.error(ECDH.class, "InvalidKeySpecException : "+e.getMessage(), e); e.printStackTrace(); } return remotePublicKey; }
/** * @see org.apache.james.jdkim.api.PublicKeyRecord#getPublicKey() */ public PublicKey getPublicKey() { try { String p = getValue("p").toString(); byte[] key = Base64.decodeBase64(p.getBytes()); KeyFactory keyFactory; keyFactory = KeyFactory.getInstance(getValue("k").toString()); X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(key); RSAPublicKey rsaKey; rsaKey = (RSAPublicKey) keyFactory.generatePublic(pubSpec); return rsaKey; } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("Unknown algorithm: " + e.getMessage()); } catch (InvalidKeySpecException e) { throw new IllegalStateException("Invalid key spec: " + e.getMessage()); } }
@Override public PrivateKey load(Credentials in) { try { String privateKeyInPemFormat = checkNotNull(in.credential, "credential in PEM format"); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePrivate(privateKeySpec(ByteSource.wrap(privateKeyInPemFormat.getBytes(UTF_8)))); } catch (NoSuchAlgorithmException e) { throw new AssertionError(e); } catch (IOException e) { throw propagate(e); } catch (InvalidKeySpecException e) { throw new AuthorizationException("security exception loading credentials. " + e.getMessage(), e); // catch IAE that is thrown when parsing the pk fails } catch (IllegalArgumentException e) { throw new AuthorizationException("cannot parse pk. " + e.getMessage(), e); } } }
@Override public PrivateKey load(Credentials in) { try { String privateKeyInPemFormat = checkNotNull(in.credential, "credential in PEM format"); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePrivate(privateKeySpec(ByteSource.wrap(privateKeyInPemFormat.getBytes(UTF_8)))); } catch (NoSuchAlgorithmException e) { throw new AssertionError(e); } catch (IOException e) { throw propagate(e); } catch (InvalidKeySpecException e) { throw new AuthorizationException("security exception loading credentials. " + e.getMessage(), e); // catch IAE that is thrown when parsing the pk fails } catch (IllegalArgumentException e) { throw new AuthorizationException("cannot parse pk. " + e.getMessage(), e); } } }
/** * Returns an ECPublicKey from bytes obtained using ECPublicKey.getEncoded() * @param data * @return ECPublicKey or null if it fails */ public static ECPublicKey getPublicKey(byte[] data, Curves curve) { ECPublicKey remotePublicKey = null; try { X509EncodedKeySpec ks = new X509EncodedKeySpec(data); KeyFactory kf = KeyFactory.getInstance("EC", curve.kfProvider); remotePublicKey = (ECPublicKey)kf.generatePublic(ks); } catch (NoSuchAlgorithmException e) { Logger.error(ECDSA.class, "NoSuchAlgorithmException : "+e.getMessage(),e); e.printStackTrace(); } catch (InvalidKeySpecException e) { Logger.error(ECDSA.class, "InvalidKeySpecException : "+e.getMessage(), e); e.printStackTrace(); } return remotePublicKey; }
@Override public PrivateKey load(Credentials in) { try { String privateKeyInPemFormat = checkNotNull(in.credential, "credential in PEM format"); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePrivate(privateKeySpec(ByteSource.wrap(privateKeyInPemFormat.getBytes(UTF_8)))); } catch (NoSuchAlgorithmException e) { throw new AssertionError(e); } catch (IOException e) { throw propagate(e); } catch (InvalidKeySpecException e) { throw new AuthorizationException("security exception loading credentials. " + e.getMessage(), e); // catch IAE that is thrown when parsing the pk fails } catch (IllegalArgumentException e) { throw new AuthorizationException("cannot parse pk. " + e.getMessage(), e); } } }
@Override public PrivateKey load(Credentials in) { try { String privateKeyInPemFormat = checkNotNull(in.credential, "credential in PEM format"); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePrivate(privateKeySpec(ByteSource.wrap(privateKeyInPemFormat.getBytes(UTF_8)))); } catch (NoSuchAlgorithmException e) { throw new AssertionError(e); } catch (IOException e) { throw propagate(e); } catch (InvalidKeySpecException e) { throw new AuthorizationException("security exception loading credentials. " + e.getMessage(), e); // catch IAE that is thrown when parsing the pk fails } catch (IllegalArgumentException e) { throw new AuthorizationException("cannot parse pk. " + e.getMessage(), e); } } }
public EnrolmentResponse scepCertPoll(PrivateKey identityKey, X509Certificate identityCert, CertificationRequest csr, X500Name issuer) throws ScepClientException { ScepUtil.requireNonNull("csr", csr); TransactionId tid; try { tid = TransactionId.sha1TransactionId( csr.getCertificationRequestInfo().getSubjectPublicKeyInfo()); } catch (InvalidKeySpecException ex) { throw new ScepClientException(ex.getMessage(), ex); } return scepCertPoll(identityKey, identityCert, tid, issuer, csr.getCertificationRequestInfo().getSubject()); }
private EnrolmentResponse enroll(MessageType messageType, CertificationRequest csr, PrivateKey identityKey, X509Certificate identityCert) throws ScepClientException { TransactionId tid; try { tid = TransactionId.sha1TransactionId( csr.getCertificationRequestInfo().getSubjectPublicKeyInfo()); } catch (InvalidKeySpecException ex) { throw new ScepClientException(ex.getMessage(), ex); } PkiMessage pkiMessage = new PkiMessage(tid, messageType); pkiMessage.setMessageData(csr); ContentInfo envRequest = encryptThenSign(pkiMessage, identityKey, identityCert); ScepHttpResponse httpResp = httpSend(Operation.PKIOperation, envRequest); CMSSignedData cmsSignedData = parsePkiMessage(httpResp.getContentBytes()); DecodedPkiMessage response = decode(cmsSignedData, identityKey, identityCert); assertSameNonce(pkiMessage, response); return new EnrolmentResponse(response); }