问题
When I return encrypted or decrypted string in Base64 format it cant resolve
BASE64Encoder()and
BASE64Dencoder()`. 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