没有输出BASE64Encoder()

时间:2014-04-21 16:05:32

标签: java arrays java-ee servlets servlet-3.0

我已经尝试了很多测试我的程序的每一步,我得到输出,但当我测试BASE64Encoder()行它不工作,我完全卡住eclipse没有在BASE64Encoder()行显示任何错误,我想要你帮助如何摆脱这个问题, 代码如下:

private void findMeaning(HttpServletResponse resp,String plainText) throws NoSuchAlgorithmException, 
    InvalidKeySpecException, 
    NoSuchPaddingException, 
    InvalidKeyException,
    InvalidAlgorithmParameterException, 
    UnsupportedEncodingException, 
    IllegalBlockSizeException, 
    BadPaddingException, 
    IOException{ 
        String text = plainText;
        String key="ezeon8547";
        KeySpec keySpec = new PBEKeySpec(key.toCharArray(), salt, iterationCount);//working
        SecretKey key1 = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);    //working    
         // Prepare the parameter to the ciphers
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

        ecipher = Cipher.getInstance(key1.getAlgorithm());//working
        ecipher.init(Cipher.ENCRYPT_MODE, key1, paramSpec);//working      
        String charSet="UTF-8";       
        byte[] in = text.getBytes(charSet);//working
        byte[] out = ecipher.doFinal(in);//working

        String encStr=new sun.misc.BASE64Encoder().encode(out);//unknown error
          sendResponse(resp, "Pincode city:" +encStr);//not get output
    }

1 个答案:

答案 0 :(得分:2)

解决方案可能是通过不使用任何Sun类来解决的。 Sun类(即与JRE一起分发sun.*包名的那些类)不是Java API的一部分,不应使用。使用Guava,Apache Commons Codec或Bouncy Castle轻量级API来执行解码。

例如,使用org.apache.commons.codec.binary.Base64.decodeBase64(String)

如果您想坚持使用普通的Java API,请使用java.util.Base64(Java 8以上)或根据this question的答案使用javax.xml.bind.DatatypeConverter