(String mode, boolean encrypt, String input, boolean inBase64, String key, String iv, boolean outBase64)
| 27 | } |
| 28 | |
| 29 | public static String aes(String mode, boolean encrypt, String input, boolean inBase64, String key, String iv, boolean outBase64) { |
| 30 | try { |
| 31 | byte[] keyBuf = key.getBytes(); |
| 32 | if (keyBuf.length < 16) keyBuf = Arrays.copyOf(keyBuf, 16); |
| 33 | byte[] ivBuf = iv == null ? new byte[0] : iv.getBytes(); |
| 34 | if (ivBuf.length < 16) ivBuf = Arrays.copyOf(ivBuf, 16); |
| 35 | Cipher cipher = Cipher.getInstance(mode + "Padding"); |
| 36 | SecretKeySpec keySpec = new SecretKeySpec(keyBuf, "AES"); |
| 37 | if (iv == null) cipher.init(encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec); |
| 38 | else cipher.init(encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(ivBuf)); |
| 39 | byte[] inBuf = inBase64 ? Base64.decode(input.replaceAll("_", "/").replaceAll("-", "+"), Base64.DEFAULT) : input.getBytes(StandardCharsets.UTF_8); |
| 40 | return outBase64 ? Base64.encodeToString(cipher.doFinal(inBuf), Base64.NO_WRAP) : new String(cipher.doFinal(inBuf), StandardCharsets.UTF_8); |
| 41 | } catch (Exception e) { |
| 42 | e.printStackTrace(); |
| 43 | return ""; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public static String rsa(String mode, boolean pub, boolean encrypt, String input, boolean inBase64, String key, boolean outBase64) { |
| 48 | try { |
no test coverage detected