Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8217925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:36:46+00:00 2026-06-07T12:36:46+00:00

WPF’s PasswordBox returns a SecureString, which hides the password from snoopers. The problem is

  • 0

WPF’s PasswordBox returns a SecureString, which hides the password from snoopers.

The problem is that you eventually have to get the value of the password, and the suggestions I’ve found on the net all involve copying the value into a string, which gets you back to the problem of snoopers.

IntPtr bstr = Marshal.SecureStringToBSTR(secureString);
string password = Marshal.PtrToStringBSTR(bstr);
Marshal.FreeBSTR(bstr);

But if you really think about it, you don’t really need the value, as a string. I mean, what do you do with a password? You hash it and then compare the result to a saved hash, and see if they are the same.

In other words, you don’t need to convert the SecureString into a string, you just need to be able to iterate over the individual characters in the string.

But how?

How do I loop over the individual characters in a BSTR, in C#, without converting it to a managed string?

EDITING: the solution, in case the link disappears:

The Marshall class provides methods that can extract individual bytes or ints from an IntPtr, at given offsets. A BSTR object contains an array of 16-bit characters, terminated by two null bytes. So you can access them by looping:

byte b = 1;
int i = 0;
while ((char)b != '\0')
{
    b = Marshal.ReadByte(bstr, i);
    // ...
    i += 2;
}

(I don’t care for that flow control. I’d have used a do…while, rather than prepopulate b with a dummy value, or I’d have used a for(;;) loop, with internal breaks, or I’d have looped on the length, which I explain how to get, below.)

Also, I’d probably use:

short b = Marshal.ReadInt16(bstr, i);

Reading the entire unicode chars, instead of just the low bytes of each.

You can get the length of the BSTR with:

int len = Marshal.ReadInt32(bstr, -4);

This is the number of bytes, not including the nulls, not the number of chars.

Also – use:

Marshal.ZeroFreeBSTR(bstr);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:36:47+00:00Added an answer on June 7, 2026 at 12:36 pm

    http://weblogs.asp.net/pglavich/archive/2005/08/15/422525.aspx

    This link shows how to use Marshal.SecureStringToBSTR(secretString) and assign it to a pointer and update the pointer to loop over the characters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

WPF has PasswordBox which receives password input into a SecureString, but I have a
WPF, Excel AddIn, C#, I have multiple asychronous calls to get data from web
WPF controls have certain properties (UserControl.Resources, UserControl.CommandBindings) that can have items added to them
WPF doesn't provide the ability to have a window that allows resize but doesn't
WPF 3.5 I have a ListView for which the XAML looks like so <ListView
In WPF we have Window.ShowDialog() which allows showing a modal dialog box. In WinForms
In WPF, I have a property with only a get{}. The value is coming
WPF can be infuriating sometimes. I have a fairly simple application that consists of
WPF launches certain method which calls external exe and waits , and then accesses
WPF is great in that I should be able to use {StaticResource MyBackground} instead

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.