0

I have tried a lot tested each step of my program, i get output but when i am testing BASE64Encoder() line it is not working,i stuck completely even eclipse not showing any error on BASE64Encoder() line ,I want your help how to get rid of this problem , code is given below

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
    }
David Conrad
  • 15,432
  • 2
  • 42
  • 54
satish
  • 17
  • 1
  • 5
  • It works for me, and yes I know I shouldn't be using it. Do you get an 'unknown error' meaning an exception, and if so what? or do you just get an empty String? – user207421 Apr 22 '14 at 00:00
  • I tested out each line working properly but when I want string value encStr it not works for me unknown error not output even no exception problem showing in code @EJP – satish Apr 22 '14 at 04:00
  • You're not being clear. What does 'unknown error' mean? Do you get the 'Pincode city:' part of the output? This mightn't be a Base64 problem at all, just an I/O problem. – user207421 Apr 22 '14 at 23:22
  • @EJP when i run this program in desktop by removing httpservletresponse it is working and encrypting but when i deploy this in google appengine and providing the string it stucks no output – satish Apr 23 '14 at 07:01

1 Answers1

2

The solution is probably solved by not using any of the Sun classes. The Sun classes (i.e those with sun.* package names distributed with the JRE) are not part of the Java API and should not be used. Use either Guava, Apache Commons Codec or Bouncy Castle lightweight API to perform the decoding.

For example, use org.apache.commons.codec.binary.Base64.decodeBase64(String).

If you want to stick to the normal Java API, use either java.util.Base64 (Java 8 onwards) or use up javax.xml.bind.DatatypeConverter according to the answers of this question.

Community
  • 1
  • 1
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • What I can write in place of sun inner class @owlstead – satish Apr 21 '14 at 18:17
  • Put in a link to the Apache Codec in the answer. Guava is case sensitive and Bouncy is a very large library. The Java classes are either XML specific or are only available from Java 8 onwards. – Maarten Bodewes Apr 21 '14 at 22:28
  • 1
    They are Sun classes, but they are not 'inner classes'. – user207421 Apr 21 '14 at 23:36
  • @EJP Hmm, yeah. Um. You are right of course. Should I call them Sun implementation classes? Normally I would just say you should not use `sun.*` classes, but Sun has unhelpfully created a few API's with `sun.*` packages, although those are not distributed with the JRE. – Maarten Bodewes Apr 21 '14 at 23:42
  • They're called sun.* classes in the Javadoc that tells you not to use them. – user207421 Apr 21 '14 at 23:43
  • 2
    @owlstead I think you're confusing internal `sun` packages with libraries distributed under `com.sun` -- the latter are OK to use. – Thorn G Apr 21 '14 at 23:53
  • @TomG Nice catch, thanks. It probably indicates that I should *definitely* go to sleep ;) – Maarten Bodewes Apr 22 '14 at 00:12