是否有命令行工具可以在 Java 密钥库中生成对称密钥?

发布于 2024-12-29 10:06:11 字数 468 浏览 0 评论 0原文

我正在为我的应用程序编写一个有关密钥更新的程序。此过程将由系统管理员每年左右执行一次。

在我的应用程序中,有一个对称密钥用于在将某些数据存储到数据库之前对其进行加密。该密钥存储在 Java 密钥库中。

当应用程序必须以加密方式在数据库中存储某些数据时,要使用的密钥别名是从配置文件中读取的,密钥是使用此密钥别名从 Java 密钥库中读取的,数据使用密钥进行加密并存储数据库中的所有内容:密钥别名、初始化向量和加密数据,全部用分号分隔。

因此,使用另一个密钥的过程很简单:使用

  1. 另一个别名在 Java 密钥库中生成一个新的对称密钥,
  2. 更改配置文件以使用这个新的密钥别名

,但我不知道有任何命令行工具可以在Java 密钥库。 java keytool 实用程序只能创建密钥对。

是否有命令行工具可以在 Java 密钥库中生成对称密钥,或者我应该开发自己的工具?

I am writing a procedure about key renewing for my application. This procedure will be executed by a sysadmin every year or so.

In my application, there is a symmetric key used to cipher some data before storing it in the database. This key is stored in a Java keystore.

When the application must store some data in the database in a ciphered way, the key alias to use is read from a configuration file, the key is read from the Java keystore with this key alias, the data is ciphered with the key and I store everything in the database: the key alias, the Initialization Vector and the ciphered data, all separated with semi-colons.

So the procedure to use another key is straightforward:

  1. generate a new symmetric key in the Java Keystore with another alias
  2. change the configuration file to use this new key alias

But I do not know any command-line tool that can create a symmetric key in a Java keystore. The java keytool utility can only create key pairs.

Is there a command line tool to generate symmetric keys in a Java keystore or should I develop my own tool?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丢了幸福的猪 2025-01-05 10:06:11

自 Java 6 起,keytool 就能够使用 -genseckey 命令。以下是 Java 6 keytool 文档的摘录

-genseckey {-alias 别名} {-keyalg keyalg}
       {-keysize keysize} [-keypass keypass]
       {-storetype storetype} {-keystore 密钥库}
       [-storepass 商店通行证]
       {-providerClassprovider_class_name {-providerArgprovider_arg}}
       {-v} {-受保护} {-Jjavaoption}

生成密钥并将其存储在由别名标识的新KeyStore.SecretKeyEntry中。

keyalg指定用于生成密钥的算法,keysize指定要生成的密钥的大小。 keypass是用于保护密钥的密码。如果未提供密码,系统会提示用户输入密码。如果在提示符下按 RETURN,则密钥密码将设置为与密钥库使用的密码相同的密码。 密钥密码必须至少有 6 个字符长。

因此,以下命令将生成新的 AES 128 位密钥

keytool -genseckey -alias mykey -keyalg AES -keysize 128 \
    -storetype jceks -keystore mykeystore.jks

keytool 命令有一个拼写错误,隐藏了有关 -genseckey 的帮助信息:

% keytool -help
[...]
-genkeypair  [-v] [-protected]
         [-alias <alias>]
         [-keyalg <keyalg>] [-keysize <taille_clé>]
         [-sigalg <sigalg>] [-dname <nomd>]
         [-validity <joursVal>] [-keypass <mot_passe_clé>]
         [-keystore <keystore>] [-storepass <mot_passe_store>]
         [-storetype <storetype>] [-providername <name>]
         [-providerclass <provider_class_name> [-providerarg <arg>]] ...
         [-providerpath <pathlist>]

-genkeypair  [-v] [-protected]
         [-alias <alias>] [-keypass <keypass>]
         [-keyalg <keyalg>] [-keysize <taille_clé>]
         [-keystore <keystore>] [-storepass <mot_passe_store>]
         [-storetype <storetype>] [-providername <name>]
         [-providerclass <provider_class_name> [-providerarg <arg>]] ...
         [-providerpath <pathlist>] 

-genkeypair 命令出现两次。事实上,第二个 -genkeypair 应该读取为 -genseckey。这就是为什么我没有注意到这个命令。

我在 Java 1.6.0_26 中遇到了这个拼写错误。我检查了最新的 Java 6 (1.6.0_31),它也有同样的问题。我还检查了最新的 Java 7,文档问题已修复:

% java -version
java version "1.7.0_03"
Java(TM) SE Runtime Environment (build 1.7.0_03-b04)
Java HotSpot(TM) Server VM (build 22.1-b02, mixed mode)
% keytool -help
 [...]
 -genkeypair         Generates a key pair
 -genseckey          Generates a secret key
 [...]

keytool is able to generate a secret key since Java 6 with the -genseckey command. Here is an excerpt of the Java 6 keytool documentation:

-genseckey {-alias alias} {-keyalg keyalg}
       {-keysize keysize} [-keypass keypass]
       {-storetype storetype} {-keystore keystore}
       [-storepass storepass]
       {-providerClass provider_class_name {-providerArg provider_arg}}
       {-v} {-protected} {-Jjavaoption}

Generates a secret key and stores it in a new KeyStore.SecretKeyEntry identified by alias.

keyalg specifies the algorithm to be used to generate the secret key, and keysize specifies the size of the key to be generated. keypass is a password used to protect the secret key. If no password is provided, the user is prompted for it. If you press RETURN at the prompt, the key password is set to the same password as that used for the keystore. keypass must be at least 6 characters long.

So the following command will generate a new AES 128 bits key

keytool -genseckey -alias mykey -keyalg AES -keysize 128 \
    -storetype jceks -keystore mykeystore.jks

The keytool command has a typo bug that hides the help information about -genseckey:

% keytool -help
[...]
-genkeypair  [-v] [-protected]
         [-alias <alias>]
         [-keyalg <keyalg>] [-keysize <taille_clé>]
         [-sigalg <sigalg>] [-dname <nomd>]
         [-validity <joursVal>] [-keypass <mot_passe_clé>]
         [-keystore <keystore>] [-storepass <mot_passe_store>]
         [-storetype <storetype>] [-providername <name>]
         [-providerclass <provider_class_name> [-providerarg <arg>]] ...
         [-providerpath <pathlist>]

-genkeypair  [-v] [-protected]
         [-alias <alias>] [-keypass <keypass>]
         [-keyalg <keyalg>] [-keysize <taille_clé>]
         [-keystore <keystore>] [-storepass <mot_passe_store>]
         [-storetype <storetype>] [-providername <name>]
         [-providerclass <provider_class_name> [-providerarg <arg>]] ...
         [-providerpath <pathlist>] 

The -genkeypair command appears twice. In fact the second -genkeypair should be read -genseckey. That's why I did not notice the command.

I have encountered this typo bug with Java 1.6.0_26. I have checkd with the latest Java 6 available (1.6.0_31) and it has the same problem. I have also checked with the latest Java 7 and the documentation problem is fixed:

% java -version
java version "1.7.0_03"
Java(TM) SE Runtime Environment (build 1.7.0_03-b04)
Java HotSpot(TM) Server VM (build 22.1-b02, mixed mode)
% keytool -help
 [...]
 -genkeypair         Generates a key pair
 -genseckey          Generates a secret key
 [...]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文