0

I have a 32 length hex string in Java and need to make an IV to encrypt. I know dart has a function IV.fromBase16, and I need something like that in Java.

I tried reverse engineering through flutter, but nothing made sense. I'm new to this part of development.

I already tried codec jar but it was worthless. I googled for questions to transform 32 to 16, but all that I have found was how to transform a string in hex values.

Also, I need to use AES and CBC, but I'm not aware if this is possible in Java due to the string length.

Thetha
  • 35
  • 3
  • What are you asking exactly? How to decode a hexadecimal string into a byte array? It looks like you're conflating several things, like the length of the string (32) with the base of a hexadecimal number (16). – Erwin Bolwidt Nov 22 '19 at 21:36

1 Answers1

-1

You can use the following libraries for encryption in java:

import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.FileOutputStream;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.util.Base64;

To generate the IV, we use the SecureRandom class. The block size required depends on the AES encryption block size. For the default block size of 128 bits, we need an initialization vector of 16 bytes.

From the initialization vector, we create an IvParameterSpec which is required when creating the Cipher.

byte[] iv = new byte[128/8];
srandom.nextBytes(iv);
IvParameterSpec ivspec = new IvParameterSpec(iv);

For reference: https://www.novixys.com/blog/java-aes-example/

Rami
  • 67
  • 5
  • Thanks, but i'm using and existing IV from a client request, and it is a 32b string... i need a way to transform in a 16b pattern – Guilherme Leão Nov 22 '19 at 21:24
  • The IV length for AES depends on the block size (not on type AES-128, AES-256), which is fixed to 128 bits, hence the IV of 16 bytes. You should really check [this](https://stackoverflow.com/a/31147653/12188799) – Javier Silva Ortíz Nov 22 '19 at 21:29