gpt4 book ai didi

java - 麦克埃利切(FlexiProvider): How can i create KeyPair from byte array?

转载 作者:太空宇宙 更新时间:2023-11-04 07:11:31 25 4
gpt4 key购买 nike

我使用 Flexi Provider(用于 McEliece 算法)生成我的公钥和私钥,然后将其保存到文件中,现在我需要从文件加载 key 对,我可以读取文件并获取公钥字节数组和私钥字节数组,但我不知道如何从中创建 key 对:我用flexiprovider.de对于 McEliece 算法:

用于生成 McEliece key :

import de.flexiprovider.api.keys.KeySpec;
import de.flexiprovider.core.FlexiCoreProvider;
import de.flexiprovider.pqc.FlexiPQCProvider;
import de.flexiprovider.pqc.ecc.mceliece.McElieceKeyFactory;
import de.flexiprovider.pqc.ecc.mceliece.McElieceKeyPairGenerator;

public KeyPair generateKeyPair(){
KeyPair kp = null;
int KeySize = 256;
Security.addProvider(new FlexiCoreProvider());
Security.addProvider(new FlexiPQCProvider());

try
{

McElieceKeyPairGenerator mcElieceKeyPairGenerator = new McElieceKeyPairGenerator();
mcElieceKeyPairGenerator.initialize(KeySize, new SecureRandom());
kp = mcElieceKeyPairGenerator.generateKeyPair();
} catch (Exception e) {
System.out.println(e.ToString());
}
return kp;
}

从字节数组加载 key 对:

public KeyPair readKeyPair(byte[] privateKey, byte[] publicKey){
//?? How can i create KeyPair
}

我尝试使用McElieceKeyFactory,但我不知道如何使用它,它需要KeySpec(de.flexiprovider.api.keys.KeySpec)来生成私钥和公钥,但de.flexiprovider.api.keys.KeySpec是抽象类,我无法从中创建新实例。那我该怎么办?

编辑 1:保存方法:

public void writeFile(byte[] data, String fileName) throws IOException{
FileOutputStream out = new FileOutputStream(fileName);
out.write(data);
out.close();
}

public void saveKeyPair(KeyPair keyPair){
byte[] privateKeyBytes = keyPair.getPrivate().getEncoded();
byte[] publicKeyBytes = keyPair.getPublic().getEncoded();

writeFile(privateKeyBytes,"Private.key");
writeFile(publicKeyBytes,"Public.key");
}

最佳答案

请尝试以下代码。另请注意,我没有直接使用 key 对生成器和 key 工厂实现

KeyPairGenerator mcElieceKeyPairGenerator = KeyPairGenerator.getInstance("McEliece");
mcElieceKeyPairGenerator.initialize(keysize, new SecureRandom());
kp = mcElieceKeyPairGenerator.generateKeyPair();

final byte[] publicKeyData = kp.getPublic().getEncoded();
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyData);
KeyFactory mcElieceKeyFactory = KeyFactory.getInstance("McEliece");
PublicKey regeneratedPublicKey = mcElieceKeyFactory.generatePublic(publicKeySpec);

final byte[] privateKeyData = kp.getPrivate().getEncoded();
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyData);
PrivateKey regeneratedPrivateKey = mcElieceKeyFactory.generatePrivate(privateKeySpec);

关于java - 麦克埃利切(FlexiProvider): How can i create KeyPair from byte array?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20577622/

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