Image encryption and decryption sun.misc.Base64Encoder and sun.misc.Base64Decoder cannot be used

1. Problem: After updating the JDK version, it is found that sun.misc.Base64Encoder and sun.misc.Base64Decoder in the project cannot be used.

2. Reason: /lib/tool.jar and /lib/rt.jar in JDK have been deleted from Java SE 9

3. Problem solving: replace the encryption and decryption method before JDK 9 with the encryption and decryption method after JDK 9

Image encryption: indicates that the image is converted to base64
Image decryption: indicates that the base64 is converted to image

1. Picture encryption and decryption used before JDK 9 (old):
   a) Encryption:
   BASE64Encoder encoder = new BASE64Encoder();

   String imagestr = encoder.encode(captcha);

   b) Decryption:
   BASE64Decoder decoder = new BASE64Decoder();

   byte[] bytes = decoder.decodeBuffer(imagestr);

2. Image encryption and decryption after JDK 9 (new):

   a)导入包
   import java.util.Base64.Encoder;
   import java.util.Base64.Decoder;
   import java.util.Base64;

   b)图片加密:
   Encoder encoder = Base64.getEncoder();
   String result = encoder.encodeToString(byteArray);

    c) Picture decryption:
   Decoder decoder = Base64.getDecoder();
   byte[] result = decoder.decode(str);

Everyone is welcome to read. I have limited knowledge, and there are inevitably mistakes or omissions in the blog I wrote. I hope you guys can give me some advice. Thank you.

Guess you like

Origin blog.csdn.net/qq_41936224/article/details/107980683