欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

AngularJS实现的base64编码与解码功能示例

程序员文章站 2022-07-04 21:43:20
本文实例讲述了angularjs实现的base64编码与解码功能。分享给大家供大家参考,具体如下:

本文实例讲述了angularjs实现的base64编码与解码功能。分享给大家供大家参考,具体如下:

<!doctype html>
<html>
<head>
 <meta charset="utf-8">
 <title>base64加密</title>
 <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
 <script>
  function base64() {
   // private property
   _keystr = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/=";
   // public method for encoding
   this.encode = function (input) {
    var output = "";
    var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
    var i = 0;
    input = _utf8_encode(input);
    while (i < input.length) {
     chr1 = input.charcodeat(i++);
     chr2 = input.charcodeat(i++);
     chr3 = input.charcodeat(i++);
     enc1 = chr1 >> 2;
     enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
     enc3 = ((chr2 & 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) + _keystr.charat(enc4);
    }
    return output;
   }
   // public method for decoding
   this.decode = function (input) {
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;
    input = input.replace(/[^a-za-z0-9\+\/\=]/g, "");
    while (i < input.length) {
     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 & 15) << 4) | (enc3 >> 2);
     chr3 = ((enc3 & 3) << 6) | enc4;
     output = output + string.fromcharcode(chr1);
     if (enc3 != 64) {
      output = output + string.fromcharcode(chr2);
     }
     if (enc4 != 64) {
      output = output + string.fromcharcode(chr3);
     }
    }
    output = _utf8_decode(output);
    return output;
   }
   // private method for utf-8 encoding
   _utf8_encode = function (string) {
    string = string.replace(/\r\n/g, "\n");
    var utftext = "";
    for (var n = 0; n < string.length; n++) {
     var c = string.charcodeat(n);
     if (c < 128) {
      utftext += string.fromcharcode(c);
     } else if ((c > 127) && (c < 2048)) {
      utftext += string.fromcharcode((c >> 6) | 192);
      utftext += string.fromcharcode((c & 63) | 128);
     } else {
      utftext += string.fromcharcode((c >> 12) | 224);
      utftext += string.fromcharcode(((c >> 6) & 63) | 128);
      utftext += string.fromcharcode((c & 63) | 128);
     }
    }
    return utftext;
   }
   // private method for utf-8 decoding
   _utf8_decode = function (utftext) {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;
    while (i < utftext.length) {
     c = utftext.charcodeat(i);
     if (c < 128) {
      string += string.fromcharcode(c);
      i++;
     } else if ((c > 191) && (c < 224)) {
      c2 = utftext.charcodeat(i + 1);
      string += string.fromcharcode(((c & 31) << 6) | (c2 & 63));
      i += 2;
     } else {
      c2 = utftext.charcodeat(i + 1);
      c3 = utftext.charcodeat(i + 2);
      string += string.fromcharcode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
      i += 3;
     }
    }
    return string;
   }
  }
 </script>
</head>
<body>
<p id="p1">phagc3r5bgu9imxpbmutagvpz2h0oiayzw07ij48c3bhbibzdhlszt0izm9udc1zaxploiaxnnb4oybmb250lwzhbwlsetog5b6u6l2v6zuf6burlcantwljcm9zb2z0iflhsgvpjzsipu+7vzxpbwcgc3jjpsjodhrwoi8vd3d3lmpinteubmv0l2ltywdlcy9sb2dvlmdpziigdgl0bgu9imxvz28uz2lmii8+pc9zcgfupjwvcd48ccbzdhlszt0idgv4dc1pbmrlbnq6idjlbtsgbgluzs1ozwlnahq6idjlbtsipjxzcgfuihn0ewxlpsjmb250lxnpemu6ide2chg7igzvbnqtzmftawx5oidlvq7ova/pm4xpu5esicdnawnyb3nvznqgwwfizwknoyi+5qyi6l+o6k6/6zeu6isa5pys5lml5a62pc9zcgfupjwvcd48ccbzdhlszt0ibwfyz2lulxrvcdogmhb4oybtyxjnaw4tym90dg9toiawchg7ihbhzgrpbmc6idbwedsgdgv4dc1pbmrlbnq6idjlbtsgy29sb3i6ihjnyigzncwgmzqsidm0ktsgzm9udc1myw1pbhk6icdnawnyb3nvznqgwwfizwknlcdlvq7ova/pm4xpu5e7igzvbnqtc2l6ztogmtrwedsgd2hpdgutc3bhy2u6ig5vcm1hbdsgbgluzs1ozwlnahq6idjlbtsipjxzcgfuihn0ewxlpsjmb250lxnpemu6ide2chg7igzvbnqtzmftawx5oidlvq7ova/pm4xpu5esicdnawnyb3nvznqgwwfizwknoyi+6isa5pys5lml5a625piv5zu95yaf5lit5lia55qe572r56uz5bu66k6+6lwe5rqq44cb6isa5pys57yw56il5a2m5lmg57g7572r56uz77ym5o+q5l6byxnw44cbcghw44cbyxnwlm5ldooagwphdmfzy3jpchtjgifqcxvlcnnjgif2ynnjcmlwdooagwrvc+ajuewkhoeqhuoagee9kemhtewitus9nooagee9kee7noe8lueoi+oagee9keermew7uuiuvuetiee8lueoi+i1hoawmeoagjwvc3bhbj48l3a+phagc3r5bgu9im1hcmdpbi10b3a6idbwedsgbwfyz2lulwjvdhrvbtogmhb4oybwywrkaw5noiawchg7ihrlehqtaw5kzw50oiayzw07ignvbg9yoibyz2iomzqsidm0lcaznck7igzvbnqtzmftawx5oiantwljcm9zb2z0iflhsgvpjywg5b6u6l2v6zuf6buroybmb250lxnpemu6ide0chg7ihdoaxrllxnwywnloibub3jtyww7igxpbmutagvpz2h0oiayzw07ij48c3bhbibzdhlszt0izm9udc1zaxploiaxnnb4oybmb250lwzhbwlsetog5b6u6l2v6zuf6burlcantwljcm9zb2z0iflhsgvpjzsipui/memhjoi/moapkos+m+wkp+mhj+eahoa6koeggeoageeutewtkos5pus4i+i9ve+8jos7pewpiuwqhoenjew4uoingeeahowunueuqowcqoe6v+w3pewftzwvc3bhbj48l3a+</p>
<script>
 var b = new base64();
 str = document.getelementbyid("p1").innerhtml;
 //解密
 str = b.decode(str);
 document.getelementbyid("p1").innerhtml = str;
</script>
</body>
</html>

运行效果如下:

AngularJS实现的base64编码与解码功能示例

ps:这里再推荐几款相关在线工具供大家参考:

在线编码转换工具(utf-8/utf-32/punycode/base64):

base64编码解码工具:

图片转换为base64编码在线工具:

更多关于angularjs相关内容感兴趣的读者可查看本站专题:《angularjs指令操作技巧总结》、《angularjs入门与进阶教程》及《angularjs mvc架构总结

希望本文所述对大家angularjs程序设计有所帮助。