473,545 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I want to clear "immutable" string contents

Hi All!

I want to clear the string contents from sensitive information
such as passwords, and etc.

It's always a case that password will appear as string at some point
or another. And i feel uneasy leaving it hanging in memory indefinitely
(especially in case when string is Interned).

So at leats for the case when string is not interned i propose:

string pass = Console.ReadLin e();
if (string.IsInter ned(pass) == null)
{
unsafe
{
fixed(void* pv = pass)
{
char* pb = (char*)pv;
for(int i =0; i<pass.Length; ++i)
pb[i] = '0';
}
}
}
Console.WriteLi ne(pass);

Note: explicit RuntimeHelpers. OffsetToStringD ata is not needed.

What do you all think about this?
Jul 19 '05 #1
6 2814
Hi,

Since you know that strings are immutable, you can't clear or modify them in
any way (in theory).

Why not use a char array instead to store your password chars? It is at your
own disposal to create the array and destroy it. A few chars won't take up
too much memory.

Edward

"cppdev" <cp*****@yahoo. com> wrote in message
news:fc******** *************** **@posting.goog le.com...
Hi All!

I want to clear the string contents from sensitive information
such as passwords, and etc.

It's always a case that password will appear as string at some point
or another. And i feel uneasy leaving it hanging in memory indefinitely
(especially in case when string is Interned).

So at leats for the case when string is not interned i propose:

string pass = Console.ReadLin e();
if (string.IsInter ned(pass) == null)
{
unsafe
{
fixed(void* pv = pass)
{
char* pb = (char*)pv;
for(int i =0; i<pass.Length; ++i)
pb[i] = '0';
}
}
}
Console.WriteLi ne(pass);

Note: explicit RuntimeHelpers. OffsetToStringD ata is not needed.

What do you all think about this?

Jul 19 '05 #2
Hi,

I would love to use byte[] or char[],
but it's not my choice. I'm using TextControl
to get information from the user in winform.
And it only has Text property.

"Edward Yang" <neo_in_matrix@ > wrote in message news:<OU******* *******@TK2MSFT NGP09.phx.gbl>. ..
Hi,

Since you know that strings are immutable, you can't clear or modify them in
any way (in theory).

Why not use a char array instead to store your password chars? It is at your
own disposal to create the array and destroy it. A few chars won't take up
too much memory.

Edward

"cppdev" <cp*****@yahoo. com> wrote in message
news:fc******** *************** **@posting.goog le.com...
Hi All!

I want to clear the string contents from sensitive information
such as passwords, and etc.

It's always a case that password will appear as string at some point
or another. And i feel uneasy leaving it hanging in memory indefinitely
(especially in case when string is Interned).

So at leats for the case when string is not interned i propose:

string pass = Console.ReadLin e();
if (string.IsInter ned(pass) == null)
{
unsafe
{
fixed(void* pv = pass)
{
char* pb = (char*)pv;
for(int i =0; i<pass.Length; ++i)
pb[i] = '0';
}
}
}
Console.WriteLi ne(pass);

Note: explicit RuntimeHelpers. OffsetToStringD ata is not needed.

What do you all think about this?

Jul 19 '05 #3
Yes i can use GetWindowText myself, but i also use
PasswordDeriveB ytes to derive keys for encryption
from user password and that only takes a string.

"JD" <No@Where.com > wrote in message news:<#i******* *******@TK2MSFT NGP09.phx.gbl>. ..
Could you create a password control that stores the text into a byte[]
instead of a string so that the pass never gets interned?

- J

"cppdev" <cp*****@yahoo. com> wrote in message
news:fc******** *************** ***@posting.goo gle.com...
Hi,

I would love to use byte[] or char[],
but it's not my choice. I'm using TextControl
to get information from the user in winform.
And it only has Text property.

"Edward Yang" <neo_in_matrix@ > wrote in message

news:<OU******* *******@TK2MSFT NGP09.phx.gbl>. ..
Hi,

Since you know that strings are immutable, you can't clear or modify them in any way (in theory).

Why not use a char array instead to store your password chars? It is at your own disposal to create the array and destroy it. A few chars won't take up too much memory.

Edward

"cppdev" <cp*****@yahoo. com> wrote in message
news:fc******** *************** **@posting.goog le.com...
> Hi All!
>
> I want to clear the string contents from sensitive information
> such as passwords, and etc.
>
> It's always a case that password will appear as string at some point
> or another. And i feel uneasy leaving it hanging in memory indefinitely > (especially in case when string is Interned).
>
> So at leats for the case when string is not interned i propose:
>
> string pass = Console.ReadLin e();
> if (string.IsInter ned(pass) == null)
> {
> unsafe
> {
> fixed(void* pv = pass)
> {
> char* pb = (char*)pv;
> for(int i =0; i<pass.Length; ++i)
> pb[i] = '0';
> }
> }
> }
> Console.WriteLi ne(pass);
>
> Note: explicit RuntimeHelpers. OffsetToStringD ata is not needed.
>
> What do you all think about this?

Jul 19 '05 #4
If a common string is used over and over again, .NET
may "intern" it or make a single instance of it and
whenever you try to create a new instance of it, it'll
just return you the reference to the main, interned one.

I believe this happens during JIT. It recognizes common
strings and just makes one copy of them.

-c

"News VS.NET ( MS ILM )" <sq**********@h otmail.com> wrote in message
news:uL******** *****@TK2MSFTNG P10.phx.gbl...
Excuse my now knowing
What does interned mean here.??

"JD" <No@Where.com > wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Could you create a password control that stores the text into a byte[] instead of a string so that the pass never gets interned?

- J

"cppdev" <cp*****@yahoo. com> wrote in message
news:fc******** *************** ***@posting.goo gle.com...
Hi,

I would love to use byte[] or char[],
but it's not my choice. I'm using TextControl
to get information from the user in winform.
And it only has Text property.

"Edward Yang" <neo_in_matrix@ > wrote in message news:<OU******* *******@TK2MSFT NGP09.phx.gbl>. ..
> Hi,
>
> Since you know that strings are immutable, you can't clear or modify
them in
> any way (in theory).
>
> Why not use a char array instead to store your password chars?

It is at
your
> own disposal to create the array and destroy it. A few chars
won't take
up
> too much memory.
>
> Edward
>
> "cppdev" <cp*****@yahoo. com> wrote in message
> news:fc******** *************** **@posting.goog le.com...
> > Hi All!
> >
> > I want to clear the string contents from sensitive information
> > such as passwords, and etc.
> >
> > It's always a case that password will appear as string at some

point > > or another. And i feel uneasy leaving it hanging in memory

indefinitely
> > (especially in case when string is Interned).
> >
> > So at leats for the case when string is not interned i propose: > >
> > string pass = Console.ReadLin e();
> > if (string.IsInter ned(pass) == null)
> > {
> > unsafe
> > {
> > fixed(void* pv = pass)
> > {
> > char* pb = (char*)pv;
> > for(int i =0; i<pass.Length; ++i)
> > pb[i] = '0';
> > }
> > }
> > }
> > Console.WriteLi ne(pass);
> >
> > Note: explicit RuntimeHelpers. OffsetToStringD ata is not needed. > >
> > What do you all think about this?



Jul 19 '05 #5
Chad

Thank you.
"Chad Myers" <cm****@N0.SP.A M.austin.rr.com > wrote in message
news:uO******** ******@TK2MSFTN GP09.phx.gbl...
If a common string is used over and over again, .NET
may "intern" it or make a single instance of it and
whenever you try to create a new instance of it, it'll
just return you the reference to the main, interned one.

I believe this happens during JIT. It recognizes common
strings and just makes one copy of them.

-c

"News VS.NET ( MS ILM )" <sq**********@h otmail.com> wrote in message
news:uL******** *****@TK2MSFTNG P10.phx.gbl...
Excuse my now knowing
What does interned mean here.??

"JD" <No@Where.com > wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Could you create a password control that stores the text into a byte[] instead of a string so that the pass never gets interned?

- J

"cppdev" <cp*****@yahoo. com> wrote in message
news:fc******** *************** ***@posting.goo gle.com...
> Hi,
>
> I would love to use byte[] or char[],
> but it's not my choice. I'm using TextControl
> to get information from the user in winform.
> And it only has Text property.
>
> "Edward Yang" <neo_in_matrix@ > wrote in message
news:<OU******* *******@TK2MSFT NGP09.phx.gbl>. ..
> > Hi,
> >
> > Since you know that strings are immutable, you can't clear or modify them in
> > any way (in theory).
> >
> > Why not use a char array instead to store your password chars? It is
at
your
> > own disposal to create the array and destroy it. A few chars

won't
take
up
> > too much memory.
> >
> > Edward
> >
> > "cppdev" <cp*****@yahoo. com> wrote in message
> > news:fc******** *************** **@posting.goog le.com...
> > > Hi All!
> > >
> > > I want to clear the string contents from sensitive information
> > > such as passwords, and etc.
> > >
> > > It's always a case that password will appear as string at some

point > > > or another. And i feel uneasy leaving it hanging in memory
indefinitely
> > > (especially in case when string is Interned).
> > >
> > > So at leats for the case when string is not interned i propose: > > >
> > > string pass = Console.ReadLin e();
> > > if (string.IsInter ned(pass) == null)
> > > {
> > > unsafe
> > > {
> > > fixed(void* pv = pass)
> > > {
> > > char* pb = (char*)pv;
> > > for(int i =0; i<pass.Length; ++i)
> > > pb[i] = '0';
> > > }
> > > }
> > > }
> > > Console.WriteLi ne(pass);
> > >
> > > Note: explicit RuntimeHelpers. OffsetToStringD ata is not needed. > > >
> > > What do you all think about this?



Jul 19 '05 #6
Chad Myers <cm****@N0.SP.A M.austin.rr.com > wrote:
If a common string is used over and over again, .NET
may "intern" it or make a single instance of it and
whenever you try to create a new instance of it, it'll
just return you the reference to the main, interned one.

I believe this happens during JIT. It recognizes common
strings and just makes one copy of them.


Fortunately it's not nearly as heuristic as that. All string
literals/constants are interned, and any string which you call Intern
on is interned. I don't believe anything else will get interned. You
also don't get the interned copy whenever you create a string with
identical contents - it's only if you're using the same string literal
or if you specifically ask for the interned version.

If the JIT started interning other strings, you'd end up with a
possible memory leak.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2732
by: Tino Lange | last post by:
Hi! I identified a bottleneck in my programs. I just want to "encrypt" data by easy xoring. Ok - that's no encryption at all - I know. But it's hardly readable - and that's enough :-) Just some quick obscurity. It turns out not to be quick at all. I really didn't expect this to be a bottleneck, but it takes quite some time.
10
11097
by: cppdev | last post by:
Hi All! I want to clear the string contents from sensitive information such as passwords, and etc. It's always a case that password will appear as string at some point or another. And i feel uneasy leaving it hanging in memory indefinitely (especially in case when string is Interned). So at leats for the case when string is not...
6
29609
by: Marty | last post by:
Hi, I would like to replace "\r\n" by "_" within a specific string. I tried : strMyString.Replace('\r', '_'); strMyString.Replace('\n', '_'); or strMyString.Replace(System.Environment.NewLine, '_');
26
3166
by: Hardy Wang | last post by:
Hi all, I know it is better to handle large string with a StringBuilder, but how does StringBuilder class improve the performance in the background? Thanks! -- WWW: http://hardywang.1accesshost.com ICQ: 3359839 yours Hardy
10
18613
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index parameter with which you can write wanted character at that position. But no, Replace method only allows replacing one known character with another. The...
5
3262
by: cameljs18 | last post by:
Converting a string variable into a string literal. How do I add the @ character in front of the string? I cannot add it when the string is created as it will affect other parts of the program. I have tried these but they do not work: label1.text = @S label1.text = "@" + S
0
7668
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5984
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4960
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.