Discuss / Java / 使用非对称加密交换信息时,应该是使用对方的公钥加密数据,然后发给对方。对方用自己的私钥解密。加上这个注释理解起来好一点。

使用非对称加密交换信息时,应该是使用对方的公钥加密数据,然后发给对方。对方用自己的私钥解密。加上这个注释理解起来好一点。

Topic source
  // 我是bob
        // 用Alice的公钥加密:
        byte[] pk = alice.getPublicKey();
        System.out.println(String.format("public key: %x", new BigInteger(1, pk)));
        byte[] encrypted = alice.encrypt(plain);
        System.out.println(String.format("encrypted: %x", new BigInteger(1, encrypted)));
  // 我是alice
        // 用自己的私钥解密:
        byte[] sk = alice.getPrivateKey();
        System.out.println(String.format("private key: %x", new BigInteger(1, sk)));
        byte[] decrypted = alice.decrypt(encrypted);
        System.out.println(new String(decrypted, "UTF-8"));

  • 1

Reply