java - Java Keystore 中的 Lazysodium 密钥
问题描述
我正在使用 Lazysodium 库 ( https://terl.gitbook.io/lazysodium/ ) 从 Java 访问 libsodium,特别是用于 Ed25519 数字签名。
我还希望能够将密钥对存储在标准 Java 密钥库中。但是,libsodium 使用字节数组而不是 JCA Keypair 实例,因此不清楚如何实现这一点。
特别是,您如何:
- 将与 Libsodium 一起使用的字节数组 Ed25519 密钥转换为 JCA KeyPair?
- 将 JCA 密钥对转换回 libsodium 的适当字节数组?
解决方案
原始私有 Ed25519 密钥和 a 之间的转换java.security.PrivateKey
是可能的,例如使用 BouncyCastle,如下所示:
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.KeyFactory;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.edec.EdECObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters;
import org.bouncycastle.crypto.util.PrivateKeyFactory;
import org.bouncycastle.util.encoders.Hex;
...
// Generate private test key
PrivateKey privateKey = loadPrivateKey("Ed25519");
byte[] privateKeyBytes = privateKey.getEncoded();
System.out.println(Base64.getEncoder().encodeToString(privateKeyBytes)); // PKCS#8-key, check this in an ASN.1 Parser, e.g. https://lapo.it/asn1js/
// java.security.PrivateKey to raw Ed25519 key
Ed25519PrivateKeyParameters ed25519PrivateKeyParameters = (Ed25519PrivateKeyParameters)PrivateKeyFactory.createKey(privateKeyBytes);
byte[] rawKey = ed25519PrivateKeyParameters.getEncoded();
System.out.println(Hex.toHexString(rawKey)); // equals the raw 32 bytes key from the PKCS#8 key
// Raw Ed25519 key to java.security.PrivateKey
KeyFactory keyFactory = KeyFactory.getInstance("Ed25519");
PrivateKeyInfo privateKeyInfo = new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed25519), new DEROctetString(rawKey));
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKeyInfo.getEncoded());
PrivateKey privateKeyReloaded = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
byte[] privateKeyBytesReloaded = privateKeyReloaded.getEncoded();
System.out.println(Base64.getEncoder().encodeToString(privateKeyBytesReloaded)); // equals the PKCS#8 key from above
其中私有测试密钥是通过以下方式生成的:
private static PrivateKey loadPrivateKey(String algorithm) throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return keyPair.getPrivate();
}
对于 X25519 也是类似的,全部替换Ed25519
为X25519
.
可以在此处找到原始公共 X25519 密钥和java.security.PublicKey
(反之亦然)之间的转换。对于原始公共 Ed25519 密钥,过程类似,其中每个都必须替换为.X25519
Ed25519
但是,我还没有测试过 Ed25519 或 X25519 密钥是否可以存储在密钥库中。
推荐阅读
- vue.js - 安装 vue-storefront 时出错
- javascript - 创建一个新的 Angular 项目
- string - bash脚本中奇怪/意外的字符串替换
- pandas - 多处理熊猫中的模糊匹配
- javascript - WooCommerce Nodejs Wrapper 返回未定义
- flutter - 颤振如何在标记单击时获取标记ID?
- python - 如何将来自浏览器的 JSON 响应保存到 python 变量或文件中
- json - 如何使用 Moshi 将 json 中的 int 列表转换为枚举列表/数组?
- python - 如何在从文本文件加载的变量上使用变量?
- vega - Vega 条形图中的多个组,带有单个条形