我已经解决了这个问题,我认为这是一个好方法,我想分享。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;
}
};