1

I have tried a couple of ways but it does't work.

Now I have 2 problems that I cannot figure out.

  1. I can't figure out how to encode a string in JavaScript to Unicode and decode using $value = iconv('UCS-2LE', 'UTF-8', $_GET["value"]); in PHP

  2. I encrypted a string in RC4 using JavaScript, encoded in base64, then transferred it to PHP. The result was garbled. I don't know why!

Where should I start?

4

1 回答 1

1

我已经解决了这个问题,我认为这是一个好方法,我想分享。Javascript 对象在 USC-2 中编码。但是 RC4 是以字节为单位工作的。首先,我使用
var plainValueArray = TextEncoder("utf-16").encode(value); 将字符串转换为字节数组;var keyArray=TextEncoder("utf8").encode(RC4Key); 其次,RC4 如下 var RC4={ crypt:function(rawBytes,keyBytes){

    var i;
    var sBuffer=new ArrayBuffer(256);
    var kBuffer=new ArrayBuffer(256);
    var cipherBuffer=new ArrayBuffer(rawBytes.length);
    var s=new Uint8Array(sBuffer);//box[]
    var k=new Uint8Array(kBuffer);//key[]
    var cipherBytes=new Uint8Array(cipherBuffer);
    for(i=0;i<rawBytes.length;i++)
    {
        cipherBytes[i]=0x00;
    }
    for(i=0;i<256;i++)
    {
        k[i]=keyBytes[i%keyBytes.length];
        s[i]=i;
    }
    var j=0;
    for(i=0;i<256;i++)
    {
        j=(j+s[i]+k[i])%256;
        x=s[i];
        s[i]=s[j];
        s[j]=x;
    }
    i=0;
    j=0;
    for(var y=0;y<rawBytes.length;y++)
    {
        i=(i+1)%256;
        j=(j+s[i])%256;
        x=s[i];
        s[i]=s[j];
        s[j]=x;
        key=s[(s[i]+s[j])%256]
        cipherBytes[y]=key^rawBytes[y];
    }
    return cipherBytes;

}

};

于 2013-07-05T09:15:54.157 回答