我正在使用这个类进行编码/解码:
public class enc_dec {
public static Cipher dcipher, ecipher;
public enc_dec() {
byte[] salt = { (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
(byte) 0x56, (byte) 0x34, (byte) 0xE3, (byte) 0x03 };
int iterationCount = 19;
try {
// Generate a temporary key. In practice, you would save this key
// Encrypting with DES Using a Pass Phrase
KeySpec keySpec = new PBEKeySpec("".toCharArray(), salt,iterationCount);
SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES")
.generateSecret(keySpec);
ecipher = Cipher.getInstance(key.getAlgorithm());
dcipher = Cipher.getInstance(key.getAlgorithm());
// Prepare the parameters to the cipthers
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt,
iterationCount);
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
} catch (Exception e) { }
}
// Encrpt Password
@SuppressWarnings("unused")
public String encrypt(String str) {
try {
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
} catch (Exception e) {
}
return null;
}
// Decrpt password
// To decrypt the encryted password
public String decrypt(String str) {
Cipher dcipher = null;
try {
byte[] salt = { (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
(byte) 0x56, (byte) 0x34, (byte) 0xE3, (byte) 0x03 };
int iterationCount = 19;
try {
String passPhrase = "";
KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(),
salt, iterationCount);
SecretKey key = SecretKeyFactory
.getInstance("PBEWithMD5AndDES")
.generateSecret(keySpec);
dcipher = Cipher.getInstance(key.getAlgorithm());
// Prepare the parameters to the cipthers
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt,
iterationCount);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
} catch (Exception e) {}
// Decode base64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
// Decrypt
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
return new String(utf8, "UTF8");
} catch (Exception e) {
}
return null;
}
这是我的URL - 我从JSP页面发送:
http://localhost:8080/Sample_Project/send_encode.ENC?type=M0z7jTCb8PPfSZxfAdhw3Q==&payment=UxCU5BdZh4U=&sr_no=eN1X3XOeYuI=
这是我的send_encode servlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session=request.getSession(false);
if(session.getAttribute("session_set")!=null)
{
classes.enc_dec enc=new classes.enc_dec();
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
String type=enc.decrypt(request.getParameter("type"));
String payment=enc.decrypt(request.getParameter("payment"));
long sr_no=Long.parseLong(request.getParameter("sr_no"));
try{
RequestDispatcher comprd = getServletContext().getRequestDispatcher("/new_page.jsp?type="+enc.encrypt(type)+"&payment="+enc.encrypt(payment)+"");
comprd.include(request, response);
}
catch(Exception e)
{
System.out.println("The ecxeption is "+e);
}
}
我面临的例外是:
WARNING: StandardWrapperValve[send_encode]: Servlet.service() for servlet send_encode threw exception
java.lang.NumberFormatException: For input string: "eN1X3XOeYuI="
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:441)
at java.lang.Long.parseLong(Long.java:483)
at controller.send_encode.doGet(send_encode.java:30)
我已将编码设置为UTF-8,但我仍然得到例外。我也尝试过在SO上发布的其他问题,但无法获得任何帮助。 提前致谢