Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 487
  • Last Modified:

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?
0
cutie2000
Asked:
cutie2000
  • 9
  • 6
  • 3
2 Solutions
 
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
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now