首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将ECIES公钥传输给客户端

将ECIES公钥传输给客户端
EN

Stack Overflow用户
提问于 2017-07-10 06:04:48
回答 1查看 398关注 0票数 0

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

代码语言:javascript
复制
    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对象时,它会抛出异常:

代码语言:javascript
复制
    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的属性(为了避免异常,禁用org.bouncycastle.math.ec.WNafL2RMultiplier)(通过引用链: 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") )

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-10 08:55:43

问题是KeyPair不能序列化为json。您只需要发送公钥内容。

代码语言:javascript
复制
 public class MobileData{
        byte publicKeyEncoded[];
 }
代码语言:javascript
复制
MobileData data = new MobileData();
data.setPublicKeyEncoded(keyPair.getPublic().getEncoded());

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45004766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档