java BASE64Encoder 是内部 API,可能会在未来版本中删除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36578625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:37:53  来源:igfitidea点击:

BASE64Encoder is internal API and may be removed in future release

javaencodingbase64

提问by Tomá?

I tried to solve this problem but I never come to a solution that would work for me. The problem is that I am getting warings about BASE64Encoder. Is there any other way to do this without the BASE64Encoder?

我试图解决这个问题,但我从来没有找到适合我的解决方案。问题是我收到了关于 BASE64Encoder 的警告。没有 BASE64Encoder 有没有其他方法可以做到这一点?

The code:

代码:

public static String Encrypt(String Data) throws Exception 
{
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data.getBytes());
    String encryptedValue = new BASE64Encoder().encode(encVal); //Here is the problem

    return encryptedValue;
}

public static String Decrypt(String encryptedData) throws Exception 
{
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData); //Another problem
    byte[] decValue = c.doFinal(decordedValue);
    String decryptedValue = new String(decValue);

    return decryptedValue;
}

private static Key generateKey() throws Exception 
{
    Key key = new SecretKeySpec(keyValue, ALGO);
    return key;
}

回答by ManoDestra

You should now be using the Base64 Encoder and Decoder classes (from Java 8 onwards).

您现在应该使用 Base64 Encoder 和 Decoder 类(从 Java 8 开始)。

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

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

import java.util.Base64;
import java.util.UUID;
import java.io.UnsupportedEncodingException;

public class HelloWorld {
   public static void main(final String... args) {
      try {
         // Encode using basic encoder
         String base64encodedString = Base64.getEncoder().encodeToString("TutorialsPoint?java8".getBytes("utf-8"));
         System.out.println("Base64 Encoded String (Basic) :" + base64encodedString);

         // Decode
         byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString);

         System.out.println("Original String: " + new String(base64decodedBytes, "utf-8"));
         base64encodedString = Base64.getUrlEncoder().encodeToString("TutorialsPoint?java8".getBytes("utf-8"));
         System.out.println("Base64 Encoded String (URL) :" + base64encodedString);

         StringBuilder stringBuilder = new StringBuilder();

         for (int i = 0; i < 10; ++i) {
            stringBuilder.append(UUID.randomUUID().toString());
         }

         byte[] mimeBytes = stringBuilder.toString().getBytes("utf-8");
         String mimeEncodedString = Base64.getMimeEncoder().encodeToString(mimeBytes);
         System.out.println("Base64 Encoded String (MIME) :" + mimeEncodedString);

      } catch (UnsupportedEncodingException e) {
         System.out.println("Error :" + e.getMessage());
      }
   }
}

Code taken from HERE.

代码取自HERE