Class PasswordUtil


  • public class PasswordUtil
    extends Object
    Adds, changes and removes the database password on a 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

    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.

    The forms which take a Path are safe from that. They build a separate file and replace the original only once it is complete, so an interrupted operation leaves the original database as it was. The in place form gives the database a new identity on disk, which drops any hard link to it. The forms which take an open Database are not, because the caller holds the file, so copy it first.

    The database must not be in use, and an open one must be open for writing.

    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
      • setDatabasePassword

        public static void setDatabasePassword​(Path dbFile,
                                               String password)
                                        throws IOException
        Sets the database password on the given database, replacing the file with the result.
        Parameters:
        dbFile - the database to modify, not in use
        password - the new password, which may not be null or empty
        Throws:
        IOException
      • setDatabasePassword

        public static void setDatabasePassword​(Path srcFile,
                                               Path dstFile,
                                               String password)
                                        throws IOException
        Writes a copy of the given database with the given password, leaving the source alone.
        Parameters:
        srcFile - the database to copy, not in use
        dstFile - the file to write, overwritten if it exists
        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
      • removeDatabasePassword

        public static void removeDatabasePassword​(Path dbFile)
                                           throws IOException
        Removes the database password from the given database, replacing the file with the result. Does nothing to the password if it does not have one.
        Parameters:
        dbFile - the database to modify, not in use
        Throws:
        IOException
      • removeDatabasePassword

        public static void removeDatabasePassword​(Path srcFile,
                                                  Path dstFile)
                                           throws IOException
        Writes a copy of the given database with no password, leaving the source alone.
        Parameters:
        srcFile - the database to copy, not in use
        dstFile - the file to write, overwritten if it exists
        Throws:
        IOException