Use base64 for encryption and decryption to solve the problem of garbled characters when an image or attachment is generated in XML format.

Source: Internet
Author: User
Base64 is one of the most common encoding methods used to transmit 8-bit code on the network. When sending an email, the user name and password for server authentication must be base64 encoded, the attachment must also be base64-encoded.
The following describes the principles of the base64 algorithm. Because the code is too long, it will not be posted here.
Base64 requires that each three 8-bit bytes be converted into four 6-bit bytes (3*8 = 4*6 = 24), and then 6-bit bytes be added with two more high 0 values, it consists of four 8-bit bytes. That is to say, the converted string is theoretically 1/3 longer than the original one.
After conversion, we use a code table to obtain the desired string (that is, the final base64 encoding). This table is like this:
0 A 17 R 34 I 51 Z
1 B 18 S 35 J 52 0
2 C 19 t 36 K 53 1
3 D 20 u 37 L 54 2
4 E 21 V 38 m 55 3
5 F 22 W 39 n 56 4
6g 23x40 o 57 5
7 H 24 y 41 P 58 6
8 I 25 Z 42 Q 59 7
9 J 26 A 43 R 60 8
10 K 27 B 44 s 61 9
11 l 28 C 45 t 62 +
12 m 29 D 46 U 63/
13 N 30 E 47 v
14 O 31 F 48 W (PAD) =
15 p 32G 49 x
16 Q 33 H 50 y
0 is used to supplement the last three bytes in the original text, and the base64 encoding is replaced by =. This is why some base64 encoding ends with one or two equal signs, but the equal signs can only be two at most.
For example, after base64 encoding, ABC returns ywjj.
  1. Private Static final string charset_name = "iso8859_1 ";
  2. Public static byte [] base64decode (string s) throws ioexception,
  3. Javax. Mail. messagingexception {
  4. If (S = NULL | S. Length () = 0 ){
  5. Return NULL;
  6. }
  7. Bytearrayinputstream BAIS = new bytearrayinputstream (S
  8. . Getbytes (charset_name ));
  9. Inputstream is = mimeutility. Decode (BAIS, "base64 ");
  10. Bytearrayoutputstream out = new bytearrayoutputstream ();
  11. Int CH =-1;
  12. While (CH = is. Read ())! =-1 ){
  13. Out. Write (CH );
  14. }
  15. Out. Flush ();
  16. BAIS. Close ();
  17. Return out. tobytearray ();
  18. }
  19. Public static string base64encode (byte [] BUF) throws ioexception,
  20. Javax. Mail. messagingexception {
  21. If (BUF = NULL | Buf. Length = 0 ){
  22. Return "";
  23. }
  24. Inputstream is = new bytearrayinputstream (BUF );
  25. Bufferedinputstream Bis = new bufferedinputstream (is );
  26. Bytearrayoutputstream baos = new bytearrayoutputstream ();
  27. Outputstream OS = javax. Mail. Internet. mimeutility
  28. . Encode (baos, "base64 ");
  29. Int CHR =-1;
  30. While (CHR = bis. Read ())! =-1 ){
  31. OS. Write (CHR );
  32. }
  33. Bis. Close ();
  34. Baos. Close ();
  35. Return new string (baos. tobytearray (), charset_name );
  36. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.