java.security.KeyStore.store()方法的使用及代码示例

x33g5p2x  于2022-01-23 转载在 其他  
字(10.8k)|赞(0)|评价(0)|浏览(243)

本文整理了Java中java.security.KeyStore.store()方法的一些代码示例,展示了KeyStore.store()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。KeyStore.store()方法的具体详情如下:
包路径:java.security.KeyStore
类名称:KeyStore
方法名:store

KeyStore.store介绍

[英]Writes this KeyStore to the specified OutputStream. The data written to the OutputStream is protected by the specified password.
[中]将此密钥库写入指定的OutputStream。写入OutputStream的数据受指定密码的保护。

代码示例

代码示例来源:origin: stackoverflow.com

KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());

char[] password = "some password".toCharArray();
ks.load(null, password);

// Store away the keystore.
FileOutputStream fos = new FileOutputStream("newKeyStoreFileName");
ks.store(fos, password);
fos.close();

代码示例来源:origin: syncany/syncany

private static void storeKeyStore(KeyStore keyStore, File keyStoreFile) {
    try {
      FileOutputStream trustStoreOutputStream = new FileOutputStream(keyStoreFile);
      keyStore.store(trustStoreOutputStream, new char[0]);

      trustStoreOutputStream.close();
    }
    catch (Exception e) {
      throw new RuntimeException("Cannot store key/truststore to file " + keyStoreFile, e);
    }
  }
}

代码示例来源:origin: apache/geode

public static <T extends Certificate> void createTrustStore(
  String filename, String password, Map<String, T> certs)
  throws GeneralSecurityException, IOException {
 KeyStore ks = KeyStore.getInstance("JKS");
 try (InputStream in = Files.newInputStream(Paths.get(filename))) {
  ks.load(in, password.toCharArray());
 } catch (EOFException e) {
  ks = createEmptyKeyStore();
 }
 for (Map.Entry<String, T> cert : certs.entrySet()) {
  ks.setCertificateEntry(cert.getKey(), cert.getValue());
 }
 try (OutputStream out = Files.newOutputStream(Paths.get(filename))) {
  ks.store(out, password.toCharArray());
 }
}

代码示例来源:origin: stackoverflow.com

KeyStore ks = KeyStore.getInstance("JKS");
ks.setKeyEntry("keyAlias", key, passwordForKeyCharArray, certChain);
OutputStream writeStream = new FileOutputStream(filePathToStore);
ks.store(writeStream, keystorePasswordCharArray);
writeStream.close();

代码示例来源:origin: stackoverflow.com

keystore.load(inputStreamFromOriginalFile, password);
KeyStore keystore2 = KeyStore.getInstance("JKS");
for (String name : toIterable(keystore.aliases())) {
  Entry entry = keystore.getEntry(name, protParam);
  keystore2.setEntry(name, entry, protParam);
}
keystore2,store(outputStream, password);

代码示例来源:origin: apache/zookeeper

private void writeKeystore(X509Certificate certificate, KeyPair entityKeyPair, String path) throws Exception {
  KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
  keyStore.load(null, PASSWORD);
  keyStore.setKeyEntry("alias", entityKeyPair.getPrivate(), PASSWORD, new Certificate[] { certificate });
  FileOutputStream outputStream = new FileOutputStream(path);
  keyStore.store(outputStream, PASSWORD);
  outputStream.flush();
  outputStream.close();
}

代码示例来源:origin: JZ-Darkal/AndroidHttpCapture

/**
 * Exports the keyStore to the specified file.
 *
 * @param file             file to save the KeyStore to
 * @param keyStore         KeyStore to export
 * @param keystorePassword the password for the KeyStore
 */
@Override
public void saveKeyStore(File file, KeyStore keyStore, String keystorePassword) {
  try (FileOutputStream fos = new FileOutputStream(file)) {
    keyStore.store(fos, keystorePassword.toCharArray());
  } catch (CertificateException | NoSuchAlgorithmException | IOException | KeyStoreException e) {
    throw new KeyStoreAccessException("Unable to save KeyStore to file: " + file.getName(), e);
  }
}

代码示例来源:origin: stackoverflow.com

X509Certificate cert = chain[k];
String alias = host + "-" + (k + 1);
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
[...]
ks.setCertificateEntry(alias, cert);
OutputStream out = new FileOutputStream("jssecacerts");
ks.store(out, passphrase);
out.close();

代码示例来源:origin: wildfly/wildfly

@Override
public void engineSetKeyEntry(String alias, Key key, char[] password, Certificate[] chain) throws KeyStoreException {
  try {
    // pack key into keystore and protect it using password
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    KeyStore keystore = KeyStore.getInstance(keyType);
    keystore.load(null, password);
    keystore.setKeyEntry(alias, key, password, chain);
    keystore.store(os, password);
    byte[] keystoreBytes = os.toByteArray();
    engineSetKeyEntry(alias, keystoreBytes, chain);
  } catch (CertificateException | NoSuchAlgorithmException | IOException e) {
    throw log.ldapKeyStoreFailedToSerializeKey(alias, e);
  }
}

代码示例来源:origin: javaee-samples/javaee7-samples

keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
  keyStore.load(in, "changeit".toCharArray());
  keyStore.store(new FileOutputStream(cacertsPath.toAbsolutePath().toFile()), "changeit".toCharArray());
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
  throw new IllegalStateException(e);

代码示例来源:origin: k9mail/k-9

private void writeCertificateFile() throws CertificateException {
  java.io.OutputStream keyStoreStream = null;
  try {
    keyStoreStream = new java.io.FileOutputStream(keyStoreFile);
    keyStore.store(keyStoreStream, "".toCharArray());
  } catch (FileNotFoundException e) {
    throw new CertificateException("Unable to write KeyStore: "
        + e.getMessage());
  } catch (CertificateException e) {
    throw new CertificateException("Unable to write KeyStore: "
        + e.getMessage());
  } catch (IOException e) {
    throw new CertificateException("Unable to write KeyStore: "
        + e.getMessage());
  } catch (NoSuchAlgorithmException e) {
    throw new CertificateException("Unable to write KeyStore: "
        + e.getMessage());
  } catch (KeyStoreException e) {
    throw new CertificateException("Unable to write KeyStore: "
        + e.getMessage());
  } finally {
    IOUtils.closeQuietly(keyStoreStream);
  }
}

代码示例来源:origin: jmdhappy/xxpay-master

/**
 * 存储ca证书成JKS格式
 * @param cert
 * @param alias
 * @param password
 * @param out
 * @throws KeyStoreException
 * @throws NoSuchAlgorithmException
 * @throws CertificateException
 * @throws IOException
 */
public static void storeCACert(Certificate cert, String alias,
    String password, OutputStream out) throws KeyStoreException,
    NoSuchAlgorithmException, CertificateException, IOException {
  KeyStore ks = KeyStore.getInstance("JKS");
  ks.load(null, null);
  ks.setCertificateEntry(alias, cert);
  // store keystore
  ks.store(out, HttpClientUtil.str2CharArray(password));
}

代码示例来源:origin: apache/hive

private static void createDefaultKeyStore()
   throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException {
  KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
  char[] password = keyStorePassword.toCharArray();
  ks.load(null, null);

  // Store away the keystore.
  try (FileOutputStream fos = new FileOutputStream(sslKeyStorePath)) {
   ks.store(fos, password);
  }
 }
}

代码示例来源:origin: apache/hbase

private static void saveKeyStore(KeyStore ks, String filename,
                 String password)
 throws GeneralSecurityException, IOException {
 FileOutputStream out = new FileOutputStream(filename);
 try {
  ks.store(out, password.toCharArray());
 } finally {
  out.close();
 }
}

代码示例来源:origin: apache/zookeeper

/**
 * Encodes the given X509Certificate and private key as a JKS KeyStore, optionally protecting the private key
 * (and possibly the cert?) with a password. Returns the byte array encoding of the key store, which may be written
 * to a file and loaded to instantiate the key store at a later point or in another process.
 * @param cert the X509 certificate to serialize.
 * @param privateKey the private key to serialize.
 * @param keyPassword an optional key password. If empty or null, the private key will not be encrypted.
 * @return the serialized bytes of the JKS key store.
 * @throws IOException
 * @throws GeneralSecurityException
 */
public static byte[] certAndPrivateKeyToJavaKeyStoreBytes(
    X509Certificate cert,
    PrivateKey privateKey,
    String keyPassword) throws IOException, GeneralSecurityException {
  KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
  char[] keyPasswordChars = keyPassword == null ? new char[0] : keyPassword.toCharArray();
  keyStore.load(null, keyPasswordChars);
  keyStore.setKeyEntry(
      "key",
      privateKey,
      keyPasswordChars,
      new Certificate[] { cert });
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  keyStore.store(outputStream, keyPasswordChars);
  outputStream.flush();
  byte[] result = outputStream.toByteArray();
  outputStream.close();
  return result;
}

代码示例来源:origin: apache/usergrid

KeyStore ks = KeyStore.getInstance( KeyStore.getDefaultType() );
ks.load( in, passphrase );
in.close();
OutputStream out = new FileOutputStream( "jssecacerts" );
ks.store( out, passphrase );
out.close();

代码示例来源:origin: igniterealtime/Openfire

/**
 * Saves the current state of the store to disk. Useful when certificates have been added or removed from the
 * store.
 */
public void persist() throws CertificateStoreConfigException
{
  try ( final FileOutputStream os = new FileOutputStream( configuration.getFile() ) )
  {
    store.store( os, configuration.getPassword() );
  }
  catch ( NoSuchAlgorithmException | KeyStoreException | CertificateException | IOException ex )
  {
    throw new CertificateStoreConfigException( "Unable to save changes to store in '" + configuration.getFile() + "'", ex );
  }
}

代码示例来源:origin: apache/zookeeper

/**
 * Encodes the given X509Certificate as a JKS TrustStore, optionally protecting the cert with a password (though
 * it's unclear why one would do this since certificates only contain public information and do not need to be
 * kept secret). Returns the byte array encoding of the trust store, which may be written to a file and loaded to
 * instantiate the trust store at a later point or in another process.
 * @param cert the certificate to serialize.
 * @param keyPassword an optional password to encrypt the trust store. If empty or null, the cert will not be encrypted.
 * @return the serialized bytes of the JKS trust store.
 * @throws IOException
 * @throws GeneralSecurityException
 */
public static byte[] certToJavaTrustStoreBytes(
    X509Certificate cert,
    String keyPassword) throws IOException, GeneralSecurityException {
  KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
  char[] keyPasswordChars = keyPassword == null ? new char[0] : keyPassword.toCharArray();
  trustStore.load(null, keyPasswordChars);
  trustStore.setCertificateEntry(cert.getSubjectDN().toString(), cert);
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  trustStore.store(outputStream, keyPasswordChars);
  outputStream.flush();
  byte[] result = outputStream.toByteArray();
  outputStream.close();
  return result;
}

代码示例来源:origin: wildfly/wildfly

try(OutputStream stream=new FileOutputStream(keyStoreName)) {
  SecretKey key=createSecretKey();
  KeyStore store=KeyStore.getInstance(storeType);
  store.load(null, null);
  store.setKeyEntry(alias, key, storePass.toCharArray(), null);
  store.store(stream, storePass.toCharArray());

代码示例来源:origin: JZ-Darkal/AndroidHttpCapture

private void initializeKeyStore() throws RootCertificateException,
    GeneralSecurityException, OperatorCreationException, IOException {
  if (authority.aliasFile(KEY_STORE_FILE_EXTENSION).exists()
      && authority.aliasFile(".pem").exists()) {
    return;
  }
  MillisecondsDuration duration = new MillisecondsDuration();
  KeyStore keystore = CertificateHelper.createRootCertificate(authority,
      KEY_STORE_TYPE);
  LOG.info("Created root certificate authority key store in {}ms",
      duration);
  OutputStream os = null;
  try {
    os = new FileOutputStream(
        authority.aliasFile(KEY_STORE_FILE_EXTENSION));
    keystore.store(os, authority.password());
  } finally {
    IOUtils.closeQuietly(os);
  }
  Certificate cert = keystore.getCertificate(authority.alias());
  exportPem(authority.aliasFile(".pem"), cert);
}

相关文章