同位转移的JavaScript base64编码 [英] base64 encoding in javascript with bit shifting

查看:314
本文介绍了同位转移的JavaScript base64编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下几点去code / EN code程序。但是,编码工作不正常(应打印CMlaKA而不是ClaKA控制台日志)。我认为这个问题是同位转移,但我不能告诉在哪里。

下面是一个的jsfiddle解释

https://jsfiddle.net/4yfrLv9y/16/

下面是code(例程在底部运行)

  VAR consoleLine =< p类= \\控制台线\\>< / P>中;控制台= {
    日志:功能(文字){
        $(#控制台登录)追加($(consoleLine)的.html(文本));
    }
};VAR的Base64 = {
        _keyStr:.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 + =,    EN code:功能(输入){
        VAR输出= []
            CHR1,CHR 2,CHR 3,ENC1,ENC2,enc3,enc4,
            I = 0;
        而(I< input.length){
            CHR1 =输入[我++];
            CHR2 =输入[我++];
            CHR3 =输入[我++];            ENC1 = CHR1&安培; 0x3F的;
            ENC2 =(CHR1>&→6)| ((CHR2&放大器;为0x3C)下; 2);
            enc3 =(CHR2>→4)| ((CHR3&放大器; 0x3中)下; 4;);
            enc4 = CHR3>> 2;            如果(isNaN(CHR2)){
                enc3 = enc4 = 64;
            }否则如果(isNaN(CHR 3)){
                enc4 = 64;
            }            output.push([this._keyStr.charAt(ENC1)
                         this._keyStr.charAt(ENC2)
                         this._keyStr.charAt(enc3)
                         this._keyStr.charAt(enc4)加入(''));
        }        返回output.join('');
    },    德codeAsArray:功能(B){
        变种D = this.de code(B)
            一个= [],
            C;
                //警报(德codeD的base64:+ D);
        为(C = 0;℃下d.length; C ++){
            一个[C] = d.char $ C $猫(三)
        }
                //警报(返回);
        返回
    },    德code:功能(输入){
        VAR输出=;
        变种CHR1,CHR2,CHR3 =;
        VAR ENC1,ENC2,enc3,enc4 =;
        变种I = 0;        做{
            ENC1 = this._keyStr.indexOf(input.charAt(我+ +));
            ENC2 = this._keyStr.indexOf(input.charAt(我+ +));
            enc3 = this._keyStr.indexOf(input.charAt(我+ +));
            enc4 = this._keyStr.indexOf(input.charAt(我+ +));            CHR1 =(ENC1 |((ENC2和3)所述; 6;));
            CHR2 =(ENC2>&→2)| ((enc3&放大器;为0x0F)所述; 4;);
            CHR3 =(enc3>→4)| (enc4&下; 2);            输出=输出+ String.fromChar code(CHR1);
            如果(enc3!= 64){
                输出=输出+ String.fromChar code(CHR 2);
                        }
            如果(enc4!= 64){
                输出=输出+ String.fromChar code(CHR 3);
            }
            CHR1 = CHR2 = CHR3 =;
            ENC1 = ENC2 = enc3 = enc4 =;
        }而(I< input.length);        返回(输出);
    }};basede code();功能basede code(){
//将转换'CMlaKa到CcnK通过的base64
    VAR德codeD =CMlaKA
    // 67 99 110 75 0 0 - 这是一个字节数组,或CcnK的ArrayBuffer
    德codeD = Base64.de code(德codeD)
    的console.log(德codeD);
}baseen code();功能baseen code(){
    变种EN codeD = [67,99,110,75]; CcnK的//字节数组
    的console.log(Base64.en code(EN codeD)+'----应该是CMlaKA不ClaKA == - 为什么会不同?');
}


解决方案

有一对夫妇在code错误,他们不仅涉及EN code方法,但德$ C $一个系统一台为好。

首先,你用的是不好的密钥字符串。 Accoring维基百科维基百科 - Base64编码'A'等于'0',而不是'。在你的榜样。

这将prevent您检查,对公共网站的有效性您code。

这是标准密钥字符串:

  _keyStr:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 + / =,

在'='到底是一个用来填充,不应该转换过程中直接​​使用

然后,你有一个问题,您的字节偏移code。
你在错误的方向计算出的base64值。你应该看看维基百科的链接,看看哪些字节应被视为每个BASE64值。

下面是去codeR固定片段:

  CHR1 = ENC1<< 2 | ((ENC2&放大器;将0xC0)GT;&→6);
CHR2 =((ENC2&放大器;为0x0F)所述; 4;)| ((enc3&放大器;为0x3C)GT;&→2);
CHR3 =((enc3&放大器; 0×03)所述; 6;)| enc4;

下面是为EN codeR固定片段:

  ENC1 =(CHR1&安培; 0xFC有)GT;> 2;
ENC2 =((CHR1&放大器; 0×03)所述; 4;)| ((CHR2&放大器; 0XF0)GT;→4);
enc3 =((CHR2&放大器;为0x0F)下; 2)| ((CHR3&放大器;将0xC0)GT;&→6);
enc4 = CHR3&安培; 0x3F的;

此外,必须调整输出值(在连接codeR),因为他们是在无限的容器,所以:

  ENC1 = ENC1&安培; 0x3F的;
ENC2 = ENC2&安培; 0x3F的;
enc3 = enc3&安培; 0x3F的;

假设你所作的所有这些变化,如果输入到去codeR是CMlaKA时,输出为[12,217,155,44,16],然后将连接codeR将返回正确的回答。

I have the following decode/encode routine. However, the encoding is not working properly (it should be printing "CMlaKA" not "ClaKA" to the console log). I think the problem is with the bit shifting, but I cant tell where.

Here is a jsfiddle to explain

https://jsfiddle.net/4yfrLv9y/16/

Here is the code (routine is run at the bottom)

var consoleLine = "<p class=\"console-line\"></p>";

console = {
    log: function (text) {
        $("#console-log").append($(consoleLine).html(text));
    }
};

var Base64 = {
        _keyStr: ".ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+=",

    encode : function (input) {
        var output = [],
            chr1, chr2, chr3, enc1, enc2, enc3, enc4,
            i = 0;
        while (i < input.length) {
            chr1 = input[i++];
            chr2 = input[i++];
            chr3 = input[i++];

            enc1 = chr1 & 0x3f;
            enc2 = (chr1 >> 6) | ((chr2 & 0x3c) << 2);
            enc3 = (chr2 >> 4) | ((chr3 & 0x3) << 4);
            enc4 = chr3 >> 2;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output.push([this._keyStr.charAt(enc1),
                         this._keyStr.charAt(enc2),
                         this._keyStr.charAt(enc3),
                         this._keyStr.charAt(enc4)].join(''));
        }

        return output.join('');
    },

    decodeAsArray: function (b) {
        var d = this.decode(b),
            a = [],
            c;
                //alert("decoded base64:" + d);
        for (c = 0; c < d.length; c++) {
            a[c] = d.charCodeAt(c)
        }
                //alert("returning a");
        return a
    },

    decode: function( input ) {
        var output = "";
        var chr1, chr2, chr3 = "";
        var enc1, enc2, enc3, enc4 = "";
        var i = 0;

        do {
            enc1 = this._keyStr.indexOf(input.charAt(i++)) ;
            enc2 = this._keyStr.indexOf(input.charAt(i++)) ;
            enc3 = this._keyStr.indexOf(input.charAt(i++)) ;
            enc4 = this._keyStr.indexOf(input.charAt(i++)) ;

            chr1 = (enc1 | ((enc2 & 3) << 6));
            chr2 = (enc2 >> 2) | ((enc3 & 0x0F) << 4);
            chr3 = (enc3 >> 4) | (enc4 << 2);

            output = output + String.fromCharCode(chr1);
            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
                        }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }
            chr1 = chr2 = chr3 = "";
            enc1 = enc2 = enc3 = enc4 = "";
        } while (i < input.length);

        return (output);
    }

};

basedecode();

function basedecode(){
//Converts 'CMlaKa to CcnK by base64'
    var decoded = "CMlaKA"
    //67 99 110 75 0 0  - This is the Byte Array, or ArrayBuffer of CcnK
    decoded = Base64.decode(decoded)
    console.log(decoded);
}

baseencode();

function baseencode(){
    var encoded = [67,99,110,75];// byte array of CcnK
    console.log(Base64.encode(encoded) + ' ---- Should be CMlaKA not ClaKA== - why is it different?'); 
}

解决方案

There are a couple of mistakes in your code and they involve not only the encode method, but the decode one as well.

First of all, you are using a bad key string. Accoring to Wikipedia Wikipedia - Base64 'A' equals '0', not '.' as in your example.

This will prevent you from checking your code against public websites for validity.

This is the "standard" key string:

_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

The '=' in the end is for padding and should not be used directly during conversion

Then, you got a problem with your byte shifting code. You calculate the base64 values in the wrong direction. You should look at the Wikipedia link to see which bytes should be considered as each base64 value.

Here is a fixed snippet for the decoder:

chr1 = enc1 << 2 | ((enc2 & 0xc0) >> 6);
chr2 = ((enc2 & 0x0f) << 4) | ((enc3 & 0x3c) >> 2);
chr3 = ((enc3 & 0x03) << 6) | enc4;

Here is a fixed snippet for the encoder:

enc1 =  (chr1 & 0xfc) >> 2;
enc2 = ((chr1 & 0x03) << 4) | ((chr2 & 0xf0) >> 4);
enc3 = ((chr2 & 0x0f) << 2) | ((chr3 & 0xc0) >> 6);
enc4 =   chr3 & 0x3f;

Moreover, you must trim the output values (in the encoder) because they are in unlimited containers, so:

enc1 = enc1 & 0x3f;
enc2 = enc2 & 0x3f;
enc3 = enc3 & 0x3f;

Assuming you made all these changes, if the input to the decoder is "CMlaKA", the output is [12,217,155,44,16] and then the encoder will return the correct answer.

这篇关于同位转移的JavaScript base64编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆