(String value, byte[] key, byte[] iv, byte[] ad, boolean encrypt)
| 793 | } |
| 794 | |
| 795 | private static String transform(String value, byte[] key, byte[] iv, byte[] ad, boolean encrypt) |
| 796 | throws InvalidCipherTextException, IOException { |
| 797 | GCMSIVBlockCipher cipher = new GCMSIVBlockCipher(new AESEngine()); |
| 798 | AEADParameters aead = new AEADParameters(new KeyParameter(key), 128, iv, ad); |
| 799 | cipher.init(encrypt, aead); |
| 800 | |
| 801 | byte[] input = (encrypt |
| 802 | ? compress(value.getBytes()) |
| 803 | : Base64.decode(value, Base64.NO_PADDING | Base64.NO_WRAP)); |
| 804 | int outputLength = cipher.getOutputSize(input.length); |
| 805 | byte[] output = new byte[outputLength]; |
| 806 | int outputOffset = cipher.processBytes(input, 0, input.length, output, 0); |
| 807 | outputOffset += cipher.doFinal(output, outputOffset); // tag |
| 808 | if (outputOffset != outputLength) |
| 809 | throw new IllegalStateException("offset=" + outputOffset + "/" + outputLength); |
| 810 | return (encrypt |
| 811 | ? Base64.encodeToString(output, Base64.NO_PADDING | Base64.NO_WRAP) |
| 812 | : new String(decompress(output))); |
| 813 | } |
| 814 | |
| 815 | public static byte[] compress(byte[] data) throws IOException { |
| 816 | try (ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length)) { |
no test coverage detected