|
security
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to encrypt a string with ProtectedData (.NET 2.0)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 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 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 > > > 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 > > > > > > 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
ClickOnce and remembering permissions granted
Tightening the default CAS policy JavaScience CD versus book Least Privilege User Accounts GSSAPI bindings for C#/.NET bad encryption How do I deistinguis between a user and a group/role Rights to get Data for Crystal reports Assembly Trust Identifying group memberships for users authenticated with AD Trus |
|||||||||||||||||||||||