gpt4 book ai didi

java - 将 ECIES 公钥传输给客户端

转载 作者:行者123 更新时间:2023-12-02 12:39:06 27 4
gpt4 key购买 nike

我是 ECIES 新手,并且使用 ECIES 算法进行加密和解密。下面是我用于加密和解密机制的代码片段。

    public static void main(String[] args) throws Exception {

// Server Side Generates KeyPair
KeyPair keyPair = serverSideKeyGeneration();

// Client receives the KeyPair or Public Key before sending actual call to server
String originalString = "Hello";
byte[] ecryptedBase64Data = clientSideCodeToGenerateEncryptedData(originalString, keyPair);
System.out.println("Encrypted Data" + ecryptedBase64Data);

// Server receives the encrypted Data and decrypt using Private Key
String originalValue = decryptEncodedString(keyPair, ecryptedBase64Data);
System.out.println(originalValue);

}

private static byte[] clientSideCodeToGenerateEncryptedData(String originalString, KeyPair keyPair) throws Exception{
Cipher cipher = Cipher.getInstance("ECIES");
cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
byte[] ecryptedBase64Data = Base64.encode(cipher.doFinal(originalString.getBytes("UTF-8")));
return ecryptedBase64Data;

}

private static KeyPair serverSideKeyGeneration() throws Exception {
Security.addProvider(new BouncyCastleProvider());

KeyPairGenerator kpg = KeyPairGenerator.getInstance("ECIES");
kpg.initialize(new ECGenParameterSpec("secp256r1"));

// Key pair to store public and private key
KeyPair keyPair = kpg.generateKeyPair();

// System.out.println(keyPair.getPublic());
// System.out.println(keyPair.getPrivate());

return keyPair;

}

private static String decryptEncodedString(KeyPair keyPair, byte[] ret) throws Exception {
Cipher iesCipherServer = Cipher.getInstance("ECIES");
iesCipherServer.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
String originalValue = new String(iesCipherServer.doFinal(Base64.decode(ret)));
return originalValue;

}

上面的代码片段工作正常,但我想要的是,在客户端和服务器之间进行任何通信之前,客户端将发送启动请求并在结束时接收公钥。然后将使用公钥加密有效负载并将其发送到服务器,服务器收到后将使用之前生成的私钥解密数据。

当我使用下面的代码片段将 keyPair 存储为 JSON 对象时,它会抛出异常:

    MobileData data = new MobileData();
data.setKeyPair(keyPair);

ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(data);

异常:

org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.bouncycastle.math.ec.WNafL2RMultiplier and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: MobileData["keyPair"]->java.security.KeyPair["public"]->org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey["parameters"]->org.bouncycastle.jce.spec.ECParameterSpec["curve"]->org.bouncycastle.math.ec.custom.sec.SecP256R1Curve["multiplier"])

如何将公钥发送给客户端?

最佳答案

问题是KeyPair无法序列化为json。您只需发送公钥内容

 public class MobileData{
byte publicKeyEncoded[];
}
<小时/>
MobileData data = new MobileData();
data.setPublicKeyEncoded(keyPair.getPublic().getEncoded());

ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(data);

公钥将被编码为base64到json字符串中

关于java - 将 ECIES 公钥传输给客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45004766/

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