ByteArrayをRC4暗号化する

CriptRC4.as
package {
import flash.utils.ByteArray;

public final class CriptRC4 {
private var keyCodes :Vector.
= new Vector.(0, false);
private const KEYLENGTH :uint = 255;

public function CriptRC4(key:String) {
keyCodes = new Vector.(KEYLENGTH +1, true);
const bufCodes:Vector. = new Vector.(KEYLENGTH +1, true);

const length:uint = key.length;
var i:uint;
for (i = 0; i <= KEYLENGTH; ++i) {
bufCodes[i] = uint(key.charCodeAt(i % length));
keyCodes[i] = i;
}
var j:uint;
for (j = i = 0; i <= KEYLENGTH; ++i) {
j = (j + keyCodes[i] + bufCodes[i]) & KEYLENGTH;

keyCodes[i] ^= keyCodes[j];
keyCodes[j] ^= keyCodes[i];
keyCodes[i] ^= keyCodes[j];
}
}

public function cript(data:ByteArray, len:uint=0):ByteArray {
data.position = 0;
const length:uint = data.length;
if ((len == 0) || (len > length)) len = length;

const buf:Vector. = keyCodes.concat();
const baRet:ByteArray = new ByteArray();
// baRet.position = 0;

var i:uint;
var j:uint;
var k:uint;
for (i = j = k = 0; k i = (i + 1) & KEYLENGTH;
j = (j + buf[i]) & KEYLENGTH;

buf[i] ^= buf[j];
buf[j] ^= buf[i];
buf[i] ^= buf[j];

const t:uint = (buf[i] + buf[j]) & KEYLENGTH;
baRet.writeByte(data.readByte() ^ buf[t]);
}

if (len < length) baRet.writeBytes(data, data.position);

return baRet;
}
}
}
スポンサーサイト
プロフィール

ふぁみべぇ

Author:ふぁみべぇ
 →メアド(四角を@に)

カテゴリ
リンク
検索フォーム
最新記事
月別アーカイブ