*/
public byte[] decryptKeyblob(byte[] formattedKey, String passphrase)
throws InvalidKeyException {
try {
byte[] keyblob = getKeyBlob(formattedKey);
ByteArrayReader bar = new ByteArrayReader(keyblob);
String type = bar.readString();
if (type.equalsIgnoreCase("3des-cbc")) {
// Decrypt the key
byte[] keydata = makePassphraseKey(passphrase);
byte[] iv = new byte[8];
if (type.equals("3DES-CBC")) {
bar.read(iv);
}
keyblob = bar.readBinaryString();
Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
KeySpec keyspec = new DESedeKeySpec(keydata);
Key key = SecretKeyFactory.getInstance("DESede").generateSecret(keyspec);
cipher.init(Cipher.DECRYPT_MODE, key,
new IvParameterSpec(iv, 0, cipher.getBlockSize()));
ByteArrayReader data = new ByteArrayReader(cipher.doFinal(
keyblob));
if (data.readInt() == cookie) {
keyblob = data.readBinaryString();
} else {
throw new InvalidKeyException(
"The host key is invalid, check the passphrase supplied");
}
} else {