2012-07-10 215 views
1

我寫了一個簡單的JSP LOGIN頁面。我以加密格式收到密碼。我正在解密它。所以只是爲了試一試我正在加密一個示例字符串,所以它需要將字節數組編碼爲十六進制字符串,我正在嘗試在我的代碼的最後一個語句中執行此操作。將字節數組轉換爲十六進制十進制字符串

<%@ 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> 

錯誤我收到的是:

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

我應該怎麼做這個聲明運行或任何其他可替代

+0

等等,你爲什麼說「org.apache.commons.codec.binary.Hex是java.lang。」?類'Hex'在'org.apache.commons.codec.binary'包中,而不是'java.lang'包中。 – 2012-07-10 04:49:59

+0

那麼如何在JSP中包含這個包? – Murtaza 2012-07-10 04:57:04

回答

1

當我使用JSP時,我正面臨着類似的問題。 誤差得到了通過下載

org.apache.commons.codec.binary 1.5 binary 

,並把它放在lib文件夾中解決。然後通過訪問該功能:

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

這應該可以解決您的問題。

乾杯!

+0

Thankyou Shahzeb。現在它工作得很好。 – Murtaza 2012-07-10 05:43:02

+0

你是歡迎隊友:) – Shahzeb 2012-07-10 05:45:52

2

如果Hex,就是要org.apache.commons.codec.binary.Hex然後進行編碼ByteArray的十六進制字符串您需要將它導入到JSP頂部(或者您可以執行org.apache.commons.codec.binary.Hex.encodeHex(encrypted))。在java.lang.*之內肯定是而不是。誰告訴你這是不正確的。

另外,請不要將Java代碼放入JSP文件中。

+0

那麼我怎麼可以將字節數組轉換爲HexaDecimal字符串,因爲我需要它來進行加密和解密。 – Murtaza 2012-07-10 04:54:03

+0

下面是一個包含許多不同選項的頁面:http://stackoverflow.com/questions/332079/in-java-how-do-i-convert-a-byte-array-to-a-string-of-hex-digits -de-keeping-l – aroth 2012-07-10 04:59:52

+0

[jsp src:line#:48] 包org.apache.commons.codec.binary不存在String encoded = new String(org.apache.commons.codec.binary.Hex.encodeHex(加密)); 這是我收到的錯誤,當我更改該代碼時,我的問題是如何包含此包? – Murtaza 2012-07-10 04:59:53

相關問題