Outlook refuses password from CryptProtectData() : <p>I'm developing a tool to import Outlook profiles using a PRF file but it does not import the password, so I have to manually add it to the registry.</p>

<p>I've spent lots of hours reading, testing and debugging this procedure and managed to figure out how everything works, except that Outlook refuses the password generated by CryptProtectData(). I've tried using both the C# .NET wrapper ProtectedData.Protect() and a C++ DLL calling CryptProtectData(), all to no avail.</p>

<p>Whenever I change the password on the registry and open Outlook, it shows the credential dialog. If I type the password then it successfully logs into my email.</p>

<p>Here is the C# code using the .NET wrapper:</p>

<pre><code>RegistryKey RegKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion\\Windows Messaging Subsystem\\Profiles\\MyProfile\\9375CFF0413111d3B88A00104B2A6676\\0000000b", true);
Byte[] Password = Encoding.UTF8.GetBytes("MyPassword");
Byte[] EncPassword = ProtectedData.Protect(Password, null, DataProtectionScope.CurrentUser);
RegKey.SetValue("IMAP Password", EncPassword);
</code></pre>

<p>This code generates a binary data with 234 bytes, 39 bytes less than the password generated by Outlook (273 bytes).</p>

<p>Here is the C++ code:</p>

<pre><code>extern "C" DLLEXPORT DATA_BLOB crypt(BYTE *input) {
    DATA_BLOB DataIn;
    DATA_BLOB DataOut;
    BYTE *pbDataInput = input;
    DWORD cbDataInput = strlen((char *)pbDataInput)+1;
    DataIn.pbData = pbDataInput;    
    DataIn.cbData = cbDataInput;

    if( !CryptProtectData(&amp;DataIn, L"IMAP Password", NULL, NULL, NULL, 0, &amp;DataOut) )
    {
        printf("Encryption error");
    }

    return DataOut;
}
</code></pre>

<p>The C# code calling the DLL:</p>

<pre><code>[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct DATA_BLOB
{
    public int cbData;
    public IntPtr pbData;
}

[DllImport("MyLib.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern DATA_BLOB crypt(Byte[] input);

(...)

RegistryKey RegKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion\\Windows Messaging Subsystem\\Profiles\\MyProfile\\9375CFF0413111d3B88A00104B2A6676\\0000000b", true);
Byte[] Password = Encoding.UTF8.GetBytes("MyPassword");
DATA_BLOB BlobData = crypt(Encoding.UTF8.GetBytes("MyPassword"));
Byte[] EncPassword = new Byte[BlobData.cbData];
Marshal.Copy(BlobData.pbData, EncPassword, 0, BlobData.cbData);

RegKey.SetValue("IMAP Password", EncPassword);
</code></pre>

<p>This code generates a password with 256 bytes, still not the 273 bytes I get from Outlook.</p>

<p>My guess is that these missing bytes are from a specific entropy I'm not using or even some detail I'm missing.</p>

<p>Any tip on the right direction will help a lot.</p>

<p>Thanks!</p>
