2

I am getting an error when I compile it by the tomcat9 server :

SEVERE: Servlet.service() for servlet [sign_in] in context with path [/Eduinq123] threw exception [Servlet execution threw an exception] with root cause java.lang.Error: Unresolved compilation problem: sun.misc.BASE64Decoder cannot be resolved to a type

package encryption;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

public class Encryption_Class {

    Cipher ecipher;
   Cipher dcipher;
   private String   key="eleaon8547";
   public String getKey()
   { 
    return key;
    }
   // 8-byte Salt
   byte[] salt = {
       (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
       (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03
   };
   // Iteration count
   int iterationCount = 19;
   public Encryption_Class() { 

   }

   /**
    * 
    * @param secretKey Key used to encrypt data
    * @param plainText Text input to be encrypted
    * @return Returns encrypted text
    * 
    */
   public String encrypt(String secretKey, String plainText) 
           throws NoSuchAlgorithmException, 
           InvalidKeySpecException, 
           NoSuchPaddingException, 
           InvalidKeyException,
           InvalidAlgorithmParameterException, 
           UnsupportedEncodingException, 
           IllegalBlockSizeException, 
           BadPaddingException{
       //Key generation for enc and desc
       KeySpec keySpec = new PBEKeySpec(secretKey.toCharArray(), salt, iterationCount);
       SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);        
        // Prepare the parameter to the ciphers
       AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

       //Enc process
       ecipher = Cipher.getInstance(key.getAlgorithm());
       ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);     
       String charSet="UTF-8";       
       byte[] in = plainText.getBytes(charSet);
       byte[] out = ecipher.doFinal(in);
       String encStr=new sun.misc.BASE64Encoder().encode(out);//Here I  get error.
       return encStr;
   }
    /**     
    * @param secretKey Key used to decrypt data
    * @param encryptedText encrypted text input to decrypt
    * @return Returns plain text after decryption
    */
   public String decrypt(String secretKey, String encryptedText)
    throws NoSuchAlgorithmException, 
           InvalidKeySpecException, 
           NoSuchPaddingException, 
           InvalidKeyException,
           InvalidAlgorithmParameterException, 
           UnsupportedEncodingException, 
           IllegalBlockSizeException, 
           BadPaddingException, 
           IOException{
        //Key generation for enc and desc
       KeySpec keySpec = new PBEKeySpec(secretKey.toCharArray(), salt, iterationCount);
       SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);        
        // Prepare the parameter to the ciphers
       AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
       //Decryption process; same key will be used for decr
       dcipher=Cipher.getInstance(key.getAlgorithm());
       dcipher.init(Cipher.DECRYPT_MODE, key,paramSpec);
       byte[] enc = new sun.misc.BASE64Decoder().decodeBuffer(encryptedText); // Here I get error in eclipse id.
       byte[] utf8 =dcipher.doFinal(enc);
       String charSet="UTF-8";     
       String plainStr = new String(utf8, charSet);
       return plainStr;
   } 
}

What should I do to overcome, any help appreciated. Thanks

Jubin Justifies
  • 397
  • 4
  • 12
  • You are trying to use an "internal" class that was removed in Java 9. See the sup link, and also https://bugs.openjdk.java.net/browse/JDK-8097121. – Stephen C Dec 27 '19 at 06:12
  • I din't get your answers @Stephen C and right now I am using jdk 11 .Can you elaborate it. – siddharth ojha Dec 28 '19 at 04:11

0 Answers0