Cannot resolve symbol BASE64Decoder (Java version 9.0.1)

帅比萌擦擦* 提交于 2020-04-18 07:30:10

问题


When I return encrypted or decrypted string in Base64 format it cant resolveBASE64Encoder()andBASE64Dencoder()`. How can I resolve it?

import javax.crypto.*;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

class DesEncrypter {
    Cipher ecipher;
    Cipher dcipher;

public DesEncrypter(SecretKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    ecipher = Cipher.getInstance("DES");
    dcipher = Cipher.getInstance("DES");
    ecipher.init(Cipher.ENCRYPT_MODE, key);
    dcipher.init(Cipher.DECRYPT_MODE, key);
}

public String encrypt(String str) throws UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException {
    byte[] utf8 = str.getBytes("UTF8");
    byte[] enc = ecipher.doFinal(utf8);
    return new sun.misc.BASE64Encoder().encode(enc);
}

public String decrypt(String str) throws IOException, IllegalBlockSizeException, BadPaddingException {
    byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
    byte[] utf8 = dcipher.doFinal(dec);
    return new String(utf8, "UTF8");
}

}


回答1:


try to use the java9 one, not the sun.misc...:

https://docs.oracle.com/javase/9/docs/api/java/util/Base64.Decoder.html




回答2:


You should not use sun.misc in general. Those classes are internal to the JDK and may be removed with new versions of Java (as happened here).

I recommend using a third party library like Apache Codecs. There are a bunch of utility classes that would make it unneccessary to do any of the code you listed.

Site: https://commons.apache.org/proper/commons-codec/

Documentation: https://commons.apache.org/proper/commons-codec/archives/1.11/apidocs/org/apache/commons/codec/binary/Base64.html




回答3:


I had the same problem today while I was running JDK 8 on Intellij and a maven task wouldn't compile the project properly, giving me the same error. Solution was: I had JDK10 folder setup on my Environment Variables... just changed to JDK8 and everything compiled just fine.



来源:https://stackoverflow.com/questions/49341304/cannot-resolve-symbol-base64decoder-java-version-9-0-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!