Working with PST Password Protection Properties
Check for Password Protection
The CRC-32 hash of the password string is stored in the PidTagPstPassword property (tag = 0x67ff0003, exposed as MapiPropertyTag.PR_PST_PASSWORD) of the MessageStore. If that property exists and is non-zero, the PST is password protected.
You do not have to read the property yourself: the MessageStore exposes both checks directly, as shown in Password Verification for Password Protected PST Files below. The raw property is only needed to reset it, which the next section covers.
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
String dataDir = "data/";
PersonalStorage pst = PersonalStorage.fromFile(dataDir + "PersonalStorage.pst");
try {
// Note the accessor: the collection is keyed by tag, so use containsKey, not contains
if (pst.getStore().getProperties().containsKey(MapiPropertyTag.PR_PST_PASSWORD)) {
// Writing zero is what removes the property
byte[] zero = ByteBuffer.allocate(4).order(ByteOrder.nativeOrder()).putInt(0).array();
pst.getStore().setProperty(new MapiProperty(MapiPropertyTag.PR_PST_PASSWORD, zero));
}
} finally {
pst.dispose();
}
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
String dataDir = "data/";
PersonalStorage pst = PersonalStorage.create(dataDir + "PersonalStorage_out.pst", FileFormatVersion.Unicode);
try {
// Set the password
pst.getStore().changePassword("Password1");
// Remove the password
pst.getStore().changePassword(null);
} finally {
pst.dispose();
}
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
String dataDir = "data/";
PersonalStorage pst = PersonalStorage.fromFile(dataDir + "passwordprotectedPST.pst");
try {
System.out.println("The storage is password protected - " + pst.getStore().isPasswordProtected());
System.out.println("Password is valid - " + pst.getStore().isPasswordValid("Password1"));
} finally {
pst.dispose();
}
Console Output
The storage is password protected - True Password is valid - True