SecureApiTest.java 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package com.sqx;
  2. import icu.xuyijie.secureapi.cipher.CipherAlgorithmEnum;
  3. import icu.xuyijie.secureapi.cipher.CipherUtils;
  4. /**
  5. * @author : codingliang
  6. * @date : 2024-08-07 11:01
  7. */
  8. public class SecureApiTest {
  9. public static void main(String[] args) {
  10. test2();
  11. }
  12. public static void test2() {
  13. CipherUtils cipherUtils = new CipherUtils(CipherAlgorithmEnum.AES_CBC_PKCS5);
  14. // 生成 key
  15. // String key = cipherUtils.getRandomSecreteKey();
  16. // // 生成偏移量(CBC模式需要,ECB模式不需要)
  17. // String iv = cipherUtils.getRandomIv();
  18. //
  19. // System.out.println(key + " " + iv);
  20. // 生成 key
  21. String key = "oURKtBcNU6axnNoebKkuPqkCn6ivhE9qbg9FpeNnfpo=";
  22. // 生成偏移量(CBC模式需要,ECB模式不需要)
  23. String iv = "eW1xeThJSkprcEZUTGh1Ng==";
  24. String content = "{\"msg\":\"success\",\"code\":0,\"data\":{\"id\":579,\"userId\":2049,\"money\":1275.39}}";
  25. String encrypt = cipherUtils.encrypt(content, key, iv);
  26. System.out.println(encrypt);
  27. String decrypt = cipherUtils.decrypt("RPX3_v1bTP4fLfrEMWHegBp_pJPh8TGDaN-SHZFjyyK_HLRIAt_wQrgWX95ZeHDKoTgvtRYcMKaTRs5YHDXFZM9GChuubOvCkwygP1ubQl4=", key, iv);
  28. System.out.println(decrypt);
  29. }
  30. public static void test1() {
  31. CipherUtils cipherUtils = new CipherUtils(CipherAlgorithmEnum.AES_CBC_PKCS5, false);
  32. // 生成 key
  33. String key = "87OGRxGq86S9QO6HJSe78dK0CpuSFVz1TkhnkM/MjE0=";
  34. // 生成偏移量(CBC模式需要,ECB模式不需要)
  35. String iv = "UjNyWWxLOWxKTEgzNFFwZw==";
  36. String content = "{\"msg\":\"success\",\"code\":0,\"data\":{\"id\":579,\"userId\":2049,\"money\":1275.39}}";
  37. String encrypt = cipherUtils.encrypt(content, key, iv);
  38. System.out.println(encrypt);
  39. String decrypt = cipherUtils.decrypt(encrypt, key, iv);
  40. System.out.println(decrypt);
  41. }
  42. // dzUjvlKSwM2V3gg_l_dvGFKdJF84W9cBWQxXKM7NqzMNRAYdEsiF5DJ60iAzNKeDWgVRjR5X2-mqdlVVdEJ5BLfE3gA2lzWn73mnvHNq1R4=
  43. // dzUjvlKSwM2V3gg_l_dvGFKdJF84W9cBWQxXKM7NqzMNRAYdEsiF5DJ60iAzNKeDWgVRjR5X2-mqdlVVdEJ5BLfE3gA2lzWn73mnvHNq1R4=
  44. }