I decrypt and encrypt query string in the following way.Some cases i am getting error as "Invalid length for a Base-64 char array or string."
There is no any space in the querystring .When i tested i found that decrpt function is not taking the "=" sign .How can i solve this issue.
localhost/aa/aa.aspx?un=bwB4AHkA working fine
localhost/aa/aa.aspx?un=SwBHAEkAVAA= not working
The method i am using like as follows
Dim _decryted As Byte()
Dim s As String = HtmlPage.Document.QueryString("un").ToString()
Dim s1 As String
_decryted = Convert.FromBase64String(s)
s1 = System.Text.Encoding.Unicode.GetString(_decryted, 0, _decryted.ToArray().Length)
Dim _encryted As Byte()
Dim s As String
Dim s1 As String = user4mapping
_encryted = System.Text.Encoding.Unicode.GetBytes(s1)
s = Convert.ToBase64String(_encryted)
Response.Write("<script language='javascript'>window.open('localhost/aa/aa.aspx?un=" & s & "','');
Just so you're aware, Convert.ToBase64String is not encrypting or decrypting -- it provides
no protection. If you need protection for a query string value, check
this.
As for the problem, I'm guessing that you did not URL encode it before you put it into the query string. Base64 encoding uses the "=" as a padding character which seems like it'd conflict with the query string's parameter syntax.
Meenu - If you have a byte[] and you're trying to stick it in the query string, you could consider HttpServerUtility.UrlTokenEncode /
UrlTokenDecode to round-trip the data. These methods perform a variant of base64 encoding: they use URL-safe characters (- and _) instead of +, /, and =.
Can u help to do the changes in the following code?I tried and getting lot of errors
Private Sub LnkLastPosition_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles LnkLastPosition.Click
Const MachineKeyPurpose As String = "Striqw"
Const Anonymous As String = "<anonymous>"
End Sub
Private Function GetMachineKeyPurpose(user As IPrincipal) As String
Return [String].Format(MachineKeyPurpose, If(user.Identity.IsAuthenticated, user.Identity.Name, Anonymous))
End Function
Private Function Protect(data As Byte()) As String
If data Is Nothing OrElse data.Length = 0 Then
Return Nothing
End If
Dim purpose = GetMachineKeyPurpose(Thread.CurrentPrincipal)
Dim value = MachineKey.Protect(data, purpose)
Return Convert.ToBase64String(value)
End Function
Private Function Unprotect(value As String) As Byte()
If [String].IsNullOrWhiteSpace(value) Then
Return Nothing
End If
Dim purpose = GetMachineKeyPurpose(Thread.CurrentPrincipal)
Dim bytes = Convert.FromBase64String(value)
Return MachineKey.Unprotect(bytes, purpose)
End Function
I tried the following code from the day u suggested the post
this.
Today i found that this code was working fine with asp.net web application but not with silverlight web application.
I did cheking on my silverlight(4) web application project .Can u help me to do this same way coding in silverlight web application project????
Public Shared Function Encrypt(plaintextValue As String) As String
Dim plaintextBytes = Encoding.UTF8.GetBytes(plaintextValue)
Return MachineKey.Encode(plaintextBytes, MachineKeyProtection.All)
End Function
Public Shared Function Decrypt(encryptedValue As String) As String
Try
Dim decryptedBytes = MachineKey.Decode(encryptedValue, MachineKeyProtection.All)
Return Encoding.UTF8.GetString(decryptedBytes)
Catch
Return Nothing
End Try
End Function
Is this code in your server-side code or in your silver light client-side code? MachineKey is only meant to be used from your server side code (like ASP.NET or MVC).
meenu.monu
0 Points
7 Posts
decrypt is not taking "="
Jun 22, 2012 12:51 AM|LINK
I decrypt and encrypt query string in the following way.Some cases i am getting error as "Invalid length for a Base-64 char array or string."
There is no any space in the querystring .When i tested i found that decrpt function is not taking the "=" sign .How can i solve this issue.
localhost/aa/aa.aspx?un=bwB4AHkA working fine
localhost/aa/aa.aspx?un=SwBHAEkAVAA= not working
The method i am using like as follows
Dim _decryted As Byte() Dim s As String = HtmlPage.Document.QueryString("un").ToString() Dim s1 As String _decryted = Convert.FromBase64String(s) s1 = System.Text.Encoding.Unicode.GetString(_decryted, 0, _decryted.ToArray().Length) Dim _encryted As Byte() Dim s As String Dim s1 As String = user4mapping _encryted = System.Text.Encoding.Unicode.GetBytes(s1) s = Convert.ToBase64String(_encryted) Response.Write("<script language='javascript'>window.open('localhost/aa/aa.aspx?un=" & s & "','');Please Help me to solve my issue
BrockAllen
All-Star
27532 Points
4906 Posts
MVP
Re: decrypt is not taking "="
Jun 22, 2012 12:57 AM|LINK
Just so you're aware, Convert.ToBase64String is not encrypting or decrypting -- it provides no protection. If you need protection for a query string value, check this.
As for the problem, I'm guessing that you did not URL encode it before you put it into the query string. Base64 encoding uses the "=" as a padding character which seems like it'd conflict with the query string's parameter syntax.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
levib
Star
7702 Points
1099 Posts
Microsoft
Re: decrypt is not taking "="
Jun 22, 2012 04:37 PM|LINK
Brock - great article!
Meenu - If you have a byte[] and you're trying to stick it in the query string, you could consider HttpServerUtility.UrlTokenEncode / UrlTokenDecode to round-trip the data. These methods perform a variant of base64 encoding: they use URL-safe characters (- and _) instead of +, /, and =.
meenu.monu
0 Points
7 Posts
Re: decrypt is not taking "="
Jun 24, 2012 02:15 AM|LINK
But this code is not working in my silverlight application in VS2010
Syed Aoun Al...
Member
14 Points
8 Posts
Re: decrypt is not taking "="
Jun 26, 2012 09:16 PM|LINK
You should use ascii characters
meenu.monu
0 Points
7 Posts
Re: decrypt is not taking "="
Jun 28, 2012 06:35 AM|LINK
Below is the code what i tried in vb.net.
Can u help to do the changes in the following code?I tried and getting lot of errors
Private Sub LnkLastPosition_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles LnkLastPosition.Click Const MachineKeyPurpose As String = "Striqw" Const Anonymous As String = "<anonymous>" End Sub Private Function GetMachineKeyPurpose(user As IPrincipal) As String Return [String].Format(MachineKeyPurpose, If(user.Identity.IsAuthenticated, user.Identity.Name, Anonymous)) End Function Private Function Protect(data As Byte()) As String If data Is Nothing OrElse data.Length = 0 Then Return Nothing End If Dim purpose = GetMachineKeyPurpose(Thread.CurrentPrincipal) Dim value = MachineKey.Protect(data, purpose) Return Convert.ToBase64String(value) End Function Private Function Unprotect(value As String) As Byte() If [String].IsNullOrWhiteSpace(value) Then Return Nothing End If Dim purpose = GetMachineKeyPurpose(Thread.CurrentPrincipal) Dim bytes = Convert.FromBase64String(value) Return MachineKey.Unprotect(bytes, purpose) End FunctionBrockAllen
All-Star
27532 Points
4906 Posts
MVP
Re: decrypt is not taking "="
Jun 28, 2012 04:03 PM|LINK
What are your errors? Are you doing .NET 4.0 or 4.5?
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
meenu.monu
0 Points
7 Posts
Re: decrypt is not taking "="
Jun 30, 2012 01:01 AM|LINK
Using VS2010 .net 4 .
"Machine key " is giving the error .
meenu.monu
0 Points
7 Posts
Re: decrypt is not taking "="
Jul 02, 2012 02:35 AM|LINK
Dear Broke,
I tried the following code from the day u suggested the post this.
Today i found that this code was working fine with asp.net web application but not with silverlight web application.
I did cheking on my silverlight(4) web application project .Can u help me to do this same way coding in silverlight web application project????
Public Shared Function Encrypt(plaintextValue As String) As String Dim plaintextBytes = Encoding.UTF8.GetBytes(plaintextValue) Return MachineKey.Encode(plaintextBytes, MachineKeyProtection.All) End Function Public Shared Function Decrypt(encryptedValue As String) As String Try Dim decryptedBytes = MachineKey.Decode(encryptedValue, MachineKeyProtection.All) Return Encoding.UTF8.GetString(decryptedBytes) Catch Return Nothing End Try End FunctionBrockAllen
All-Star
27532 Points
4906 Posts
MVP
Re: decrypt is not taking "="
Jul 02, 2012 03:33 AM|LINK
Is this code in your server-side code or in your silver light client-side code? MachineKey is only meant to be used from your server side code (like ASP.NET or MVC).
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/