Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static byte[] decryptToByteArray(byte[] cipherText, byte[] secretKey)
- throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
- BadPaddingException, InvalidAlgorithmParameterException {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
- SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey, "AES");
- cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(new byte[16]));
- for (int i = 0; i < cipherText.length; i += 16) {
- try {
- baos.write(cipher.doFinal(Arrays.copyOfRange(cipherText, i, i + 16)));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return baos.toByteArray();
- }
- public static byte[] encryptToByteArray(byte[] data, byte[] secretKey)
- throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
- BadPaddingException, InvalidAlgorithmParameterException {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
- SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey, "AES");
- cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(new byte[16]));
- for (int i = 0; i < data.length; i += 16) {
- try {
- baos.write(cipher.doFinal(Arrays.copyOfRange(data, i, i + 16)));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return baos.toByteArray();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement