正在加载中,请稍后
author 首页 关于 笔记

按回车搜索更多

前后端高效率实现Base64编码解码
2018-08-18 阅读 {{counts.readCount}} 评论 {{counts.commentCount}}

前端javascript没什么可说的啦 直接贴代码吧

  1. // 引入base64.js
  2. <script type="text/javascript" src="js/base64.js"></script>

  1. // base64.js 文件内容
  2. function Base64() {  
  3.     // private property  
  4.     _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";  
  5.     // public method for encoding  
  6.     this.encode = function (input) {  
  7.         var output = "";  
  8.         var chr1, chr2, chr3, enc1, enc2, enc3, enc4;  
  9.         var i = 0;  
  10.         input = _utf8_encode(input);  
  11.         while (< input.length) {  
  12.             chr1 = input.charCodeAt(i++);  
  13.             chr2 = input.charCodeAt(i++);  
  14.             chr3 = input.charCodeAt(i++);  
  15.             enc1 = chr1 >> 2;  
  16.             enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);  
  17.             enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);  
  18.             enc4 = chr3 & 63;  
  19.             if (isNaN(chr2)) {  
  20.                 enc3 = enc4 = 64;  
  21.             } else if (isNaN(chr3)) {  
  22.                 enc4 = 64;  
  23.             }  
  24.             output = output +  
  25.             _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +  
  26.             _keyStr.charAt(enc3) + _keyStr.charAt(enc4);  
  27.         }  
  28.         return output;  
  29.     }  
  30.     // public method for decoding  
  31.     this.decode = function (input) {  
  32.         var output = "";  
  33.         var chr1, chr2, chr3;  
  34.         var enc1, enc2, enc3, enc4;  
  35.         var i = 0;  
  36.         input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");  
  37.         while (< input.length) {  
  38.             enc1 = _keyStr.indexOf(input.charAt(i++));  
  39.             enc2 = _keyStr.indexOf(input.charAt(i++));  
  40.             enc3 = _keyStr.indexOf(input.charAt(i++));  
  41.             enc4 = _keyStr.indexOf(input.charAt(i++));  
  42.             chr1 = (enc1 << 2) | (enc2 >> 4);  
  43.             chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);  
  44.             chr3 = ((enc3 & 3) << 6) | enc4;  
  45.             output = output + String.fromCharCode(chr1);  
  46.             if (enc3 != 64) {  
  47.                 output = output + String.fromCharCode(chr2);  
  48.             }  
  49.             if (enc4 != 64) {  
  50.                 output = output + String.fromCharCode(chr3);  
  51.             }  
  52.         }  
  53.         output = _utf8_decode(output);  
  54.         return output;  
  55.     }  
  56.     // private method for UTF-8 encoding  
  57.     _utf8_encode = function (string) {  
  58.         string = string.replace(/\r\n/g,"\n");  
  59.         var utftext = "";  
  60.         for (var n = 0; n < string.length; n++) {  
  61.             var c = string.charCodeAt(n);  
  62.             if (< 128) {  
  63.                 utftext += String.fromCharCode(c);  
  64.             } else if((> 127) && (< 2048)) {  
  65.                 utftext += String.fromCharCode((>> 6) | 192);  
  66.                 utftext += String.fromCharCode((& 63) | 128);  
  67.             } else {  
  68.                 utftext += String.fromCharCode((>> 12) | 224);  
  69.                 utftext += String.fromCharCode(((>> 6) & 63) | 128);  
  70.                 utftext += String.fromCharCode((& 63) | 128);  
  71.             }  
  72.  
  73.         }  
  74.         return utftext;  
  75.     } 
  76.     // private method for UTF-8 decoding  
  77.     _utf8_decode = function (utftext) {  
  78.         var string = "";  
  79.         var i = 0;  
  80.         var c = c1 = c2 = 0;  
  81.         while ( i < utftext.length ) {  
  82.             c = utftext.charCodeAt(i);  
  83.             if (< 128) {  
  84.                 string += String.fromCharCode(c);  
  85.                 i++;  
  86.             } else if((> 191) && (< 224)) {  
  87.                 c2 = utftext.charCodeAt(i+1);  
  88.                 string += String.fromCharCode(((& 31) << 6) | (c2 & 63));  
  89.                 i += 2;  
  90.             } else {  
  91.                 c2 = utftext.charCodeAt(i+1);  
  92.                 c3 = utftext.charCodeAt(i+2);  
  93.                 string += String.fromCharCode(((& 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));  
  94.                 i += 3;  
  95.             }  
  96.         }  
  97.         return string;  
  98.     }  
  99. }
  1. // 用法:先实例化
  2. var base = new Base64();
  3. // 加密
  4. var xxx = base.encode(xxx);
  5. // 解密
  6. var xxx = base.decode(xxx);


后端就厉害了,我找了3种方法实现,最后才发现,最好用的,效率最高的,其实就是java8自带的。。。

只贴java8自带的代码了

  1. // 引入依赖必须选java.util.Base64
  2. import java.util.Base64;
  3. public class BASE64Utils{
  4. // 已经直接写成了最简单直接的方法,丢在工具类 ,无脑调用就行
  5. public static String base64Encode(String text) throws IOException{
  6. return Base64.getEncoder().encodeToString(text.getBytes("UTF-8"));
  7. }
  8. public static String base64Decode(String text) throws IOException{
  9. return new String(Base64.getDecoder().decode(text), "UTF-8");
  10. }
  11. }
  1. // 例如工具类叫BASE64Utils
  2.  
  3. // 加密
  4. String xxx = BASE64Utils.base64Encode("xxx");
  5. // 解密
  6. String xxx = BASE64Utils.base64Decode("xxx");


大功告成啦 若有问题可以留言哈~

提交
评论区空空如也,赶紧添加一条评论吧 评论 {{counts.commentCount}}
{{comment.name}} {{comment.os}} {{comment.browser}}
{{dateFormatter(comment.createTime)}}

{{comment.message}}

{{comment.reply.name}} {{comment.reply.os}} {{comment.reply.browser}}
{{dateFormatter(comment.reply.createTime)}}

{{comment.reply.message}}

zzzmh
关于我 留言板

网址导航

{{alert.message}}
留言板 * 站长不经常查看信箱 若有重要事宜联系邮箱 admin@zzzmh.cn 取消 发送