首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

rc4加密解密

Posted on 2014-03-21 17:29  达奇  阅读(595)  评论(0编辑  收藏  举报
function rc4(data, key) {
    var seq = Array(256);
    var das = Array(data.length);
    for (var i = 0; i < 256; i++) {
        seq[i] = i;
        var j = (j + seq[i] + key.charCodeAt(i % key.length)) % 256;
        var temp = seq[i];
        seq[i] = seq[j];
        seq[j] = temp
    }
    for (var i = 0; i < data.length; i++) {
        das[i] = data.charCodeAt(i)
    }
    for (var x = 0; x < das.length; x++) {
        var i = (i + 1) % 256;
        var j = (j + seq[i]) % 256;
        var temp = seq[i];
        seq[i] = seq[j];
        seq[j] = temp;
        var k = (seq[i] + (seq[j] % 256)) % 256;
        das[x] = String.fromCharCode(das[x] ^ seq[k])
    }
    return das.join('')
}