Working with PST Password Protection Properties

Check for Password protection

The MapiPropertyTag.PR_PST_PASSWORD value from the MapiPropertyTag class is used to check if a file is password protected.

The CRC-32 hash of the password string is stored in the PidTagPstPassword (tag = 0x67ff0003) property in the MessageStore. If this property exists and is non-zero, then the PST is password protected.

The following code snippet shows how to check if a PST file is password protected and whether the given string is a valid password for that PST.

The code snippet below shows two functions, the first checks if the PST is password-protected, and the second shows how to check whether a provided password is correct or not.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
private static boolean isPasswordProtected(PersonalStorage pst) {
// If the property exists and is nonzero, then the PST file is password protected.
if (pst.getStore().getProperties().contains(MapiPropertyTag.PR_PST_PASSWORD)) {
long passwordHash = pst.getStore().getProperties().get_Item(MapiPropertyTag.PR_PST_PASSWORD).getLong();
return passwordHash != 0;
}
return false;
}
private static boolean isPasswordValid(String password, PersonalStorage pst) {
// If the property exists and is nonzero, then the PST file is password protected.
if (pst.getStore().getProperties().contains(MapiPropertyTag.PR_PST_PASSWORD)) {
// The property value contains the CRC-32 hash of the password string of PST.
long passwordHash = pst.getStore().getProperties().get_Item(MapiPropertyTag.PR_PST_PASSWORD).getLong();
CRC32 crc = new CRC32();
crc.update(password.getBytes());
return passwordHash != 0 && passwordHash == crc.getValue();
}
return false;
}

Remove/Reset the PR_PST_PASSWORD Property

Removal of the PR_PST_PASSWORD property cannot be achieved as other properties are removed from a message store. Instead, we need to set its value to zero (0) in order to get it removed. The “Store” property of the PST class allows access to the store properties of PST instead of MessageStoreProperties of PST in this case.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
public static void PSTPasswordRemove() {
PersonalStorage pst = PersonalStorage.fromFile(dataDir + "PersonalStorage.pst");
if (pst.getStore().getProperties().contains(MapiPropertyTag.PR_PST_PASSWORD)) {
MapiProperty property = new MapiProperty(MapiPropertyTag.PR_PST_PASSWORD, getBytes((int) 0));
pst.getStore().setProperty(property);
}
}
//Helper method
public static byte[] getBytes(int value) {
ByteBuffer buffer = ByteBuffer.allocate(4).order(ByteOrder.nativeOrder());
buffer.putInt(value);
return buffer.array();
}

Set/Change PST Password

The following code snippet shows you how to set a password on PST files.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
PersonalStorage pst = PersonalStorage.create(dataDir + "PersonalStorage_out.pst", FileFormatVersion.Unicode);
{
// Set the password
String password = "Password1";
pst.getStore().changePassword(password);
// Remove the password
pst.getStore().changePassword(null);
}

Password Verification for Password Protected PST Files

Aspose.Email enables the developers to check if the PST file is password protected and to check if the given password is correct or not. For this, the API provides the PersonalStorage.Store.IsPasswordProtected property and PersonalStorage.Store.IsPasswordValid() method. The PersonalStorage.Store.IsPasswordProtected property returns true if the PST file is password protected and false if it is not. The PersonalStorage.Store.IsPasswordValid() method which takes the string password as a parameter and returns true if the password is correct and false it is incorrect.

The following code snippet demonstrates the use of PersonalStorage.Store.IsPasswordProtected property and PersonalStorage.Store.IsPasswordValid() method.

Sample Code

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
PersonalStorage pst = PersonalStorage.fromFile(dataDir + "passwordprotectedPST.pst");
System.out.println("The storage is password protected - " + pst.getStore().isPasswordProtected());
System.out.println("Password is valid - " + pst.getStore().isPasswordValid("Password1"));
pst.dispose();

Console Output

The storage is password protected - True Password is valid - True