Comparing version 1.0.0 to 1.1.0
@@ -0,1 +1,8 @@ | ||
v1.1.0 / 2014-06-24 | ||
================== | ||
* New method `RC4A` | ||
* New method `VMPC` | ||
* New method `RC4+` | ||
v1.0.0 / 2014-06-23 | ||
@@ -2,0 +9,0 @@ ================== |
243
index.js
@@ -102,11 +102,9 @@ "use strict"; | ||
/** | ||
* string code | ||
* generate ksa | ||
* | ||
* @function codeString | ||
* @param {String} str - data | ||
* @return {String} | ||
* @function ksa | ||
* @return {Array} | ||
*/ | ||
rc4.prototype.codeString = function(str) { | ||
rc4.prototype.ksa = function(key) { | ||
var res = ''; | ||
var j = 0; | ||
@@ -120,3 +118,16 @@ var s = sbox(); | ||
} | ||
i = j = 0; | ||
return s; | ||
}; | ||
/** | ||
* RC4 string code | ||
* | ||
* @function codeString | ||
* @param {String} str - data | ||
* @return {String} | ||
*/ | ||
rc4.prototype.codeString = function(str) { | ||
var res = ''; | ||
var i = 0, j = 0; | ||
var s = this.ksa(); | ||
for (var y = 0, l = str.length; y < l; y++) { | ||
@@ -131,3 +142,3 @@ i = (i + 1) % 256; | ||
/** | ||
* byte code | ||
* RC4 byte code | ||
* | ||
@@ -141,11 +152,4 @@ * @function codeByte | ||
var res = []; | ||
var j = 0; | ||
var s = sbox(); | ||
var key = this.key; | ||
var len = this.len; | ||
for (var i = 0; i < 256; i++) { | ||
j = (j + s[i] + key[i % len]) % 256; | ||
s[j] = [s[i],s[i] = s[j]][0]; | ||
} | ||
i = j = 0; | ||
var i = 0, j = 0; | ||
var s = this.ksa(); | ||
for (var y = 0, l = byt.length; y < l; y++) { | ||
@@ -155,3 +159,3 @@ i = (i + 1) % 256; | ||
s[j] = [s[i],s[i] = s[j]][0]; | ||
res[y] = (byt[y] ^ s[(s[i] + s[j]) % 256]); | ||
res[y] = byt[y] ^ s[(s[i] + s[j]) % 256]; | ||
} | ||
@@ -161,3 +165,3 @@ return res; | ||
/** | ||
* mixed code. Alias for codeString or codeByte | ||
* RC4 mixed code. Alias for codeString or codeByte | ||
* | ||
@@ -179,1 +183,202 @@ * @function code | ||
}; | ||
/** | ||
* RC4A string code | ||
* | ||
* @function codeStringRC4A | ||
* @param {String} str - data | ||
* @return {String} | ||
*/ | ||
rc4.prototype.codeStringRC4A = function(str) { | ||
var res = ''; | ||
var i = 0, j1 = 0, j2 = 0; | ||
var s1 = this.ksa(); | ||
var s2 = s1.slice(); | ||
for (var y = 0, l = str.length; y < l; y++) { | ||
i = (i + 1) % 256; | ||
j1 = (j1 + s1[i]) % 256; | ||
s1[j1] = [s1[i],s1[i] = s1[j1]][0]; | ||
res += String.fromCharCode(str.charCodeAt(y) | ||
^ s2[(s1[i] + s1[j1]) % 256]); | ||
y++; | ||
j2 = (j2 + s2[i]) % 256; | ||
s2[j2] = [s2[i],s2[i] = s2[j2]][0]; | ||
res += String.fromCharCode(str.charCodeAt(y) | ||
^ s1[(s2[i] + s2[j2]) % 256]); | ||
} | ||
return res; | ||
}; | ||
/** | ||
* RC4A byte code | ||
* | ||
* @function codeByteRC4A | ||
* @param {Array} byt - data | ||
* @return {Array} | ||
*/ | ||
rc4.prototype.codeByteRC4A = function(byt) { | ||
var res = []; | ||
var i = 0, j1 = 0, j2 = 0; | ||
var s1 = this.ksa(); | ||
var s2 = s1.slice(); | ||
for (var y = 0, l = byt.length; y < l; y++) { | ||
i = (i + 1) % 256; | ||
j1 = (j1 + s1[i]) % 256; | ||
s1[j1] = [s1[i],s1[i] = s1[j1]][0]; | ||
res[y] = byt[y] ^ s2[(s1[i] + s1[j1]) % 256]; | ||
y++; | ||
j2 = (j2 + s2[i]) % 256; | ||
s2[j2] = [s2[i],s2[i] = s2[j2]][0]; | ||
res[y] = byt[y] ^ s1[(s2[i] + s2[j1]) % 256]; | ||
} | ||
return res; | ||
}; | ||
/** | ||
* RC4A mixed code. Alias for codeString or codeByte | ||
* | ||
* @function codeRC4A | ||
* @param {String|Array} boh - data | ||
* @return {String|Array} | ||
*/ | ||
rc4.prototype.codeRC4A = function(boh) { | ||
if (typeof (boh) == 'string') { | ||
return this.codeStringVMPC(boh); | ||
} else if (Array.isArray(boh)) { | ||
return this.codeByteVMPC(boh); | ||
} else { | ||
throw new Error('Invalid data'); | ||
} | ||
return; | ||
}; | ||
/** | ||
* VMPC string code | ||
* | ||
* @function codeStringVMPC | ||
* @param {String} str - data | ||
* @return {String} | ||
*/ | ||
rc4.prototype.codeStringVMPC = function(str) { | ||
var res = ''; | ||
var i = 0, j = 0; | ||
var s = this.ksa(); | ||
var a = null, b = null; | ||
for (var y = 0, l = str.length; y < l; y++) { | ||
a = s[i]; | ||
j = s[(j + a) % 256]; | ||
b = s[j]; | ||
res += String.fromCharCode(str.charCodeAt(y) ^ s[s[b] + 1]); | ||
s[j] = [a,s[i] = b][0]; | ||
i = (i + 1) % 256; | ||
} | ||
return res; | ||
}; | ||
/** | ||
* VMPC byte code | ||
* | ||
* @function codeByteVMPC | ||
* @param {Array} byt - data | ||
* @return {Array} | ||
*/ | ||
rc4.prototype.codeByteVMPC = function(byt) { | ||
var res = []; | ||
var i = 0, j = 0; | ||
var s = this.ksa(); | ||
var a = null, b = null; | ||
for (var y = 0, l = byt.length; y < l; y++) { | ||
a = s[i]; | ||
j = s[(j + a) % 256]; | ||
b = s[j]; | ||
res[y] = byt[y] ^ s[s[b] + 1]; | ||
s[j] = [a,s[i] = b][0]; | ||
i = (i + 1) % 256; | ||
} | ||
return res; | ||
}; | ||
/** | ||
* VMPC mixed code. Alias for codeString or codeByte | ||
* | ||
* @function codeVMPC | ||
* @param {String|Array} boh - data | ||
* @return {String|Array} | ||
*/ | ||
rc4.prototype.codeVMPC = function(boh) { | ||
if (typeof (boh) == 'string') { | ||
return this.codeStringVMPC(boh); | ||
} else if (Array.isArray(boh)) { | ||
return this.codeByteVMPC(boh); | ||
} else { | ||
throw new Error('Invalid data'); | ||
} | ||
return; | ||
}; | ||
/** | ||
* RC4p string code | ||
* | ||
* @function codeStringRC4p | ||
* @param {String} str - data | ||
* @return {String} | ||
*/ | ||
rc4.prototype.codeStringRC4p = function(str) { | ||
var res = ''; | ||
var i = 0, j = 0; | ||
var s = this.ksa(); | ||
var a = null, b = null, c = null; | ||
for (var y = 0, l = str.length; y < l; y++) { | ||
i = (i + 1) % 256; | ||
a = s[i]; | ||
j = s[(j + a) % 256]; | ||
b = s[j]; | ||
s[j] = [a,s[i] = b][0]; | ||
c = (s[i << 5 ^ j >> 3] + s[j << 5 ^ i >> 3]) % 256; | ||
res += String.fromCharCode(str.charCodeAt(y) ^ (s[a + b] + s[c ^ 0xAA]) | ||
^ s[j + b]); | ||
} | ||
return res; | ||
}; | ||
/** | ||
* RC4p byte code | ||
* | ||
* @function codeByteRC4p | ||
* @param {Array} byt - data | ||
* @return {Array} | ||
*/ | ||
rc4.prototype.codeByteRC4p = function(byt) { | ||
var res = []; | ||
var i = 0, j = 0; | ||
var s = this.ksa(); | ||
var a = null, b = null, c = null; | ||
for (var y = 0, l = byt.length; y < l; y++) { | ||
i = (i + 1) % 256; | ||
a = s[i]; | ||
j = s[(j + a) % 256]; | ||
b = s[j]; | ||
s[j] = [a,s[i] = b][0]; | ||
c = (s[i << 5 ^ j >> 3] + s[j << 5 ^ i >> 3]) % 256; | ||
res[y] = byt[y] ^ (s[a + b] + s[c ^ 0xAA]) ^ s[j + b]; | ||
} | ||
return res; | ||
}; | ||
/** | ||
* RC4p mixed code. Alias for codeString or codeByte | ||
* | ||
* @function codeRC4p | ||
* @param {String|Array} boh - data | ||
* @return {String|Array} | ||
*/ | ||
rc4.prototype.codeRC4p = function(boh) { | ||
if (typeof (boh) == 'string') { | ||
return this.codeStringVMPC(boh); | ||
} else if (Array.isArray(boh)) { | ||
return this.codeByteVMPC(boh); | ||
} else { | ||
throw new Error('Invalid data'); | ||
} | ||
return; | ||
}; |
{ | ||
"name": "arc4", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "rc4 stream cipher", | ||
@@ -26,2 +26,3 @@ "main": "index.js", | ||
"rc4", | ||
"stream", | ||
"cipher" | ||
@@ -28,0 +29,0 @@ ], |
#arc4 [![Build Status](https://travis-ci.org/hex7c0/arc4.svg?branch=master)](https://travis-ci.org/hex7c0/arc4) [![NPM version](https://badge.fury.io/js/arc4.svg)](http://badge.fury.io/js/arc4) | ||
[RC4](http://en.wikipedia.org/wiki/RC4) stream cipher | ||
[RC4](https://en.wikipedia.org/wiki/RC4) stream cipher | ||
my original [python code](https://github.com/hex7c0/EncryptoPy/blob/master/modules/rc/rc4.py) | ||
## Installation | ||
@@ -28,2 +30,7 @@ | ||
change your key (warning) | ||
```js | ||
rc4.change('foo'); | ||
``` | ||
encode string data | ||
@@ -44,11 +51,20 @@ ```js | ||
change your key (warning) | ||
same methods with `RC4A` postifx for [RC4A](https://en.wikipedia.org/wiki/RC4#RC4A) | ||
```js | ||
rc4.change('foo'); | ||
rc4.codeRC4A('string or byte'); | ||
``` | ||
same methods with `VMPC` postifx for [VMPC](https://en.wikipedia.org/wiki/RC4#VMPC) | ||
```js | ||
rc4.codeVMPC('string or byte'); | ||
``` | ||
same methods with `RC4p` postifx for [RC4+](https://en.wikipedia.org/wiki/RC4#RC4.2B) | ||
```js | ||
rc4.codeRC4p('string or byte'); | ||
``` | ||
### rc4(param) | ||
- `param` - **String|Array** Your key *(default "throw Error")* | ||
- `param` - **String | Array** Your key *(default "throw Error")* | ||
@@ -55,0 +71,0 @@ #### Examples |
46819
357
77