Home All Groups Group Topic Archive Search About

How to encrypt a string with ProtectedData (.NET 2.0)

Author
27 Mar 2006 1:01 PM
Zemp Dominik
Hi

How can I encrypt a string with the new ProtectedData class in the .NET
Framework 2.0? I have an example, but I receive always a 㣊 as output?!?

Here's my code:

        Dim plainBytes As Byte()
        Dim encryptedBytes As Byte()
        Dim cipherText As String

        ' Convert the string value.
        plainBytes = System.Text.Encoding.Unicode.GetBytes("Me.TextBox1.Text")

        ' Encrypt the string value (plain text) with the DAPI.
        encryptedBytes = ProtectedData.Protect(plainBytes, Nothing,
DataProtectionScope.CurrentUser)

        ' Convert the byte array and return it.
        cipherText = System.Text.Encoding.Unicode.GetString(encryptedBytes)
        Me.TextBox2.Text = cipherText


Wha'ts wrong in my this code? Can someone help me?
Thanks and Regards
Dominik

Author
27 Mar 2006 1:14 PM
Dominick Baier [DevelopMentor]
you need a loss-less conversion from encryptedBytes to cipherText - use:

cipherText = Convert.ToBase64String(encryptedBytes)';

---------------------------------------
Dominick Baier - DevelopMentor
http://www.leastprivilege.com

Show quoteHide quote
> Hi
>
> How can I encrypt a string with the new ProtectedData class in the
> .NET Framework 2.0? I have an example, but I receive always a 㣊 as
> output?!?
>
> Here's my code:
>
> Dim plainBytes As Byte()
> Dim encryptedBytes As Byte()
> Dim cipherText As String
> ' Convert the string value.
> plainBytes =
> System.Text.Encoding.Unicode.GetBytes("Me.TextBox1.Text")
> ' Encrypt the string value (plain text) with the DAPI.
> encryptedBytes = ProtectedData.Protect(plainBytes, Nothing,
> DataProtectionScope.CurrentUser)
> ' Convert the byte array and return it.
> cipherText =
> System.Text.Encoding.Unicode.GetString(encryptedBytes)
> Me.TextBox2.Text = cipherText
> Wha'ts wrong in my this code? Can someone help me?
> Thanks and Regards
> Dominik
Author
27 Mar 2006 9:45 PM
Zemp Dominik
Hi

Ok, thank you. The encryption process is working now. But... I've now a
little problem with the decryption. :)

        Dim cipherBytes As Byte()
        Dim decryptedBytes As Byte()

        ' Convert the string value.
        cipherBytes = Convert.FromBase64String(Me.TextBox1.Text)

        ' Decrypt the string value (cipher text) with the DAPI.
        decryptedBytes = ProtectedData.Unprotect(cipherBytes, Nothing,
DataProtectionScope.CurrentUser)

        ' Convert the byte array and return it.
        Me.TextBox2.Text =
System.Text.Encoding.Unicode.GetString(decryptedBytes)

Regards
Dominik

Show quoteHide quote
"Dominick Baier [DevelopMentor]" wrote:

> you need a loss-less conversion from encryptedBytes to cipherText - use:
>
> cipherText = Convert.ToBase64String(encryptedBytes)';
>
> ---------------------------------------
> Dominick Baier - DevelopMentor
> http://www.leastprivilege.com
>
> > Hi
> >
> > How can I encrypt a string with the new ProtectedData class in the
> > .NET Framework 2.0? I have an example, but I receive always a 㣊 as
> > output?!?
> >
> > Here's my code:
> >
> > Dim plainBytes As Byte()
> > Dim encryptedBytes As Byte()
> > Dim cipherText As String
> > ' Convert the string value.
> > plainBytes =
> > System.Text.Encoding.Unicode.GetBytes("Me.TextBox1.Text")
> > ' Encrypt the string value (plain text) with the DAPI.
> > encryptedBytes = ProtectedData.Protect(plainBytes, Nothing,
> > DataProtectionScope.CurrentUser)
> > ' Convert the byte array and return it.
> > cipherText =
> > System.Text.Encoding.Unicode.GetString(encryptedBytes)
> > Me.TextBox2.Text = cipherText
> > Wha'ts wrong in my this code? Can someone help me?
> > Thanks and Regards
> > Dominik
>
>
>
Author
27 Mar 2006 9:50 PM
Zemp Dominik
Uppsss... sorry! My mistake! It works but I've used the wrong textbox
control! ;)

Dominik

Show quoteHide quote
"Zemp Dominik" wrote:

> Hi
>
> Ok, thank you. The encryption process is working now. But... I've now a
> little problem with the decryption. :)
>
>         Dim cipherBytes As Byte()
>         Dim decryptedBytes As Byte()
>
>         ' Convert the string value.
>         cipherBytes = Convert.FromBase64String(Me.TextBox1.Text)
>
>         ' Decrypt the string value (cipher text) with the DAPI.
>         decryptedBytes = ProtectedData.Unprotect(cipherBytes, Nothing,
> DataProtectionScope.CurrentUser)
>
>         ' Convert the byte array and return it.
>         Me.TextBox2.Text =
> System.Text.Encoding.Unicode.GetString(decryptedBytes)
>
> Regards
> Dominik
>
> "Dominick Baier [DevelopMentor]" wrote:
>
> > you need a loss-less conversion from encryptedBytes to cipherText - use:
> >
> > cipherText = Convert.ToBase64String(encryptedBytes)';
> >
> > ---------------------------------------
> > Dominick Baier - DevelopMentor
> > http://www.leastprivilege.com
> >
> > > Hi
> > >
> > > How can I encrypt a string with the new ProtectedData class in the
> > > .NET Framework 2.0? I have an example, but I receive always a 㣊 as
> > > output?!?
> > >
> > > Here's my code:
> > >
> > > Dim plainBytes As Byte()
> > > Dim encryptedBytes As Byte()
> > > Dim cipherText As String
> > > ' Convert the string value.
> > > plainBytes =
> > > System.Text.Encoding.Unicode.GetBytes("Me.TextBox1.Text")
> > > ' Encrypt the string value (plain text) with the DAPI.
> > > encryptedBytes = ProtectedData.Protect(plainBytes, Nothing,
> > > DataProtectionScope.CurrentUser)
> > > ' Convert the byte array and return it.
> > > cipherText =
> > > System.Text.Encoding.Unicode.GetString(encryptedBytes)
> > > Me.TextBox2.Text = cipherText
> > > Wha'ts wrong in my this code? Can someone help me?
> > > Thanks and Regards
> > > Dominik
> >
> >
> >
Author
27 Mar 2006 1:24 PM
Mitch Gallant
Probably because you are trying to display nondisplayable unicode characters
in the TextBox.

Check size of your encryptedBytes array and use something like:

public static void PrintValues( Byte[] myArr )
    {
          foreach ( Byte i in myArr )
              {
                 Console.Write( "\t{0}", i );
            }
      Console.WriteLine();
    }

(MSDN sample :  ProtectedData.Protect)
- Mitch

Show quoteHide quote
"Zemp Dominik" <zemp.dominik@nospam.ecreation.ch> wrote in message
news:7477874A-64A9-4C05-B536-0ACDC0863F9D@microsoft.com...
> Hi
>
> How can I encrypt a string with the new ProtectedData class in the .NET
> Framework 2.0? I have an example, but I receive always a ? as output?!?
>
> Here's my code:
>
>        Dim plainBytes As Byte()
>        Dim encryptedBytes As Byte()
>        Dim cipherText As String
>
>        ' Convert the string value.
>        plainBytes = System.Text.Encoding.Unicode.GetBytes("Me.TextBox1.Text")
>
>        ' Encrypt the string value (plain text) with the DAPI.
>        encryptedBytes = ProtectedData.Protect(plainBytes, Nothing,
> DataProtectionScope.CurrentUser)
>
>        ' Convert the byte array and return it.
>        cipherText = System.Text.Encoding.Unicode.GetString(encryptedBytes)
>        Me.TextBox2.Text = cipherText
>
>
> Wha'ts wrong in my this code? Can someone help me?
> Thanks and Regards
> Dominik