protected Key engineUnwrap(byte[] wrappedKey,String wrappedKeyAlgorithm,int wrappedKeyType) throws InvalidKeyException, NoSuchAlgorithmException {
  if (wrappedKey.length == 0) {
    throw new InvalidKeyException("The wrapped key is empty");
  }
  byte[] buffer=new byte[wrappedKey.length];
  cipher.decrypt(wrappedKey,0,wrappedKey.length,buffer,0);
  for (int i=0; i < buffer.length / 2; i++) {
    byte temp=buffer[i];
    buffer[i]=buffer[buffer.length - 1 - i];
    buffer[buffer.length - 1 - i]=temp;
  }
  iv=new byte[IV_LEN];
  System.arraycopy(buffer,0,iv,0,iv.length);
  cipher.init(true,cipherKey.getAlgorithm(),cipherKey.getEncoded(),iv);
  byte[] buffer2=new byte[buffer.length - iv.length];
  cipher.decrypt(buffer,iv.length,buffer2.length,buffer2,0);
  int keyValLen=buffer2.length - CHECKSUM_LEN;
  byte[] cks=getChecksum(buffer2,0,keyValLen);
  int offset=keyValLen;
  for (int i=0; i < CHECKSUM_LEN; i++) {
    if (buffer2[offset + i] != cks[i]) {
      throw new InvalidKeyException("Checksum comparison failed");
    }
  }
  cipher.init(decrypting,cipherKey.getAlgorithm(),cipherKey.getEncoded(),IV2);
  byte[] out=new byte[keyValLen];
  System.arraycopy(buffer2,0,out,0,keyValLen);
  return ConstructKeys.constructKey(out,wrappedKeyAlgorithm,wrappedKeyType);
}

protected Key engineUnwrap ( byte [ ] wrappedKey , String wrappedKeyAlgorithm , int wrappedKeyType ) throws InvalidKeyException , NoSuchAlgorithmException { if ( wrappedKey . length == 0 ) { throw new InvalidKeyException ( "The wrapped key is empty" ) ; } byte [ ] buffer = new byte [ wrappedKey . length ] ; cipher . decrypt ( wrappedKey , 0 , wrappedKey . length , buffer , 0 ) ; for ( int i = 0 ; i < buffer . length / 2 ; i ++ ) { byte temp = buffer [ i ] ; buffer [ i ] = buffer [ buffer . length - 1 - i ] ; buffer [ buffer . length - 1 - i ] = temp ; } iv = new byte [ IV_LEN ] ; System . arraycopy ( buffer , 0 , iv , 0 , iv . length ) ; cipher . init ( true , cipherKey . getAlgorithm ( ) , cipherKey . getEncoded ( ) , iv ) ; byte [ ] buffer2 = new byte [ buffer . length - iv . length ] ; cipher . decrypt ( buffer , iv . length , buffer2 . length , buffer2 , 0 ) ; int keyValLen = buffer2 . length - CHECKSUM_LEN ; byte [ ] cks = getChecksum ( buffer2 , 0 , keyValLen ) ; int offset = keyValLen ; for ( int i = 0 ; i < CHECKSUM_LEN ; i ++ ) { if ( buffer2 [ offset + i ] != cks [ i ] ) { throw new InvalidKeyException ( "Checksum comparison failed" ) ; } } cipher . init ( decrypting , cipherKey . getAlgorithm ( ) , cipherKey . getEncoded ( ) , IV2 ) ; byte [ ] out = new byte [ keyValLen ] ; System . arraycopy ( buffer2 , 0 , out , 0 , keyValLen ) ; return ConstructKeys . constructKey ( out , wrappedKeyAlgorithm , wrappedKeyType ) ; }
unwrap a previously wrapped key .
