gpt4 book ai didi

Java 加密将原始字节转换为公钥,反之亦然

转载 作者:行者123 更新时间:2023-12-01 23:42:38 24 4
gpt4 key购买 nike

我正在尝试从 byte[] 生成一个 java.security.PublicKey,它只是一个原始 key ——它不在 X.509 或 ASN 中。 1.在其他语言中,人们使用的是 libsodium,它只是给他们一个原始字节[]。我还需要能够将我的 java.security.PublicKey 转换为 byte[] 供他们使用。 java.security 似乎不遗余力地使这个具有挑战性,而不是希望我依赖预编码的 X509 SubjectPublicKeyInfo。我在这里缺少一个简单的答案吗?

最佳答案

Libsodium ,像所有 NaCl 衍生物一样,使用 Curve25519用于其加密操作,即用于 DH key 交换的 X25519 和用于签名的 Ed25519。Java 自 version 11 起支持 X25519 , 自 version 15 起支持 Ed25519 .这两种功能均由 SunEC 提供商提供。

对于原始 key 的导入/导出,最简单的方法是使用一些 BouncyCastle功能。以下代码为 X25519(Java 11 及更高版本)创建 X.509 格式的公共(public)测试 key ,并从中导出原始的 32 字节 key 。然后将原始 key 再次导入 X.509 key :

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

import org.bouncycastle.asn1.edec.EdECObjectIdentifiers;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.crypto.params.X25519PublicKeyParameters;
import org.bouncycastle.crypto.util.PublicKeyFactory;
import org.bouncycastle.util.encoders.Hex;
...
// Generate public test key in X.509 format
PublicKey publicKey = loadPublicKey("X25519");
byte[] publicKeyBytes = publicKey.getEncoded();
System.out.println(Base64.getEncoder().encodeToString(publicKeyBytes)); // X.509-key, check this in an ASN.1 Parser, e.g. https://lapo.it/asn1js/

// PublicKey to raw key
X25519PublicKeyParameters x25519PublicKeyParameters = (X25519PublicKeyParameters)PublicKeyFactory.createKey(publicKeyBytes);
byte[] rawKey = x25519PublicKeyParameters.getEncoded();
System.out.println(Hex.toHexString(rawKey)); // equals the raw 32 bytes key from the X.509 key

// Raw key to PublicKey
KeyFactory keyFactory = KeyFactory.getInstance("X25519");
SubjectPublicKeyInfo subjectPublicKeyInfo = new SubjectPublicKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X25519), rawKey);
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(subjectPublicKeyInfo.getEncoded());
publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
publicKeyBytes = publicKey.getEncoded();
System.out.println(Base64.getEncoder().encodeToString(publicKeyBytes)); // equals the X.509 key from above

创建 X.509 测试 key 的位置:

private static PublicKey loadPublicKey(String algorithm) throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return keyPair.getPublic();
}

和 Ed25519(Java 15 及更高版本)的模拟。

X25519 和 Ed25519 也可以单独使用 BouncyCaSTLe 中的类实现,例如org.bouncycaSTLe.math.ec.rfc7748.X25519( key 协商),org.bouncycaSTLe.crypto.generators.X25519KeyPairGenerator( key 生成),org.bouncycaSTLe .crypto.params.X25519PublicKeyParameters( key 容器),以及 Ed25519 的模拟类。

关于Java 加密将原始字节转换为公钥,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64777136/

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