暖风拂柳 發表於 2020-4-29 09:30:00

javascript进行hex、base64、bytes[]、string的互转

<h2 id="0x01-简介">0x01 简介</h2>
<blockquote>
<p>这段代码部分是从Brida中提取出来以及网上收集拼凑的,用以实现hex、base64、bytes[]、string这几种方式的互相转换,base64ToBytes暂时实现。<br>
这段代码的主要用途是使用frida进行通用hook的时候需要将结果转化成不同的编码方式,以便查找。</p>
</blockquote>
<pre><code class="language-javascript">// Native ArrayBuffer to Base64
function base64ArrayBuffer(arrayBuffer) {
var base64    = ''
var encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

var bytes         = new Uint8Array(arrayBuffer)
var byteLength    = bytes.byteLength
var byteRemainder = byteLength % 3
var mainLength    = byteLength - byteRemainder

var a, b, c, d
var chunk

// Main loop deals with bytes in chunks of 3
for (var i = 0; i &lt; mainLength; i = i + 3) {
    // Combine the three bytes into a single integer
    chunk = (bytes &lt;&lt; 16) | (bytes &lt;&lt; 8) | bytes

    // Use bitmasks to extract 6-bit segments from the triplet
    a = (chunk &amp; 16515072) &gt;&gt; 18 // 16515072 = (2^6 - 1) &lt;&lt; 18
    b = (chunk &amp; 258048)   &gt;&gt; 12 // 258048   = (2^6 - 1) &lt;&lt; 12
    c = (chunk &amp; 4032)   &gt;&gt;6 // 4032   = (2^6 - 1) &lt;&lt; 6
    d = chunk &amp; 63               // 63       = 2^6 - 1

    // Convert the raw binary segments to the appropriate ASCII encoding
    base64 += encodings + encodings + encodings + encodings
}

// Deal with the remaining bytes and padding
if (byteRemainder == 1) {
    chunk = bytes

    a = (chunk &amp; 252) &gt;&gt; 2 // 252 = (2^6 - 1) &lt;&lt; 2

    // Set the 4 least significant bits to zero
    b = (chunk &amp; 3)   &lt;&lt; 4 // 3   = 2^2 - 1

    base64 += encodings + encodings + '=='
} else if (byteRemainder == 2) {
    chunk = (bytes &lt;&lt; 8) | bytes

    a = (chunk &amp; 64512) &gt;&gt; 10 // 64512 = (2^6 - 1) &lt;&lt; 10
    b = (chunk &amp; 1008)&gt;&gt;4 // 1008= (2^6 - 1) &lt;&lt; 4

    // Set the 2 least significant bits to zero
    c = (chunk &amp; 15)    &lt;&lt;2 // 15    = 2^4 - 1

    base64 += encodings + encodings + encodings + '='
}

return base64
}

(function() {
var base64EncodeChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
base64DecodeChars = new Array(( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), 62, ( - 1), ( - 1), ( - 1), 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, ( - 1), ( - 1), ( - 1), ( - 1), ( - 1));
this.base64encode = function(e) {
      var r, a, c, h, o, t;
      for (c = e.length, a = 0, r = ''; a &lt; c;) {
          if (h = 255 &amp; e.charCodeAt(a++), a == c) {
            r += base64EncodeChars.charAt(h &gt;&gt; 2),
            r += base64EncodeChars.charAt((3 &amp; h) &lt;&lt; 4),
            r += '==';
            break
          }
          if (o = e.charCodeAt(a++), a == c) {
            r += base64EncodeChars.charAt(h &gt;&gt; 2),
            r += base64EncodeChars.charAt((3 &amp; h) &lt;&lt; 4 | (240 &amp; o) &gt;&gt; 4),
            r += base64EncodeChars.charAt((15 &amp; o) &lt;&lt; 2),
            r += '=';
            break
          }
          t = e.charCodeAt(a++),
          r += base64EncodeChars.charAt(h &gt;&gt; 2),
          r += base64EncodeChars.charAt((3 &amp; h) &lt;&lt; 4 | (240 &amp; o) &gt;&gt; 4),
          r += base64EncodeChars.charAt((15 &amp; o) &lt;&lt; 2 | (192 &amp; t) &gt;&gt; 6),
          r += base64EncodeChars.charAt(63 &amp; t)
      }
      return r
}
this.base64decode = function(e) {
      var r, a, c, h, o, t, d;
      for (t = e.length, o = 0, d = ''; o &lt; t;) {
          do r = base64DecodeChars;
          while (o &lt; t &amp;&amp; r == -1);
          if (r == -1) break;
          do a = base64DecodeChars;
          while (o &lt; t &amp;&amp; a == -1);
          if (a == -1) break;
          d += String.fromCharCode(r &lt;&lt; 2 | (48 &amp; a) &gt;&gt; 4);
          do {
            if (c = 255 &amp; e.charCodeAt(o++), 61 == c) return d;
            c = base64DecodeChars
          } while ( o &lt; t &amp;&amp; c == - 1 );
          if (c == -1) break;
          d += String.fromCharCode((15 &amp; a) &lt;&lt; 4 | (60 &amp; c) &gt;&gt; 2);
          do {
            if (h = 255 &amp; e.charCodeAt(o++), 61 == h) return d;
            h = base64DecodeChars
          } while ( o &lt; t &amp;&amp; h == - 1 );
          if (h == -1) break;
          d += String.fromCharCode((3 &amp; c) &lt;&lt; 6 | h)
      }
      return d
}
this.hexToBase64 = function(str) {
      return base64encode(String.fromCharCode.apply(null, str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
}
this.Base64Tohex = function(str) {
      for (var i = 0,
      bin = base64decode(str.replace(/[ \r\n]+$/, "")), hex = []; i &lt; bin.length; ++i) {
          var tmp = bin.charCodeAt(i).toString(16);
          if (tmp.length === 1) tmp = "0" + tmp;
          hex = tmp;
      }
      return hex.join("");
}
}) ();
//hexToBase64 Base64Tohex base64decode base64encode

function bytesToString(bytes){
        return hexToString(bytesToHex(bytes));
}


function bytesToBase64(bytes){
        return base64ArrayBuffer(bytes);
}

// Convert a byte array to a hex string
function bytesToHex(bytes) {
    for (var hex = [], i = 0; i &lt; bytes.length; i++) {
      hex.push((bytes &gt;&gt;&gt; 4).toString(16));
      hex.push((bytes &amp; 0xF).toString(16));
    }
    return hex.join("");
}


function stringToBase64(str){
        return base64encode(str);
}

function stringToBytes(str){
return hexToBytes(stringToHex(str));
}

// Convert a ASCII string to a hex string
function stringToHex(str) {
    return str.split("").map(function(c) {
      return ("0" + c.charCodeAt(0).toString(16)).slice(-2);
    }).join("");
}

function hexToBytes(hex) {
    for (var bytes = [], c = 0; c &lt; hex.length; c += 2)
    bytes.push(parseInt(hex.substr(c, 2), 16));
    return bytes;
}

// Convert a hex string to a ASCII string
function hexToString(hexStr) {
    var hex = hexStr.toString();//force conversion
    var str = '';
    for (var i = 0; i &lt; hex.length; i += 2)
      str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
    return str;
}

function hexToBase64(hexStr){
return hexToBase64(hexStr);
}

function base64ToString(base64str){
return base64decode(base64str);
}

function base64ToHex(base64str){
return Base64Tohex(base64str);
}

function base64ToBytes(base64str){
//null
}
//bytesToString bytesToBase64 bytesToHex
//stringToBase64 stringToBytes stringToHex
//hexToBytes hexToString hexToBase64
//base64ToString base64ToHex
</code></pre>


</div>
<div id="MySignature" role="contentinfo">
    <center>
<hr>
<img src="https://files.cnblogs.com/files/ldhbetter/Free-Converter.com-2020_04_08_1040182162-8854055.bmp" width="270px" height="270px"><br>
<b>~欢迎各位师傅关注我的公众号哟~</b>
<hr>
</center><br><br>
来源:https://www.cnblogs.com/askta0/p/12800030.html
頁: [1]
查看完整版本: javascript进行hex、base64、bytes[]、string的互转