Base64 encryption and decryption

Source: Internet
Author: User
The base64 encryption principle is as follows: 1. convert each character in the string to an octal number (the number of digits is not enough to add 0 at a high position, for example, if the binary number of A is '000000' and the value of 7 is changed to '000000', a long string of 1100001 is obtained by concatenating the binary number of each character. 2. then convert the 6-digit 6-digit number in this string into a 10-digit number, and then find the corresponding character in the following table, so that the encrypted string is obtained. 0 A 9 J 18 s 27 B 36 K 45 t 54 2 63/1 B 10 K 19 t 28 C 37 L 46 U 55 3 64 = 2 C 11 l 20 u 29 D 38 m 47 v 56 4 3 D 12 m 21 V 30 E 39 n 48 W 57 5 4 E 13 N 22 W 31 F 40 o 49x58 6 5 F 14 O 23x32G 41 P 50 y 59 7 6g 15 p 24 y 33 H 42 Q 51 Z 60 8 7 H 16 Q 25 Z 34 I 43 R 52 0 61 9 8 I 17 R 26 35 J 44 s 53 1 62 + (this may happen when processing the last 6 digits, if there are 2 or 4 digits left in the binary string, add 4 0 or 2 0 at the end to make up 6 digits, when adding four zeros, you must add two equal signs (=) to the last encrypted string and one equal sign (=) to the string. ') The opposite process is involved in decryption, so it is not described in detail. The following code implements base64 encryption and decryption in Java: Import Java. io. ioexception; <br/> import sun. misc. base64decoder; <br/> import sun. misc. base64encoder; <br/> public class base64coding {<br/> Private Static base64encoder encoder = new sun. misc. base64encoder (); <br/> Private Static base64decoder decoder = new sun. misc. base64decoder (); <br/> Public base64coding () {}< br/> Public static string encode (string s) {<br/> return encoder. encode (S. getbytes (); <br/>}< br/> Public static string decode (string s) {<br/> try {<br/> byte [] temp = decoder. decodebuffer (s); <br/> return new string (temp); <br/>} catch (ioexception IOE) {<br/> // handler <br/>}< br/> return S; <br/>}< br/>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> string STR = "13516741234"; <br/> system. out. println ("encrypted"); <br/> system. out. println (base64coding. encode (STR); <br/> system. out. println ("decrypt"); <br/> system. out. println (base64coding. decode (base64coding. encode (STR); <br/>}< br/>}

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.