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?
cutie2000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
It will compile fine - are you facing any problems while running?
0
Mayank SAssociate Director - Product EngineeringCommented:
You should use a StringBuffer a StringBuilder instead of concatenating strings using + and =
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Leo EikelmanDirector, IT and Business DevelopmentCommented:
you shold probably use a StringBuffer but if there are any problems, that would not be the cause of it.

Are you getting any errors?

Leo

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

cutie2000Author Commented:
I am not getting any help here.
I will request for a delete.
0
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
Leo EikelmanDirector, IT and Business DevelopmentCommented:
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.
0
cutie2000Author Commented:
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.
0
Mayank SAssociate Director - Product EngineeringCommented:
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?
0
Mayank SAssociate Director - Product EngineeringCommented:
No comments?
0
Leo EikelmanDirector, IT and Business DevelopmentCommented:
abandoned?
0
cutie2000Author Commented:
Give me till weekend and I will post.
Quite tighted with my work.
0
cutie2000Author Commented:
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.
0
Mayank SAssociate Director - Product EngineeringCommented:
Try this for encryption/ decryption: http://www.javaalmanac.com/egs/javax.crypto/DesString.html
0
cutie2000Author Commented:
Is there an equivalent for ASP?
I need both ASP and Java to use the same encryption/decryption logic.
0
Mayank SAssociate Director - Product EngineeringCommented:
Sorry, for ASP you need to ask in the ASP topic area.
0
cutie2000Author Commented:
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.
0
Mayank SAssociate Director - Product EngineeringCommented:
You asked: >> Is there an equivalent for ASP?

That can be answered in the ASP topic-area, not here.
0
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.