Exemplo n.º 1
0
  /**
   * 用户参数验证
   *
   * @throws Exception
   */
  @Test
  public void signTest1() throws Exception {
    String DEFAULT_PUBLIC_KEY =
        "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDveyqk24CbH/mAgEbUD03pVYQ1"
            + "\r"
            + "pTbhZf+9hnVJMtVfYEHf33i53Myq/qVkPsAI6/6iCHgOXhR1RGRmCXtZnTELoPVn"
            + "\r"
            + "K3nRQJVFpeVmH3s4i6q6BXaTt6i0RTfWQzeP9SgUtfSPzoAxoGM4DfRw3U5wYvzH"
            + "\r"
            + "v60/Mvd4IvWafQeYvQIDAQAB"
            + "\r";

    String DEFAULT_PRIVATE_KEY =
        "MIICXAIBAAKBgQDveyqk24CbH/mAgEbUD03pVYQ1pTbhZf+9hnVJMtVfYEHf33i5"
            + "\r"
            + "3Myq/qVkPsAI6/6iCHgOXhR1RGRmCXtZnTELoPVnK3nRQJVFpeVmH3s4i6q6BXaT"
            + "\r"
            + "t6i0RTfWQzeP9SgUtfSPzoAxoGM4DfRw3U5wYvzHv60/Mvd4IvWafQeYvQIDAQAB"
            + "\r"
            + "AoGAO5hl+1KYhYIGgADsH1eTpu5eEU+FAcB1TP/J7iZVTP/SRNkC3RXiZOcr1296"
            + "\r"
            + "MH4yBrae0cx9wNT9Oxs+9AUXL/dmPnJh36+4Rg4P1aHl65Q0hvuq7UUwCqPXG+AT"
            + "\r"
            + "RjIP57htK/E/0dyvMicXfMr1Ip6+gUFcYgy3nChm9phfz+0CQQD6+79KR82d6Amg"
            + "\r"
            + "1Rk8yhXMND8eO8t2CzqBeZlDJlmOmBwJhLjOglTcCeDrecAUQgcq+euQUB0Mca4l"
            + "\r"
            + "Z48u1iOvAkEA9ESQRwgSNWFP8xOqDPBvBFbE/UT4caB9/SqJDHMgDwj/XvV6IXoN"
            + "\r"
            + "29yprg+z3uJJi2ANZZIalUDpxsFXbg0pUwJBANV6zHqiGIL3mzjyCUVrnp7S0d0l"
            + "\r"
            + "fyoo4tq+U16KgCKJv09ZVNhSg1umC2o/ZOHWR8KGUZeujQbIqxelvmRYQIECQEC7"
            + "\r"
            + "yuCIKwsqdt2cYHx9W25y8FGObajvN3RYSWmbOOvDHqozs+IbToDtwan3T1vJ7GOb"
            + "\r"
            + "WPGJ/rF/OnUJ1/m6UzECQGxVPKOcSUsQI1NKTLHvATgGxAdvQNii1b3DjjqNLFDN"
            + "\r"
            + "aBtbG4svFr0t7HLkilUQnvex3oJkTsp/VYBxTbSSLVU="
            + "\r";

    String timestamp = String.valueOf("11");
    String uuid = "22";

    Sign sign = new Sign();

    sign.setTimestamp(timestamp);
    sign.setUuid(uuid);

    // 签名
    Sign sign2 = AnguoAppUtil.enSign(sign, DEFAULT_PUBLIC_KEY);

    System.out.println("加密:" + sign2);

    Sign signbean = AnguoAppUtil.deSign(sign2, DEFAULT_PRIVATE_KEY);

    System.out.println("解密:" + signbean);
    Assert.assertEquals(signbean.getTimestamp(), sign.getTimestamp());
  }
Exemplo n.º 2
0
  /**
   * 用户参数验证
   *
   * @throws Exception
   */
  @Test
  public void signTest2() throws Exception {
    String DEFAULT_PUBLIC_KEY =
        "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDveyqk24CbH/mAgEbUD03pVYQ1"
            + "\r"
            + "pTbhZf+9hnVJMtVfYEHf33i53Myq/qVkPsAI6/6iCHgOXhR1RGRmCXtZnTELoPVn"
            + "\r"
            + "K3nRQJVFpeVmH3s4i6q6BXaTt6i0RTfWQzeP9SgUtfSPzoAxoGM4DfRw3U5wYvzH"
            + "\r"
            + "v60/Mvd4IvWafQeYvQIDAQAB"
            + "\r";

    String DEFAULT_PRIVATE_KEY =
        "MIICXAIBAAKBgQDveyqk24CbH/mAgEbUD03pVYQ1pTbhZf+9hnVJMtVfYEHf33i5"
            + "\r"
            + "3Myq/qVkPsAI6/6iCHgOXhR1RGRmCXtZnTELoPVnK3nRQJVFpeVmH3s4i6q6BXaT"
            + "\r"
            + "t6i0RTfWQzeP9SgUtfSPzoAxoGM4DfRw3U5wYvzHv60/Mvd4IvWafQeYvQIDAQAB"
            + "\r"
            + "AoGAO5hl+1KYhYIGgADsH1eTpu5eEU+FAcB1TP/J7iZVTP/SRNkC3RXiZOcr1296"
            + "\r"
            + "MH4yBrae0cx9wNT9Oxs+9AUXL/dmPnJh36+4Rg4P1aHl65Q0hvuq7UUwCqPXG+AT"
            + "\r"
            + "RjIP57htK/E/0dyvMicXfMr1Ip6+gUFcYgy3nChm9phfz+0CQQD6+79KR82d6Amg"
            + "\r"
            + "1Rk8yhXMND8eO8t2CzqBeZlDJlmOmBwJhLjOglTcCeDrecAUQgcq+euQUB0Mca4l"
            + "\r"
            + "Z48u1iOvAkEA9ESQRwgSNWFP8xOqDPBvBFbE/UT4caB9/SqJDHMgDwj/XvV6IXoN"
            + "\r"
            + "29yprg+z3uJJi2ANZZIalUDpxsFXbg0pUwJBANV6zHqiGIL3mzjyCUVrnp7S0d0l"
            + "\r"
            + "fyoo4tq+U16KgCKJv09ZVNhSg1umC2o/ZOHWR8KGUZeujQbIqxelvmRYQIECQEC7"
            + "\r"
            + "yuCIKwsqdt2cYHx9W25y8FGObajvN3RYSWmbOOvDHqozs+IbToDtwan3T1vJ7GOb"
            + "\r"
            + "WPGJ/rF/OnUJ1/m6UzECQGxVPKOcSUsQI1NKTLHvATgGxAdvQNii1b3DjjqNLFDN"
            + "\r"
            + "aBtbG4svFr0t7HLkilUQnvex3oJkTsp/VYBxTbSSLVU="
            + "\r";

    String localUserParam =
        new String(
            AnguoEncryptUtil.decryptBASE64(
                "eyJzaWduIjoiRzluUmxyNll0U0ZVL3JoSkZCUk5mdW9tSmY1ckFCSnBDRmhRQTFTcWI1T2JBSjBVQk82d05DbHJJTFBHUk1Ic0FLMUxJVVcyZ1owUjBBWXdodktYZGFrYjFST1RqOTZvNk9XVlZnQm10L29UWUI5b1VuNWRZNkF4ODhMclR1ZEwrbVRxTjQvaXNLUjg1dDJCM2pKZi9vVWV2QmNHWFlMeUh3RHZqb0ZEN0QwPSIsInRpbWVzdGFtcCI6IjIwMTQwOTIyIiwidXVpZCI6bnVsbH0="));

    // 获得签名数据
    Sign loginSign = AnguoJsonUtil.fromJson(localUserParam, Sign.class);

    // 解密
    loginSign = AnguoAppUtil.deSign(loginSign, DEFAULT_PRIVATE_KEY);

    String timestamp = String.valueOf(System.currentTimeMillis());
    String uuid = UUID.randomUUID().toString();

    Sign sign = new Sign();

    sign.setTimestamp(timestamp);
    sign.setUuid(uuid);

    // 签名
    Sign sign2 = AnguoAppUtil.enSign(sign, DEFAULT_PUBLIC_KEY);

    System.out.println("加密:" + sign2);

    Sign signbean = AnguoAppUtil.deSign(sign2, DEFAULT_PRIVATE_KEY);

    System.out.println("解密:" + signbean);
    Assert.assertEquals(signbean.getTimestamp(), sign.getTimestamp());
  }
Exemplo n.º 3
0
  @Test
  public void test3() throws Exception {
    String DEFAULT_PUBLIC_KEY =
        "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDveyqk24CbH/mAgEbUD03pVYQ1"
            + "\r"
            + "pTbhZf+9hnVJMtVfYEHf33i53Myq/qVkPsAI6/6iCHgOXhR1RGRmCXtZnTELoPVn"
            + "\r"
            + "K3nRQJVFpeVmH3s4i6q6BXaTt6i0RTfWQzeP9SgUtfSPzoAxoGM4DfRw3U5wYvzH"
            + "\r"
            + "v60/Mvd4IvWafQeYvQIDAQAB"
            + "\r";

    String DEFAULT_PRIVATE_KEY =
        "MIICXAIBAAKBgQDveyqk24CbH/mAgEbUD03pVYQ1pTbhZf+9hnVJMtVfYEHf33i5"
            + "\r"
            + "3Myq/qVkPsAI6/6iCHgOXhR1RGRmCXtZnTELoPVnK3nRQJVFpeVmH3s4i6q6BXaT"
            + "\r"
            + "t6i0RTfWQzeP9SgUtfSPzoAxoGM4DfRw3U5wYvzHv60/Mvd4IvWafQeYvQIDAQAB"
            + "\r"
            + "AoGAO5hl+1KYhYIGgADsH1eTpu5eEU+FAcB1TP/J7iZVTP/SRNkC3RXiZOcr1296"
            + "\r"
            + "MH4yBrae0cx9wNT9Oxs+9AUXL/dmPnJh36+4Rg4P1aHl65Q0hvuq7UUwCqPXG+AT"
            + "\r"
            + "RjIP57htK/E/0dyvMicXfMr1Ip6+gUFcYgy3nChm9phfz+0CQQD6+79KR82d6Amg"
            + "\r"
            + "1Rk8yhXMND8eO8t2CzqBeZlDJlmOmBwJhLjOglTcCeDrecAUQgcq+euQUB0Mca4l"
            + "\r"
            + "Z48u1iOvAkEA9ESQRwgSNWFP8xOqDPBvBFbE/UT4caB9/SqJDHMgDwj/XvV6IXoN"
            + "\r"
            + "29yprg+z3uJJi2ANZZIalUDpxsFXbg0pUwJBANV6zHqiGIL3mzjyCUVrnp7S0d0l"
            + "\r"
            + "fyoo4tq+U16KgCKJv09ZVNhSg1umC2o/ZOHWR8KGUZeujQbIqxelvmRYQIECQEC7"
            + "\r"
            + "yuCIKwsqdt2cYHx9W25y8FGObajvN3RYSWmbOOvDHqozs+IbToDtwan3T1vJ7GOb"
            + "\r"
            + "WPGJ/rF/OnUJ1/m6UzECQGxVPKOcSUsQI1NKTLHvATgGxAdvQNii1b3DjjqNLFDN"
            + "\r"
            + "aBtbG4svFr0t7HLkilUQnvex3oJkTsp/VYBxTbSSLVU="
            + "\r";

    System.err.println("公钥加密——私钥解密");

    String timestamp = String.valueOf(System.currentTimeMillis());
    String uuid = UUID.randomUUID().toString();

    Sign sign = new Sign();

    sign.setTimestamp(timestamp);
    sign.setUuid(uuid);

    // String sign1=uuid+"|"+timestamp;

    String inputStr = uuid + "|" + timestamp;
    byte[] data = inputStr.getBytes();

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

    String sendStr = AnguoEncryptUtil.encryptBASE64(encodedData);

    System.out.println("传输字符串" + sendStr);

    byte[] aaa = AnguoEncryptUtil.decryptBASE64(sendStr);

    byte[] decodedData = AnguoEncryptUtil.decryptByPrivateKey(aaa, DEFAULT_PRIVATE_KEY);

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