Reputation: 1189
What's a simple way to make AES 128 private_keys
and init vectors
?
I read it was common practice to use UUID, but when I tried to implement:
UUID uuid = UUID.randomUUID() ;
String private_key = uuid.toString();
The error message:
Invalid AES key length: 36 bytes
tells me that uuid.toString() = "38400000-8cf0-11bd-b23e-10b96e4ef00d"
isn't 128 bytes.
Also, how would you create a 16 byte Init vector as a string? Can you also use uuid?
Upvotes: 0
Views: 1409
Reputation: 12085
Keys (encryption generally) is defined over byte byte arrays. You may check the blog for examples
and btw - we are talking about 128 bit (=16 bytes)
What's a simple way to make AES 128 private_keys and init vectors?
The most secure way is using the SecureRandom class, especially for keys. I wouldn't consider using uuid secure enough (it's not random enough)
SecureRandom rnd = new SecureRandom();
byte[] key = new byte[KEY_SIZE / 8];
byte[] iv = new byte[SYMMETRIC_BLOCK_SIZE / 8];
rnd.nextBytes(key);
rnd.nextBytes(iv);
How to create random AES 128 PrivateKey and IV as strings?
If you want textual representation of the key or iv (byte arrays), you may encode them, Base64 or Hex are the most common encodings
Base64.getEncoder().encodeToString(...)
Upvotes: 1