Click here to Skip to main content
16,017,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys please help or Advise, i want to call a javascript from web service.what im doing is i do encrypt a string in javascript calling the web service, so now i want to decrypt the information in a web service
Posted
Updated 29-Sep-11 22:00pm
v2
Comments
Muralikrishna8811 30-Sep-11 4:06am    
Hi,
you want invoke javascript function after completeing process in webservice
muchabiseki 30-Sep-11 4:10am    
no, before the web service complete its process i want to use a function in javascrip to decrypt the encrypted string
muchabiseki 30-Sep-11 4:15am    
i do have this function in javascript and is working fine, but my main problem is when i try and convert it to vb.net is not working correctly


function decrypt(str, pwd) {
alert("i decrypt")
if (str == null || str.length < 8) {
alert("A salt value could not be extracted from the encrypted message because it's length is too short. The message cannot be decrypted.");
return;
}
if (pwd == null || pwd.length <= 0) {
alert("Please enter a password with which to decrypt the message.");
return;
}
var prand = "";
for (var i = 0; i < pwd.length; i++) {
prand += pwd.charCodeAt(i).toString();
}
var sPos = Math.floor(prand.length / 5);
//alert("Prand" + prand);
var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) + prand.charAt(sPos * 4) + prand.charAt(sPos * 5));
//alert(mult); //18168
//alert(pwd);

// alert(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) + prand.charAt(sPos * 4) + prand.charAt(sPos * 5));
//alert(prand.charAt(sPos));
//alert(prand.charAt(sPos*2));
//alert(prand.charAt(sPos*3));

var incr = Math.round(pwd.length / 2);
var modu = Math.pow(2, 31) - 1;
var tp = str.substring(str.length - 8, str.length);
var salt = parseInt(tp, 16);

str = str.substring(0, str.length - 8);
prand += salt;

while (prand.length > 10) {
prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
}
alert(prand);

prand = (mult * prand + incr) % modu;
var enc_chr = "";
var enc_str = "";

for (var i = 0; i < str.length; i += 2) {
enc_chr = parseInt(parseInt(str.substring(i, i + 2), 16) ^ Math.floor((prand / modu) * 255));
enc_str += String.fromCharCode(enc_chr);
prand = (mult * prand + incr) % modu;
}
return enc_str;
}
muchabiseki 30-Sep-11 4:16am    
so that is why im asking if there is a way to call it from web service
Muralikrishna8811 30-Sep-11 4:40am    
you want excute this funtion in webservice right?

1 solution

The web service runs on the server and you don't have Javascript there. (Well, technically you could run up the scripting engine and call out to it, but don't do that.)

If it's for an algorithmic purpose, as it seems to be, you will need to translate the function to the service language (C# probably).

Alternatively, if it's just to protect communication, you can call to your web service over HTTPS and ditch the custom encoding completely. Remember, if you're shipping the code to the client as JS, they can reverse engineer it and then apply the same decryption technique to intercepted data, unless the key is not known to them.
 
Share this answer
 
Comments
muchabiseki 30-Sep-11 5:32am    
thanks for your reply. ive tried to convert the code to vb.net cos dats what im using.but im having dificulties in doing that especially with the java functions like (ParseInt, substring the way it is formated).parseint from vb.net is completely different from the java


please help
BobJanova 30-Sep-11 8:20am    
parseInt -> int.Parse
string.substring(x,y) -> string.Substring(x, y-x)
string.charAt(x) -> string[x] (or is it string(x) in VB)
string.fromCharCode(i) -> (char)i
muchabiseki 30-Sep-11 8:57am    
thanks again, that is what i did but it still give me problem.
here it is

Public Function decrypt1(ByVal str, ByVal pwd) As String
Try

If (str Is Nothing Or str.length < 8) Then
Return Nothing
End If
If (pwd Is Nothing Or pwd.length <= 0) Then
Return Nothing
End If
Dim prand As String = ""
For i As Integer = 0 To pwd.length - 1
'prand += pwd.chars(i).ToString()
prand += Asc(pwd.chars(i).ToString).ToString
Next

'Dim sPos As VariantType = Math.Floor(prand.length / 5)
'Dim mult As VariantType = ParseInt(prand.Chars(sPos) + prand.Chars(sPos * 2) + prand.Chars(sPos * 3) + prand.Chars(sPos * 4) + prand.Chars(sPos * 5))
'Dim incr As VariantType = Math.Round(pwd.length / 2)
'Dim modu As VariantType = Math.Pow(2, 31) - 1
'Dim salt As VariantType = ParseInt(str.substring(str.length - 8, str.length), 16)

Dim sPos As Integer = Math.Floor(prand.Length / 5)
Dim mult1 As String = ""
Try
mult1 = CInt(prand.Chars(sPos).ToString).ToString & CInt(prand.Chars(sPos * 2).ToString).ToString & _
CInt(prand.Chars(sPos * 3).ToString).ToString & CInt(prand.Chars(sPos * 4).ToString).ToString & CInt(prand.Chars(sPos * 5).ToString)
Catch ex As Exception
mult1 = CInt(prand.Chars(sPos).ToString).ToString & CInt(prand.Chars(sPos * 2).ToString).ToString & _
CInt(prand.Chars(sPos * 3).ToString).ToString & CInt(prand.Chars(sPos * 4).ToString).ToString
End Try


Dim mult As Integer = CInt(mult1)

'Dim mult As VariantType = Int32.TryParse(prand.Chars(sPos) + prand.Chars(sPos * 2) + prand.Chars(sPos * 3) + prand.Chars(sPos * 4) + prand.Chars(sPos * 5), 16)
Dim incr As Double = Math.Round(pwd.length / 2)
Dim modu As Double = Math.Pow(2, 31) - 1
' Dim salt As VariantType = Val(str.substring(str.length - 8, str.length))
'str = str.substring(0, str.length - 8)
'Dim salt As VariantType = Val(str.Substring(str.length - 8, str.length - (str.length - 8)))
Dim salt As Long '= Int32.TryParse(str.Substring(str.length - 8, str.length - (str.length - 8)), 16)
salt = ReturnBinHexToInteger(str.Substring(str.length - 8, 8))
str = str.Substring(0, str.length - 8)
prand += salt.ToString

' while (prand.length > 10) {
' prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
'}
'prand = (mult * prand + incr) % modu;
''
'prand = prand.Substring(0, 10)

Dim tempprand As String = ""
While (prand.Length > 10)
Try
If prand = tempprand Then 'endless loop handle
prand = FormatNumber(prand, 0, Microsoft.VisualBasic.TriState.False, Microsoft.VisualBasic.TriState.False, Microsoft.VisualBasic.TriState.False)
Exit While
End If

Catch ex As Exception
End Try
'prand = (CDbl(prand.Substring(0, 10)) + FormatNumber(prand.Substring(10, prand.Length - 10), -1, Microsoft.VisualBasic.TriState.False, Microsoft.VisualBasic.TriState.False, Microsoft.VisualBasic.TriState.False)).ToString
tempprand = prand
prand = FormatNumber(CDbl(prand.Substring(0, 10)) + CDbl(prand.Substring(10, prand.Length - 10)), -1, Microsoft.VisualBasic.TriState.False, Microsoft.VisualBasic.TriState.False, Microsoft.VisualBasic.TriState.False)


'prand = (prand.Substring(0, 10)) + (prand.Substring(10, prand.Length - 10))
End While
prand = (mult * p

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900