我是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:未找到针对类org.bouncycastle.math.ec.WNafL2RMultiplier的序列化器,也未发现创建BeanSerializer的属性(为避免异常,请禁用SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS))(通过参考链:MobileData [“ keyPair”]-> java.security.KeyPair [“ public”]-> org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey [“ parameters]]-> org.bouncycastle.jce.spec.ECParameterSpec [”曲线“]-> org.bouncycastle.math.ec.custom.sec.SecP256R1Curve [”乘数“])


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

最佳答案

问题是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字符串中

09-16 10:00