Java keystore password security

Java keystore password security

Creates a password parameter and specifies the protection algorithm and associated parameters to use when encrypting a keystore entry.

Method Summary

Methods declared in class java.lang.Object

Constructor Detail

PasswordProtection

public PasswordProtection​(char[] password)

Creates a password parameter. The specified password is cloned before it is stored in the new PasswordProtection object.

PasswordProtection

public PasswordProtection​(char[] password, String protectionAlgorithm, AlgorithmParameterSpec protectionParameters)

Creates a password parameter and specifies the protection algorithm and associated parameters to use when encrypting a keystore entry. The specified password is cloned before it is stored in the new PasswordProtection object.

Method Detail

getProtectionAlgorithm

Gets the name of the protection algorithm. If none was set then the keystore provider will use its default protection algorithm. The name of the default protection algorithm for a given keystore type is set using the ‘keystore..keyProtectionAlgorithm’ security property. For example, the keystore.PKCS12.keyProtectionAlgorithm property stores the name of the default key protection algorithm used for PKCS12 keystores. If the security property is not set, an implementation-specific algorithm will be used.

Читайте также:  Php 7 for production

getProtectionParameters

getPassword

Gets the password. Note that this method returns a reference to the password. If a clone of the array is created it is the caller’s responsibility to zero out the password information after it is no longer needed.

destroy

isDestroyed

public boolean isDestroyed()

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Криптография в Java. Класс KeyStore

Привет, Хабр! Представляю вашему вниманию перевод 9 статьи «Java KeyStore» автора Jakob Jenkov из серии статей для начинающих, желающих освоить основы криптографии в Java.

Оглавление:

Хранилище ключей

Java KeyStore — это хранилище ключей в виде базы данных, представленное классом KeyStore (java.security.KeyStore). Хранилище можно записать на диск и снова прочитать, оно может быть защищено паролем, а каждая запись ключа в хранилище ключей может быть защищена собственным паролем, что делает класс KeyStore полезным механизмом для безопасной работы с ключами шифрования. Хранилище ключей может содержать ключи следующих типов:

  • Закрытые ключи (Private keys)
  • Открытые ключи и сертификаты(Public keys + certificates)
  • Секретные ключи (Secret keys)

Закрытый и открытый ключи используются в асимметричном шифровании. Открытый ключ может иметь связанный сертификат. Сертификат — это документ, удостоверяющий личность человека, организации или устройства, претендующего на владение открытым ключом. Сертификат обычно имеет цифровую подпись проверяющей стороны в качестве доказательства. Секретные ключи используются в симметричном шифровании. В большинстве случаев при настройке безопасного соединения симметричные ключи уступают асимметричным, поэтому чаще всего вы будете хранить открытые и закрытые ключи в хранилище ключей.

Создание хранилища ключей

Вы можете создать экземпляр KeyStore , вызвав его метод getInstance() . Вот пример создания экземпляра класса:

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

В этом примере создается экземпляр KeyStore по умолчанию. Также можно создавать экземпляры KeyStore с другим форматом хранения ключа, передавая параметр в метод getInstance() . Например создание экземпляра хранилища ключей PKCS12 :

KeyStore keyStore = KeyStore.getInstance("PKCS12");

Загрузка хранилища ключей

Прежде чем использовать экземпляр хранилища ключей, его необходимо загрузить. Экземпляры класса KeyStore часто записываются на диск или в другое хранилище для последующего использования, потому класс KeyStore предполагает, что вы должны прочитать его данные, прежде чем сможете его использовать. Однако можно инициализировать пустой экземпляр KeyStore без данных, как вы увидите позже.

Загрузка данных из файла или другого хранилища выполняется путем вызова метода load() который принимает два параметра:

  • InputStream из которого будут загружены данные.
  • char[] Массив символов, содержащий пароль от хранилища ключей.

Вот пример загрузки хранилища ключей:

char[] keyStorePassword = "123abc".toCharArray(); try(InputStream keyStoreData = new FileInputStream("keystore.ks"))

В этом примере загружается файл хранилища ключей keystore.ks. Если вы не хотите загружать какие-либо данные в хранилище ключей, просто передайте значение null для параметра InputStream . Вот как выглядит загрузка пустого хранилища ключей:

keyStore3.load(null, keyStorePassword);

Экземпляр класса KeyStore всегда должен загружаться либо с данными, либо с null . В противном случае хранилище ключей не инициализируется, и все вызовы его методов будут вызывать исключения.

Получение ключей

Вы можете получить ключи экземпляра класса KeyStore через его метод getEntry() . Запись хранилища ключей сопоставлена с псевдонимом (alias), который идентифицирует ключ, и защищена паролем ключа. Таким образом, чтобы получить доступ к ключу, вы должны передать псевдоним ключа и пароль методу getEntry() . Вот пример доступа к записи в экземпляре KeyStore :

char[] keyPassword = "789xyz".toCharArray(); KeyStore.ProtectionParameter entryPassword = new KeyStore.PasswordProtection(keyPassword); KeyStore.Entry keyEntry = keyStore3.getEntry("keyAlias", entryPassword);

Если вы знаете, что запись, к которой вы хотите получить доступ, является закрытым ключом, вы можете преобразовать экземпляр KeyStore.Entry в KeyStore.PrivateKeyEntry . Вот как это выглядит:

KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) keyStore3.getEntry("keyAlias", entryPassword);

После приведения к KeyStore.PrivateKeyEntry вы можете получить доступ к закрытому ключу, сертификату и цепочке сертификатов с помощью следующих методов:

Помещение ключей в хранилище

Вы также можете поместить ключи в экземпляр класса KeyStore . Пример помещения секретного ключа (симметричного ключа) в экземпляр KeyStore :

SecretKey secretKey = getSecretKey(); KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.SecretKeyEntry(secretKey); keyStore3.setEntry("keyAlias2", secretKeyEntry, entryPassword);

Хранение

Иногда вы можете захотеть сохранить хранилище ключей на какое-либо хранилище (диск, база данных и т. д.), чтобы вы могли загрузить его снова в другой раз. Экземпляр класса KeyStore сохраняется вызовом метода store() . Пример:

char[] keyStorePassword = "123abc".toCharArray(); try (FileOutputStream keyStoreOutputStream = new FileOutputStream("data/keystore.ks"))

Источник

Java keystore password security

Creates a password parameter and specifies the protection algorithm and associated parameters to use when encrypting a keystore entry.

Method Summary

Methods inherited from class java.lang.Object

Constructor Detail

PasswordProtection

public PasswordProtection(char[] password)

Creates a password parameter. The specified password is cloned before it is stored in the new PasswordProtection object.

PasswordProtection

public PasswordProtection(char[] password, String protectionAlgorithm, AlgorithmParameterSpec protectionParameters)

Creates a password parameter and specifies the protection algorithm and associated parameters to use when encrypting a keystore entry. The specified password is cloned before it is stored in the new PasswordProtection object.

Method Detail

getProtectionAlgorithm

Gets the name of the protection algorithm. If none was set then the keystore provider will use its default protection algorithm. The name of the default protection algorithm for a given keystore type is set using the ‘keystore..keyProtectionAlgorithm’ security property. For example, the keystore.PKCS12.keyProtectionAlgorithm property stores the name of the default key protection algorithm used for PKCS12 keystores. If the security property is not set, an implementation-specific algorithm will be used.

getProtectionParameters

getPassword

Gets the password. Note that this method returns a reference to the password. If a clone of the array is created it is the caller’s responsibility to zero out the password information after it is no longer needed.

destroy

isDestroyed

public boolean isDestroyed()

Источник

Class KeyStore.PasswordProtection

Creates a password parameter and specifies the protection algorithm and associated parameters to use when encrypting a keystore entry.

Method Summary

Methods declared in class java.lang.Object

Constructor Details

PasswordProtection

Creates a password parameter. The specified password is cloned before it is stored in the new PasswordProtection object.

PasswordProtection

public PasswordProtection (char[] password, String protectionAlgorithm, AlgorithmParameterSpec protectionParameters)

Creates a password parameter and specifies the protection algorithm and associated parameters to use when encrypting a keystore entry. The specified password is cloned before it is stored in the new PasswordProtection object.

Method Details

getProtectionAlgorithm

Gets the name of the protection algorithm. If none was set then the keystore provider will use its default protection algorithm.

getProtectionParameters

getPassword

Gets the password. Note that this method returns a reference to the password. If a clone of the array is created it is the caller’s responsibility to zero out the password information after it is no longer needed.

destroy

isDestroyed

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Оцените статью