Created
November 24, 2008 13:24
-
-
Save ymnk/28462 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ARCFour{ | |
| private var x:Int = _ | |
| private var y:Int = _ | |
| private val state = new Array[Int](256) | |
| def decrypt(src:Array[Byte], off:Int, length:Int):Array[Byte]={ | |
| encrypt(src, off, length) | |
| } | |
| def decrypt(src:Array[Byte], soff:Int, | |
| dst:Array[Byte], doff:Int, length:Int){ | |
| encrypt(src, soff, dst, doff, length) | |
| } | |
| def encrypt(src:Array[Byte], off:Int, length:Int):Array[Byte]={ | |
| val dst = new Array[Byte](length) | |
| encrypt(src, off, dst , 0, length) | |
| dst | |
| } | |
| def encrypt(src:Array[Byte], soff:Int, | |
| dst:Array[Byte], doff:Int, length:Int){ | |
| var sx, sy=0 | |
| var i = 0 | |
| while(i < length){ | |
| x = (x+1)&0xff | |
| sx = state(x) | |
| y = (y+sx)&0xff | |
| sy = state(y) | |
| state(y) = sx | |
| state(x) = sy | |
| dst(i+doff) = ((src(i+soff)&0xff)^(state((sx+sy)&0xff))).asInstanceOf[Byte] | |
| i += 1 | |
| } | |
| } | |
| def init(key:Array[Byte]){ | |
| x=0 | |
| y=0 | |
| (state /: (0 until 256)){(b, i) => b(i)=i; b} | |
| val keylen = key.length | |
| var kindex, sindex = 0 | |
| var i = 0 | |
| while(i < state.length){ | |
| sindex = (sindex+(key(kindex)&0xff)+state(i))&0xff | |
| state(i) = state(sindex) match { | |
| case u => state(sindex) = state(i); u | |
| } | |
| i += 1 | |
| kindex += 1 | |
| if(kindex >= keylen){ | |
| kindex = 0 | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment