将字节数组转换为十六进制十进制字符串
我写了一个简单的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));
我应该怎么做这个声明运行或任何其他可替代
如果Hex
,就是要org.apache.commons.codec.binary.Hex
然后进行编码ByteArray的十六进制字符串您需要将它导入到JSP顶部(或者您可以执行org.apache.commons.codec.binary.Hex.encodeHex(encrypted)
)。在java.lang.*
之内肯定是而不是。谁告诉你这是不正确的。
另外,请不要将Java代码放入JSP文件中。
那么我怎么可以将字节数组转换为HexaDecimal字符串,因为我需要它来进行加密和解密。 – Murtaza 2012-07-10 04:54:03
下面是一个包含许多不同选项的页面: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
[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
等等,你为什么说“org.apache.commons.codec.binary.Hex是java.lang。”?类'Hex'在'org.apache.commons.codec.binary'包中,而不是'java.lang'包中。 – 2012-07-10 04:49:59
那么如何在JSP中包含这个包? – Murtaza 2012-07-10 04:57:04