Thanks very much. Using your code and a few other bits and pieces, I've
come up with this, which generates a SecretKey for AES and then wraps it
using PBE:
KeyGenerator generator = KeyGenerator.getInstance("AES");
generator.init(128);
Key keyToBeWrapped = generator.generateKey();
Log.v("tag","input: " + new String(keyToBeWrapped.getEncoded()));
String password = "password";
int iterationCount = 100;
int saltLength = 8;
int keyLength = 256;
SecureRandom random = new SecureRandom();
byte[] salt = new byte[saltLength];
random.nextBytes(salt);
KeySpec keySpec = new PBEKeySpec(password.toCharArray(),
salt, iterationCount, keyLength);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(
"PBKDF2WithHmacSHA1");
SecretKey key = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] iv = new byte[cipher.getBlockSize()];
random.nextBytes(iv);
IvParameterSpec ivParams = new IvParameterSpec(iv);
cipher.init(Cipher.WRAP_MODE, key, ivParams);
byte[] wrappedKey = cipher.wrap(keyToBeWrapped);
Log.v("tag", "wrapped: " + new String(wrappedKey));
cipher.init(Cipher.UNWRAP_MODE, key, ivParams);
Key keyUnwrapped = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
Log.v("tag","unwrapped: " + new String(keyUnwrapped.getEncoded()));
This code works -- but not on Android. It works in a J2SE program linked to
BouncyCastle v. 1.47. Under Android (tested with 4.1 and 4.2) it throws an
exception at the line
cipher.init(Cipher.UNWRAP_MODE, key, ivParams);
The exception is: java.security.InvalidAlgorithmParameterException: no IV
set when one expected.
This is very hard to understand, because as you can see an IV was in fact
set, and as mentioned it works on the desktop. Any thoughts?
Matthew
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en