Link to home
Start Free TrialLog in
Avatar of cutie2000
cutie2000

asked on

Simple ASP function to Java

I managed to convert part of ASP to Java.
But I am sure they are slightly wrong.


---
ASP
---

Function encrypt(strText)
    Dim i , c
    Dim strBuff
    If strKey <> "" And strText <> "" Then
        strKey = UCase(strKey)
        If Len(strKey) Then
            For i = 1 To Len(strText)
                c = Asc(Mid(strText, i, 1))
                c = c + Asc(Mid(strKey, (i Mod Len(strKey)) + 1, 1))
                strBuff = strBuff & Chr(c And &HFF)
            Next
        Else
            strBuff = strText
        End If

        encrypt = Server.URLEncode(strBuff)
    Else
        encrypt = ""
    End If
End Function

Function decrypt(strText)
    Dim i , c
    Dim strBuff
    If strKey <> "" And strText <> "" Then
        strKey = UCase(strKey)
        If Len(strKey) Then
            For i = 1 To Len(strText)
                c = Asc(Mid(strText, i, 1))
                c = c - Asc(Mid(strKey, (i Mod Len(strKey)) + 1, 1))
                strBuff = strBuff & Chr(c And &HFF)
            Next
        Else
            strBuff = strText
        End If

        decrypt = strBuff
    Else
        decrypt = ""
    End If
End Function


----
Java
----

public static String encryptText(String strText, String strKey) {
      int i , c;
      String strBuff="";

      if ( strKey != "" && strText != "" ) {
            strKey = strKey.toUpperCase();

            if ( strKey.length() > 0 ) {
                  for ( i=0; i<strText.length(); i++ ) {
                        c = (int)strText.charAt(i);
                        System.out.println(i % strKey.length() + 1);
                        c = c + (int)strKey.charAt(i % strKey.length());
                        strBuff = strBuff + (char)(c & 0xFF);    // <-- I am sure is wrong
                  }
            }
            else {
                  strBuff = strText;
            }

            return strBuff;
      }
      else {
            return "";
      }
}

public static String decryptText(String strText, String strKey)      {
      int i, c;
      String strBuff="";

      if ( strKey != "" && strText != "" ) {
            strKey = strKey.toUpperCase();

            if ( strKey.length() > 0 ) {
                  for ( i=0; i<strText.length(); i++ ) {
                        c = (int)strText.charAt(i);
                        c = c - (int)(strKey.charAt(i % strKey.length()));
                        strBuff = strBuff + (char)(c & 0xFF);    // <-- I am sure is wrong
                  }
            }
            else {
                  strBuff = strText;
            }

            return strBuff;
      }
      else {
            return "";
      }
}

---

Can anyone help me?
Avatar of Mayank S
Mayank S
Flag of India image

It will compile fine - are you facing any problems while running?
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cutie2000
cutie2000

ASKER

I am not getting any help here.
I will request for a delete.
Actually you are not helping us understand what the problem is - you have not answered any of our questions as to what errors you are getting with your existing code. Until you reply to our comments, it is not possible for us to assume what the error is. Compilation-wise, your code is ok.
I was just about to post the same thing.  

1.  Can you be more specific in your problem.
2.  What error messages are you getting.
3.  What line of code causes the error.
You are right that both ASP and java compile well but then they are not working as it is.
Some ids i pass in will result in funny characters being generated and are not suitable to use to pass to a querystring. So I feel above are not really going to work. Plus, the ASP and java do not seem to encrypt and give the same output.
What input do you pass to it, and what is the output that you are getting (give an example). And what output would you expect to see with that input instead?
No comments?
abandoned?
Give me till weekend and I will post.
Quite tighted with my work.
ASP Side
-----------

Code - EncDec.asp

<%
Dim strKey

strKey = "abcdef"

Function encrypt(strText)
    Dim i , c
    Dim strBuff
    If strKey <> "" And strText <> "" Then
        strKey = UCase(strKey)
        If Len(strKey) Then
            For i = 1 To Len(strText)
                c = Asc(Mid(strText, i, 1))
                c = c + Asc(Mid(strKey, (i Mod Len(strKey)) + 1, 1))
                strBuff = strBuff & Chr(c And &HFF)
            Next
        Else
            strBuff = strText
        End If

        encrypt = Server.URLEncode(strBuff)
    Else
        encrypt = ""
    End If
End Function

Function decrypt(strText)
    Dim i , c
    Dim strBuff
    If strKey <> "" And strText <> "" Then
        strKey = UCase(strKey)
        If Len(strKey) Then
            For i = 1 To Len(strText)
                c = Asc(Mid(strText, i, 1))
                c = c - Asc(Mid(strKey, (i Mod Len(strKey)) + 1, 1))
                strBuff = strBuff & Chr(c And &HFF)
            Next
        Else
            strBuff = strText
        End If

        decrypt = strBuff
    Else
        decrypt = ""
    End If
End Function
%>


Code - page1.asp

<%
Option Explicit
%>

<!-- #include file="encdec.asp" -->

<%
Dim id

id = 999

Response.Write "<form name=""Form1"">" & vbCrlf
Response.Write "<input type=""text"" name=""txtID"">" & vbCrlf
Response.Write "<br>" & vbCrlf
Response.Write "<a href=""page2.asp?id=" & encrypt("999") & """>Click Me</a>" & vbCrlf
Response.Write "</form>" & vbCrlf
%>

Code - page2.asp

<%
Option Explicit
%>

<!-- #include file="encdec.asp" -->

<%
Dim id

id = decrypt(Request.QueryString("id"))

Response.Write "Encrypted id<br>"
Response.Write "id = " & Request.QueryString("id")

Response.Write "<br><br>"

Response.Write "Decrypted id<br>"
Response.Write "id = " & id
%>


Java Side
-----------

Code - EncDec.java

public class EncDec {
    public static String encryptText(String strText, String strKey) {
         int i , c;
         String strBuff="";

         if ( strKey != "" && strText != "" ) {
              strKey = strKey.toUpperCase();

              if ( strKey.length() > 0 ) {
                   for ( i=0; i<strText.length(); i++ ) {
                        c = (int)strText.charAt(i);
                        System.out.println(i % strKey.length() + 1);
                        c = c + (int)strKey.charAt(i % strKey.length());
                        strBuff = strBuff + (char)(c & 0xFF);    // <-- I am sure is wrong
                   }
              }
              else {
                   strBuff = strText;
              }

              return strBuff;
         }
         else {
              return "";
         }
    }

    public static String decryptText(String strText, String strKey)     {
         int i, c;
         String strBuff="";

         if ( strKey != "" && strText != "" ) {
              strKey = strKey.toUpperCase();

              if ( strKey.length() > 0 ) {
                   for ( i=0; i<strText.length(); i++ ) {
                        c = (int)strText.charAt(i);
                        c = c - (int)(strKey.charAt(i % strKey.length()));
                        strBuff = strBuff + (char)(c & 0xFF);    // <-- I am sure is wrong
                   }
              }
              else {
                   strBuff = strText;
              }

              return strBuff;
         }
         else {
              return "";
         }
    }
}


Code - test.java

public class test {
    public static void main(String args[]) {
        String id, enc_id, dec_id;
        String strKey = "abcdef";

        id = "999";
        enc_id = EncDec.encryptText(id, strKey);
        System.out.println("Encrypted id");
        System.out.println(enc_id+"\n");

        dec_id = EncDec.decryptText(enc_id, strKey);
        System.out.println("Decrypted id");
        System.out.println(dec_id+"\n");
    }
}



If you were to test the 2 codes, they give different encrypted results.
Is there an equivalent for ASP?
I need both ASP and Java to use the same encryption/decryption logic.
Sorry, for ASP you need to ask in the ASP topic area.
My original question is to convert a asp to java.
It is not done at all.
Not a single line of code.

I then request for a delete but then was told to state why.
I stated and gave an example why it is not working.

Then what I got was a java des algo which I can easily get from the internet.
It is not solving my original question at all.

Please delete and refund.
You asked: >> Is there an equivalent for ASP?

That can be answered in the ASP topic-area, not here.
About: >> If you were to test the 2 codes, they give different encrypted results.

We don't know ASP so we can't tell you why the ASP code is giving a different result than expected - that's why I said you should've asked this in ASP too (or maybe posted a link to this Q in the ASP area). I'm ok if this Q stays open for a while until the problem is completely solved - post a link in the ASP topic-area.