1

I have written a simple JSP LOGIN page. I have received password in encrypted format. I am decrypting it. So just for a try i am encrypting a sample string, so it needs to then encode byte array to Hexadecimal string, which i am trying to do in the last statement of my code.

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.security.*" %>
<%@ page import="javax.crypto.*" %>
<%@ page import="javax.crypto.spec.*" %>
<%@ page import="java.lang.*" %>

<HTML>
<HEAD>
<TITLE>Simple JSP/Oracle Query Example</TITLE>
</HEAD>
<BODY>

<%
   Class.forName("oracle.jdbc.OracleDriver");

   Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@xxx:xxxx:xxxx","xxxxxx","xxxxxx");
                        // @//machineName:port:SID,   userid,  password

    Statement st=conn.createStatement();

    ResultSet rs=st.executeQuery("Select * from Cxxxxxxx");

    while(rs.next()){
        String name=rs.getString("user_id");
        String p=rs.getString("password");
        out.println(name+":"+p);
        out.println("</br>");


    String algorithm1 = "DES";//magical mystery constant
    String algorithm2 = "DES/CBC/NoPadding";//magical mystery constant
    IvParameterSpec iv = new IvParameterSpec( new byte [] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } );//magical mystery constant
    Cipher cipher;
    SecretKey key;
    String k="12345abc";
    key = new SecretKeySpec( k.getBytes("UTF-8"), algorithm1 );
    cipher = Cipher.getInstance( algorithm2 );

    String str="test4abc";

    cipher.init( Cipher.ENCRYPT_MODE, key, iv ); //normally you could leave out the IvParameterSpec argument, but not with Oracle

    byte[] bytes=str.getBytes("UTF-8");

    byte[] encrypted = cipher.doFinal( bytes );

    //Problem is in the statement BELOW --->
    String encoded = new String( Hex.encodeHex( encrypted ) );
    }
%>  
</BODY>
</HTML>

Error I am receiving is that:

[jsp src:line #:48]
cannot resolve symbol symbol : variable Hex location: class _check1 String encoded = new String( Hex.encodeHex( encrypted ) ); 

How should I make this statement run or any other alternative to encode ByteArray to HexaDecimal String

Murtaza
  • 308
  • 1
  • 3
  • 18
  • Wait, why do you say "org.apache.commons.codec.binary.Hex which is in java.lang."? The class `Hex` is in package `org.apache.commons.codec.binary`, not in package `java.lang`. – Ray Toal Jul 10 '12 at 04:49
  • So how to include that package in JSP?? – Murtaza Jul 10 '12 at 04:57

2 Answers2

2

If Hex is meant to be org.apache.commons.codec.binary.Hex then you need to import it as such at the top of your JSP (or alternately you can do org.apache.commons.codec.binary.Hex.encodeHex( encrypted )). It is definitely not inside of java.lang.*. Whoever told you that was incorrect.

Also, please, please, please do not put Java code inside of JSP files.

aroth
  • 54,026
  • 20
  • 135
  • 176
  • SO how could I then convert byte array to HexaDecimal String, as I need it for encrypting and decrypting. – Murtaza Jul 10 '12 at 04:54
  • Here's a page with many different options: http://stackoverflow.com/questions/332079/in-java-how-do-i-convert-a-byte-array-to-a-string-of-hex-digits-while-keeping-l – aroth Jul 10 '12 at 04:59
  • [jsp src:line #:48] package org.apache.commons.codec.binary does not exist String encoded = new String( org.apache.commons.codec.binary.Hex.encodeHex( encrypted ) ); This is the error I am receiving when I change that code, my question is how to include this package? – Murtaza Jul 10 '12 at 04:59
  • I have tried these. but whenever I try to include this Integer.to.... function it gives the same error Some library is missing, dont know which one. – Murtaza Jul 10 '12 at 05:01
  • You need to make sure the commons-codec JAR file is on your webapp's classpath. The simplest way to do that is to include it in your WAR file's `/lib` folder, assuming that you're building/deploying a WAR file. – aroth Jul 10 '12 at 05:01
  • Not deploying a war file, I am using oracle application server. So it compiles it byself, but anyway thanks I'll look forward to this idea – Murtaza Jul 10 '12 at 05:05
1

I was facing a similar issue a while back when I was working with JSP. The error got solved by downloading

org.apache.commons.codec.binary 1.5 binary 

and placing it in the lib folder. Then accessing that function by:

org.apache.commons.codec.binary.Hex.encodeHex( encrypted ))

This should resolve your problem.

Cheers!

okay_google
  • 339
  • 4
  • 17