Exemplo n.º 1
0
  @Test
  public void testSign() throws Exception {
    System.err.println("私钥加密——公钥解密");
    String inputStr = "sign";
    byte[] data = inputStr.getBytes();

    byte[] encodedData = AnguoEncryptUtil.encryptByPrivateKey(data, rSACoder.getPrivateKey());

    byte[] decodedData = AnguoEncryptUtil.decryptByPublicKey(encodedData, rSACoder.getPublicKey());

    String outputStr = new String(decodedData);
    System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr);
    Assert.assertEquals(inputStr, outputStr);

    System.err.println("私钥签名——公钥验证签名");
    // 产生签名
    String sign = AnguoEncryptUtil.sign(decodedData, rSACoder.getPrivateKey());

    System.err.println("签名:\r" + sign);

    // 验证签名
    boolean status = AnguoEncryptUtil.verify(decodedData, rSACoder.getPublicKey(), sign);
    System.err.println("状态:\r" + status);
    Assert.assertEquals(true, status);
  }
Exemplo n.º 2
0
  @Test
  public void test() throws Exception {

    System.err.println("公钥加密——私钥解密");
    String inputStr = "abc";
    byte[] data = inputStr.getBytes();

    byte[] encodedData = AnguoEncryptUtil.encryptByPublicKey(data, rSACoder.getPublicKey());

    byte[] decodedData =
        AnguoEncryptUtil.decryptByPrivateKey(encodedData, rSACoder.getPrivateKey());

    String outputStr = new String(decodedData);
    System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr);
    Assert.assertEquals(inputStr, outputStr);
  }