gpt4 book ai didi

java - 生成 GOST 34.10-2001 key 对并将其保存到某个 keystore

转载 作者:搜寻专家 更新时间:2023-11-01 01:44:46 28 4
gpt4 key购买 nike

目前我需要为 GOST 34.10-2001 签名算法生成一个 key 对。很高兴发现充气城堡提供商支持此算法,但我无法生成 key 对并将其保存到任何类型的任何 keystore 。目前我尝试了这个命令(如果 keyalgDSA 并且 sigalg 是 SHA1withDSA,这个命令效果很好):

keytool -genkey -alias test1 -keyalg ECGOST3410 -keysize 512  -sigalg GOST3411withECGOST3410 \
-keypass test_1 -validity 1000 -storetype JKS -keystore test1.jks -storepass test_1 -v \
-provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "bcprov-jdk16-1.46.jar"

但是我有一个错误:

keytool error: java.lang.IllegalArgumentException: unknown key size.
java.lang.IllegalArgumentException: unknown key size.
at sun.security.x509.CertAndKeyGen.generate(CertAndKeyGen.java:134)
at sun.security.tools.KeyTool.doGenKeyPair(KeyTool.java:1156)
at sun.security.tools.KeyTool.doCommands(KeyTool.java:786)
at sun.security.tools.KeyTool.run(KeyTool.java:172)
at sun.security.tools.KeyTool.main(KeyTool.java:166)

当我尝试操作 keysize 或从命令中删除 keysize 选项时,我看到的错误完全相同。但是有一些特殊情况。当我将 keysize 设置为 256 时,我遇到了另一个错误:

keytool error: java.lang.IllegalArgumentException: key size not configurable.
java.lang.IllegalArgumentException: key size not configurable.
at sun.security.x509.CertAndKeyGen.generate(CertAndKeyGen.java:134)
at sun.security.tools.KeyTool.doGenKeyPair(KeyTool.java:1156)
at sun.security.tools.KeyTool.doCommands(KeyTool.java:786)
at sun.security.tools.KeyTool.run(KeyTool.java:172)
at sun.security.tools.KeyTool.main(KeyTool.java:166)

目前我不知道如何生成 key 对以及如何将其保存到 keystore 。我还有一些 Java 代码可以为 GOST 34.10-2001 算法生成 key 对:

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

KeyPairGenerator kpg = KeyPairGenerator.getInstance("ECGOST3410", "BC");
kpg.initialize(new ECGenParameterSpec("GostR3410-2001-CryptoPro-A"));

KeyPair kp = kpg.generateKeyPair();

此代码示例使用 ECGenParameterSpec 类来初始化 key 对生成器,因此我可能应该以某种方式将它提供给 key 工具(-providerArg provider_arg- Jjava选项)?

附言我认为我应该提供曲线名称作为某些参数,但我无法确定应该使用哪个参数。

最佳答案

您将无法使用 keytool 和 BC 创建带有 GOST3410 key 的 keystore 。

keytool 使用的

sun.security.x509.CertAndKeyGen 类不提供使用参数初始化 key 生成器的选项,而 BC GOST3410 key generator需要使用 ECParameterSpec 进行初始化。

您可以创建 key 对+证书并以编程方式将它们放入 keystore :

Security.addProvider( new org.bouncycastle.jce.provider.BouncyCastleProvider() );

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance( "ECGOST3410", "BC" );
keyPairGenerator.initialize( new ECGenParameterSpec( "GostR3410-2001-CryptoPro-A" ) );
KeyPair keyPair = keyPairGenerator.generateKeyPair();

org.bouncycastle.asn1.x500.X500Name subject = new org.bouncycastle.asn1.x500.X500Name( "CN=Me" );
org.bouncycastle.asn1.x500.X500Name issuer = subject; // self-signed
BigInteger serial = BigInteger.ONE; // serial number for self-signed does not matter a lot
Date notBefore = new Date();
Date notAfter = new Date( notBefore.getTime() + TimeUnit.DAYS.toMillis( 365 ) );

org.bouncycastle.cert.X509v3CertificateBuilder certificateBuilder = new org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder(
issuer, serial,
notBefore, notAfter,
subject, keyPair.getPublic()
);
org.bouncycastle.cert.X509CertificateHolder certificateHolder = certificateBuilder.build(
new org.bouncycastle.operator.jcajce.JcaContentSignerBuilder( "GOST3411withECGOST3410" )
.build( keyPair.getPrivate() )
);
org.bouncycastle.cert.jcajce.JcaX509CertificateConverter certificateConverter = new org.bouncycastle.cert.jcajce.JcaX509CertificateConverter();
X509Certificate certificate = certificateConverter.getCertificate( certificateHolder );

KeyStore keyStore = KeyStore.getInstance( "JKS" );
keyStore.load( null, null ); // initialize new keystore
keyStore.setEntry(
"alias",
new KeyStore.PrivateKeyEntry(
keyPair.getPrivate(),
new Certificate[] { certificate }
),
new KeyStore.PasswordProtection( "entryPassword".toCharArray() )
);
keyStore.store( new FileOutputStream( "test.jks" ), "keystorePassword".toCharArray() );

关于java - 生成 GOST 34.10-2001 key 对并将其保存到某个 keystore ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14580340/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com