zoukankan      html  css  js  c++  java
  • js base64加密与C#后台base64解密

    js代码

      1   function encode64(input) {
      2         var output = "";
      3         var base = new Base64();
      4         var output = base.encode(input);
      5         return output;
      6     }
      7 
      8     function Base64() {
      9 
     10         // private property
     11         _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
     12 
     13         // public method for encoding
     14         this.encode = function (input) {
     15             var output = "";
     16             var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
     17             var i = 0;
     18             input = _utf8_encode(input);
     19             while (i < input.length) {
     20                 chr1 = input.charCodeAt(i++);
     21                 chr2 = input.charCodeAt(i++);
     22                 chr3 = input.charCodeAt(i++);
     23                 enc1 = chr1 >> 2;
     24                 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
     25                 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
     26                 enc4 = chr3 & 63;
     27                 if (isNaN(chr2)) {
     28                     enc3 = enc4 = 64;
     29                 } else if (isNaN(chr3)) {
     30                     enc4 = 64;
     31                 }
     32                 output = output +
     33                 _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
     34                 _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
     35             }
     36             return output;
     37         }
     38 
     39         // public method for decoding
     40         this.decode = function (input) {
     41             var output = "";
     42             var chr1, chr2, chr3;
     43             var enc1, enc2, enc3, enc4;
     44             var i = 0;
     45             input = input.replace(/[^A-Za-z0-9+/=]/g, "");
     46             while (i < input.length) {
     47                 enc1 = _keyStr.indexOf(input.charAt(i++));
     48                 enc2 = _keyStr.indexOf(input.charAt(i++));
     49                 enc3 = _keyStr.indexOf(input.charAt(i++));
     50                 enc4 = _keyStr.indexOf(input.charAt(i++));
     51                 chr1 = (enc1 << 2) | (enc2 >> 4);
     52                 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
     53                 chr3 = ((enc3 & 3) << 6) | enc4;
     54                 output = output + String.fromCharCode(chr1);
     55                 if (enc3 != 64) {
     56                     output = output + String.fromCharCode(chr2);
     57                 }
     58                 if (enc4 != 64) {
     59                     output = output + String.fromCharCode(chr3);
     60                 }
     61             }
     62             output = _utf8_decode(output);
     63             return output;
     64         }
     65 
     66         // private method for UTF-8 encoding
     67         _utf8_encode = function (string) {
     68             string = string.replace(/
    /g, "
    ");
     69             var utftext = "";
     70             for (var n = 0; n < string.length; n++) {
     71                 var c = string.charCodeAt(n);
     72                 if (c < 128) {
     73                     utftext += String.fromCharCode(c);
     74                 } else if ((c > 127) && (c < 2048)) {
     75                     utftext += String.fromCharCode((c >> 6) | 192);
     76                     utftext += String.fromCharCode((c & 63) | 128);
     77                 } else {
     78                     utftext += String.fromCharCode((c >> 12) | 224);
     79                     utftext += String.fromCharCode(((c >> 6) & 63) | 128);
     80                     utftext += String.fromCharCode((c & 63) | 128);
     81                 }
     82 
     83             }
     84             return utftext;
     85         }
     86 
     87         // private method for UTF-8 decoding
     88         _utf8_decode = function (utftext) {
     89             var string = "";
     90             var i = 0;
     91             var c = c1 = c2 = 0;
     92             while (i < utftext.length) {
     93                 c = utftext.charCodeAt(i);
     94                 if (c < 128) {
     95                     string += String.fromCharCode(c);
     96                     i++;
     97                 } else if ((c > 191) && (c < 224)) {
     98                     c2 = utftext.charCodeAt(i + 1);
     99                     string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
    100                     i += 2;
    101                 } else {
    102                     c2 = utftext.charCodeAt(i + 1);
    103                     c3 = utftext.charCodeAt(i + 2);
    104                     string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
    105                     i += 3;
    106                 }
    107             }
    108             return string;
    109         }
    110     }
    View Code
     1   /// <summary>
     2         /// base64解密
     3         /// </summary>
     4         /// <param name="encode">编码,与js一致</param>
     5         /// <param name="result">加密源字符串</param>
     6         /// <returns></returns>
     7         public static string DecodeBase64(Encoding encode, string result)
     8         {
     9             string decode = "";
    10             byte[] bytes = Convert.FromBase64String(result);
    11             try
    12             {
    13                 decode = encode.GetString(bytes);
    14             }
    15             catch
    16             {
    17                 decode = result;
    18             }
    19             return decode;
    20         }
    View Code
  • 相关阅读:
    华为2019软件题
    图像的存储格式转化(python实现)
    windows+两个ubuntu系统的引导启动问题
    《视觉SLAM十四讲》课后习题—ch6
    视觉SLAM十四讲课后习题—ch8
    LINQ根据时间排序问题(OrderBy、OrderByDescending)
    Element的扩展
    CSharp
    jQuery函数使用记录
    日记越累
  • 原文地址:https://www.cnblogs.com/wsl2011/p/9717639.html
Copyright © 2011-2022 走看看