diff --git a/SIL.PasswordStore/Provider/WindowsProvider.cs b/SIL.PasswordStore/Provider/WindowsProvider.cs index 6897936..547e2a0 100644 --- a/SIL.PasswordStore/Provider/WindowsProvider.cs +++ b/SIL.PasswordStore/Provider/WindowsProvider.cs @@ -25,13 +25,13 @@ public void SetPassword(string service, string user, string password) var passwordLength = string.IsNullOrEmpty(password) ? 0 - : (uint)Encoding.UTF8.GetBytes(password).Length + 1; + : (uint)Encoding.Unicode.GetByteCount(password); var credential = new Credential { Flags = 0, Type = CredType.Generic, TargetName = GetTargetName(service, user), Comment = null, - CredentialBlobSize = passwordLength * 2, + CredentialBlobSize = passwordLength, CredentialBlob = Marshal.StringToCoTaskMemUni(password), Persist = CredPersist.LocalMachine, AttributeCount = 0 @@ -58,7 +58,7 @@ public void SetPassword(string service, string user, string password) var credential = credentialHandle.GetCredential(); return credential?.CredentialBlobSize == 0 ? null - : Marshal.PtrToStringUni(credential?.CredentialBlob ?? IntPtr.Zero); + : Marshal.PtrToStringUni(credential?.CredentialBlob ?? IntPtr.Zero, (int)(credential?.CredentialBlobSize ?? 0) / 2).TrimEnd('\0'); } var error = Marshal.GetLastWin32Error();