Working with PST Password Protection Properties
Working with PST Password Protection
Outlook can protect a PST file with a password: it asks for the password before it shows the content of the file. The password is kept as a property of the message store, while the messages themselves are not encrypted. Aspose.Email for C++ reaches it through PersonalStorage::get_Store, which returns the MessageStore of the file. This article shows how to:
- set or change the password of a PST file;
- find out whether a file is protected and check a password;
- read a protected file;
- remove the password.
The samples below use these headers and namespaces:
#include <Mapi/MapiProperty.h>
#include <Mapi/MapiPropertyCollection.h>
#include <Mapi/MapiPropertyTag.h>
#include <Storage/Pst/PersonalStorage.h>
#include <Storage/Pst/MessageStore.h>
#include <Storage/Pst/FolderInfo.h>
#include <Storage/Pst/FolderInfoCollection.h>
#include <Storage/Pst/MessageInfo.h>
#include <system/array.h>
#include <system/convert.h>
#include <system/io/file.h>
using namespace System;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::Storage::Pst;
Setting or Changing the Password
MessageStore::ChangePassword sets a new password, replacing the old one if there was any. The storage has to be opened for writing.
// a copy of the sample storage, protected with a password
IO::File::Copy(u"demo.pst", u"protected.pst", true);
auto pst = PersonalStorage::FromFile(u"protected.pst", true);
pst->get_Store()->ChangePassword(u"Password1");
pst->Dispose();
Console::WriteLine(u"protected.pst now has a password");
Detecting a Protected Storage
MessageStore::get_IsPasswordProtected tells whether Outlook will ask for a password when the file is opened. A read-only storage is enough for the check.
auto pst = PersonalStorage::FromFile(u"protected.pst", false);
Console::WriteLine(u"Password protected: " + Convert::ToString(pst->get_Store()->get_IsPasswordProtected()));
pst->Dispose();
Validating a Password
MessageStore::IsPasswordValid compares a candidate with the stored password and returns true only on an exact match - the comparison is case-sensitive. This is the way to authenticate a user before your application hands out the content of a protected file.
auto pst = PersonalStorage::FromFile(u"protected.pst", false);
for (String candidate : { String(u"password1"), String(u"Password1") })
{
Console::WriteLine(candidate + u" is valid: " + Convert::ToString(pst->get_Store()->IsPasswordValid(candidate)));
}
pst->Dispose();
Reading a Protected Storage
Because the content is not encrypted, a protected file is opened and read exactly like any other one - no password is passed anywhere. Whether to ask the user for the password first, with the check shown above, is a decision of the application.
// no password is needed to open the file
auto pst = PersonalStorage::FromFile(u"protected.pst", false);
for (auto folder : pst->get_RootFolder()->GetSubFolders())
{
for (auto messageInfo : folder->EnumerateMessages())
{
Console::WriteLine(folder->get_DisplayName() + u": " + messageInfo->get_Subject());
}
}
pst->Dispose();
Removing the Password
There are two ways to take the protection off. The shorter one is to pass nullptr to ChangePassword:
auto pst = PersonalStorage::FromFile(u"protected.pst", true);
// a new string changes the password, an empty value removes it
pst->get_Store()->ChangePassword(nullptr);
Console::WriteLine(u"Password protected: " + Convert::ToString(pst->get_Store()->get_IsPasswordProtected()));
pst->Dispose();
The other one works on the store property directly. PR_PST_PASSWORD cannot be deleted from a message store the way properties are deleted from a message, but writing zero into it switches the protection off. MessageStore::SetProperty stores the new value, and the Properties collection tells whether the file has a password property at all.
// prepare a protected copy to work on
IO::File::Copy(u"demo.pst", u"reset.pst", true);
auto protectedCopy = PersonalStorage::FromFile(u"reset.pst", true);
protectedCopy->get_Store()->ChangePassword(u"Password1");
protectedCopy->Dispose();
auto pst = PersonalStorage::FromFile(u"reset.pst", true);
auto store = pst->get_Store();
Console::WriteLine(u"Before: " + Convert::ToString(store->get_IsPasswordProtected()));
// the property cannot be deleted from the store, but a zero value switches the protection off
if (store->get_Properties()->ContainsKey(MapiPropertyTag::PR_PST_PASSWORD))
{
auto zero = System::MakeObject<MapiProperty>(MapiPropertyTag::PR_PST_PASSWORD,
System::MakeArray<uint8_t>({ 0, 0, 0, 0 }));
store->SetProperty(zero);
}
Console::WriteLine(u"After: " + Convert::ToString(store->get_IsPasswordProtected()));
pst->Dispose();