Class PasswordUtil


  • public class PasswordUtil
    extends Object
    Adds, changes and removes the database password on an open database. The current password can be read with Database.getDatabasePassword(), which needs nothing from this library.

    Only jet 4 databases (mdb files written by access 2000 and 2003) are supported, and the other formats throw rather than write something ms access cannot open:

    • jet 3 stores the password field without the creation date mask, and has not been verified
    • in an accdb, a database password means the file is encrypted, which is a different mechanism entirely (see CryptCodecProvider)
    • msisam (money) files feed the password into the page encoding key

    Neither operation is atomic. Setting a password changes the page 0 header and rewrites every SID in the database, and the two have to agree or ms access prompts for the password and then refuses to open the file. An interruption part way through leaves the database in exactly that state, and re-running the operation does not repair it, because the key is derived from the header the run starts with. Copy the file first.

    The database must be open for writing and must not be in concurrent use.

    Author:
    James Ahlborn
    • Method Detail

      • setDatabasePassword

        public static void setDatabasePassword​(Database db,
                                               String password)
                                        throws IOException
        Sets the database password on the given database, replacing any existing one.
        Parameters:
        db - the database to modify, open for writing
        password - the new password, which may not be null or empty
        Throws:
        IOException
      • removeDatabasePassword

        public static void removeDatabasePassword​(Database db)
                                           throws IOException
        Removes the database password from the given database. Does nothing if it does not have one.
        Parameters:
        db - the database to modify, open for writing
        Throws:
        IOException