Some time ago, work needs, need to use JS and Java matching base64 algorithm, but did not find, and then online copy a set. It is still very effective to use.
Gossip less flocculation, directly on dry.
JS version Base 64 algorithm Base64.js
var keystr = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/=";// The ANSI-encoded string is BASE64 encoded function encode64 (input) {var output = "";var chr1, chr2, chr3 = "";var enc1, enc2, enc3, enc4 = ""; var i = 0;do {chr1 = input.charcodeat (i++); Chr2 = input.charcodeat (i++); chr3 = input.charcodeat (i++);enc1 = chr1 >> 2;enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);enc3 = ((chr2 & &NBSP;15) << 2) | (chr3 >> 6);enc4 = chr3 & 63;if (IsNaN (CHR2)) {enc3 = enc4 = 64;} else if (IsNaN (CHR3)) {enc4 = 64;} Output = output + keystr.charat (ENC1) + keystr.charat (ENC2) + keyStr.charAt ( ENC3) &NBSP;+&NBsp;keystr.charat (ENC4);chr1 = chr2 = chr3 = ""; enc1 = enc2 = enc3 = enc4 = "";} while (i < input.length); return output;} Converts an BASE64 encoded string into an ANSI-encoded string function decode64 (input) {var output = ""; Var chr1, chr2, chr3 = ";var enc1, enc2, enc3, enc4 = "; var i = 0;if (input.length % 4 != 0) {return "";} var base64test = /[^a-za-z0-9\+\/\=]/g;if (base64test.exec (input)) {return "";} Do {enc1 = keystr.indexof (Input.charat (i++)); Enc2 = keystr.indexof (Input.charAt (i++)) ; Enc3 = keystr.indexof (Input.charat (i++)); Enc4 = keystr.indexof (Input.charAt (i++)); chr1 = (enc1 << 2) | (enc2 >> 4);chr2 = ((enc2 &&NBSP;15) << 4) | (ENC3&NBSP;>>&NBSP;2);chr3 = ((enc3 & 3) << 6) | enc4;output = output + string.fromcharcode (CHR1);if (enc3 != 64) {output += string.fromcharcode (CHR2);} if (enc4 != 64) {output += string.fromcharcode (CHR3);} chr1 = chr2 = chr3 = ""; enc1 = enc2 = enc3 = enc4 = "";} while (i < input.length); return output;} Function utf16to8 (str) { var out, i, len, c; out = ""; len = str.length; for (i = 0; i < len; i++) { c = str.charcodeat (i); if ((c >= 0x0001) && (c <= 0x007f) { out +=&nbsP;str.charat (i); } else if (c > 0x07ff) { out += string.fromcharcode (0xe0 | (c >> 12) & 0x0f)); out += string.fromcharcode (0x80 | ( c >> 6) ( & 0x3f)); out += String.fromCharCode (0x80 | ((c >> 0) & 0x3f)); } else { out += string.fromcharcode (0xC0 | ((c >> 6) & 0x1f); out += string.fromcharcode (0x80 | ((c >> 0) & 0x3f)); } } return out;} Function utf8to16 (str) { var out, i, len, c; var char2, char3; out = ""; len = Str.length; i = 0; while (I < len) { c = str.charcodeat (i++); switch (c >> 4) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: // 0xxxxxxx out += str.charat (i-1); break; case 12: case 13: // 110x xxxx 10xx xxxx char2 = str.charcodeat (i++); out += string.fromcharcode ((c & 0x1f) << 6) | (char2 & 0x3F)); break; case 14: // 1110 xxxx 10xx xxxx 10xx xxxx char2 = str.charcodeat (i++); char3 = str.charcodeat (i++); out += string.fromcharcode ((c & 0x0f) << 12) | ((char2 & 0x3f) << 6) | ((char3 & 0x3f) << 0); break; } } return Out;}
Examples of parsing in JS:
Encryption
var testcode= "test88"; var resultcode= encode64 (Utf16to8 (Testcode)); Console.log (ResultCode);
Output
DGVzdDg4
Decrypt
var testcode= "DGVzdDg4"; resultcode= utf8to16 (Decode64 (Testcode)); Console.log (ResultCode);
Output
Test88
Parsing the base64 encoding in Java:
Encryption
String localstr= "test88"; String resultstr =base64.getencoder (). Encodetostring (Localstr.getbytes ()); System.out.println (RESULTSTR);
Output
DGVzdDg4
Decrypt
String teststr= "DGVzdDg4"; String Testresult=new string (Base64.getdecoder (). Decode (TESTSTR)); System.out.println (TestResult);
Output
Test88
Attached base64.js for base64 encryption decrypted JS component, test.html for the test file in JS Test.java as a test file in Java
This article is from "Your father's shawl Hair" blog, please be sure to keep this source http://nimalegebi.blog.51cto.com/5614287/1828160
Implementation of JavaScript Base64 algorithm and Base64 encryption decryption in Java