-
Notifications
You must be signed in to change notification settings - Fork 21
/
System.Security.Cryptography.RC4.debug.js
127 lines (125 loc) · 4.66 KB
/
System.Security.Cryptography.RC4.debug.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//=============================================================================
// Jocys.com JavaScript.NET Classes (In C# Object Oriented Style)
// Created by Evaldas Jocys <[email protected]>
//=============================================================================
/// <reference path="System.debug.js" />
//=============================================================================
// Namespaces
//-----------------------------------------------------------------------------
// <PropertyGroup>
// <RootNamespace>System.Security.Cryptography</RootNamespace>
// <PropertyGroup>
//-----------------------------------------------------------------------------
System.Type.RegisterNamespace("System.Security.Cryptography");
//=============================================================================
System.Security.Cryptography.RC4 = function () {
/// <summary>
/// Represents RC4 symmetric cipher algorithm class.
/// Original Source:
/// http://farhadi.ir/downloads/rc4.js
/// RC4 symmetric cipher encryption/decryption
/// Copyright (c) 2006 by Ali Farhadi.
/// released under the terms of the Gnu Public License.
/// see the GPL for details.
/// Email: ali[at]farhadi[dot]ir
/// Website: http://farhadi.ir/
/// </summary>
//---------------------------------------------------------
// Public properties.
this.Name = "RC4";
//---------------------------------------------------------
this.Encrypt = function (key, data) {
return this.Cipher(key, data);
};
//---------------------------------------------------------
this.EncryptAsHex = function (key, data) {
return System.Convert.BytesToHexString(this.Cipher(key, data));
};
//---------------------------------------------------------
this.EncryptAsBase64 = function (key, data) {
var hash = this.ComputeHashAsBin(key, data);
return System.Convert.ToBase64String(this.Cipher(key, data), false);
};
//---------------------------------------------------------
this.Decrypt = function (key, data) {
return this.Cipher(key, data);
};
//---------------------------------------------------------
this.DecryptFromHex = function (key, hexData) {
var data = System.Convert.HexStringToBytes(hexData);
return this.Cipher(key, data);
};
//---------------------------------------------------------
this.DecryptFromBase64 = function (key, base64Data) {
var data = System.Convert.FromBase64String(base64Data, true);
return this.Cipher(key, data);
};
//---------------------------------------------------------
this.Test = function () {
/// <summary>
/// Perform a simple self-test to see if algorithm is working.
/// </summary>
var key = [0x6B, 0x65, 0x79]; // "key";
var data = [0x61, 0x62, 0x63]; // "abc";
var ciph = [0x6A, 0x0E, 0x57]; // "abc" encrypted with "key".
// Perform encryption decryption.
var encrypted = this.Encrypt(key, data);
var decrypted = this.Decrypt(key, encrypted);
isSuccess = true;
// Check values.
for (var i = 0; i < data.length; i++) {
if (ciph[i] !== encrypted[i] || data[i] !== decrypted[i]) {
isSuccess = false;
break;
}
}
return isSuccess;
};
//---------------------------------------------------------
this.Cipher = function (key, data) {
/// <summary>
/// Encrypt given plain text using the key with RC4 algorithm.
/// All parameters and return value are in binary format.
/// </summary>
/// <param name="key">Secret key bytes for encryption/decryption.</param>
/// <param name="data">Data bytes to be encrypted/decrypted.</param>
/// <returns>Encrypted/decrypted bytes.</returns>
// Convert string to bytes if neccessarty.
if (typeof key === "string") key = System.Text.Encoding.UTF8.GetBytes(key);
if (typeof data === "string") data = System.Text.Encoding.UTF8.GetBytes(data);
// Begin encryption/decrypion.
var s = [];
var kl = key.length;
var dl = data.length;
for (var i = 0; i < 256; i++) {
s[i] = i;
}
var j = 0;
var x;
for (i = 0; i < 256; i++) {
j = (j + s[i] + key[i % kl]) % 256;
x = s[i];
s[i] = s[j];
s[j] = x;
}
i = 0;
j = 0;
var cipher = new Array(dl);
for (var y = 0; y < dl; y++) {
i = (i + 1) % 256;
j = (j + s[i]) % 256;
x = s[i];
s[i] = s[j];
s[j] = x;
cipher[y] = data[y] ^ s[(s[i] + s[j]) % 256];
}
return cipher;
};
//---------------------------------------------------------
this.Initialize = function () {
};
this.Initialize.apply(this, arguments);
};
//==============================================================================
// END
//------------------------------------------------------------------------------