APIs

Show:
  1. /**
  2. RC4 encryption decryption library
  3. @class RC4
  4. @constructor
  5. @return {Object} instantiated RC4
  6. **/
  7. function RC4(i_passkey) {
  8. this.m_passkey = i_passkey;
  9. };
  10.  
  11. RC4.prototype = {
  12. constructor: RC4,
  13.  
  14. /**
  15. Arcfour_algorithm
  16. @method _arcfour_crypt
  17. @param {Object} i_data
  18. @param {Object} i_key
  19. @return {String} _arcfour_byte_string
  20. **/
  21. _arcfour_crypt: function (i_data, i_key) {
  22. var self = this;
  23. var STATE_LENGTH = 256;
  24. var i, i1, i2, x, y, temp;
  25. var secretKey = new Array(STATE_LENGTH);
  26. var keyLen = i_key.length;
  27. var dataLen = i_data.length;
  28. var output = new Array(dataLen);
  29.  
  30. i1 = 0;
  31. i2 = 0;
  32. x = 0;
  33. y = 0;
  34. for (i = 0; i < STATE_LENGTH; i++)
  35. secretKey[i] = i;
  36.  
  37.  
  38. for (i = 0; i < STATE_LENGTH; i++) {
  39. i2 = ((i_key.charCodeAt(i1) & 255) + secretKey[i] + i2) & 255;
  40. // swap
  41. temp = secretKey[i];
  42. secretKey[i] = secretKey[i2];
  43. secretKey[i2] = temp;
  44. i1 = (i1 + 1) % keyLen;
  45. }
  46.  
  47. for (i = 0; i < dataLen; i++) {
  48. x = (x + 1) & 255;
  49. y = (secretKey[x] + y) & 255;
  50. // swap
  51. temp = secretKey[x];
  52. secretKey[x] = secretKey[y];
  53. secretKey[y] = temp;
  54. // xor
  55. output[i] = i_data.charCodeAt(i) ^ secretKey[(secretKey[x] + secretKey[y]) & 255];
  56. }
  57. return self._arcfour_byte_string(output);
  58. },
  59.  
  60. /**
  61. Convert byte array into string.
  62. @method _arcfour_byte_string
  63. @param {Object} i_input
  64. @return {Object} output
  65. **/
  66. _arcfour_byte_string: function (i_input) {
  67. var self = this;
  68. var output = new String();
  69. for (var i = 0; i < i_input.length; i++) {
  70. output += String.fromCharCode(i_input[i]);
  71. }
  72. return output;
  73. },
  74.  
  75. /**
  76. Get the hex representation of an array of bytes
  77. @method _arcfour_hex_encode
  78. @param {Object} i_input
  79. @return {Object} output
  80. **/
  81. _arcfour_hex_encode: function (i_input) {
  82. var self = this;
  83. var hex_digits = "0123456789abcdef";
  84. var output = new String();
  85. for (var i = 0; i < i_input.length; i++) {
  86. output += hex_digits.charAt((i_input.charCodeAt(i) >>> 4) & 15);
  87. output += hex_digits.charAt(i_input.charCodeAt(i) & 15);
  88. }
  89. return output;
  90. },
  91.  
  92. /**
  93. Decode hex string
  94. @method _arcfour_hex_decode
  95. @param {Object} i_input
  96. @return {Object} output
  97. **/
  98. _arcfour_hex_decode: function (i_input) {
  99. var self = this;
  100. var left, right, index;
  101. var output = new Array(i_input.length / 2);
  102. for (var i = 0; i < i_input.length; i += 2) {
  103. left = i_input.charCodeAt(i);
  104. right = i_input.charCodeAt(i + 1);
  105. index = i / 2;
  106. if (left < 97) // left < 'a'
  107. output[index] = ((left - 48) << 4); // left - '0'
  108. else
  109. output[index] = ((left - 97 + 10) << 4);
  110. if (right < 97)
  111. output[index] += (right - 48);
  112. else
  113. output[index] += (right - 97 + 10);
  114. }
  115. return self._arcfour_byte_string(output);
  116. },
  117.  
  118. /**
  119. Generate a key in case we need a new one
  120. @method _genKey
  121. @return {Number} new ley
  122. **/
  123. _genKey: function () {
  124. var self = this;
  125. var key = new Array();
  126. var i = 0;
  127. var n;
  128. while (i < 16) {
  129. n = Math.ceil(Math.random() * 255);
  130. key[i] = n;
  131. i++;
  132. }
  133. return self._arcfour_hex_encode(self._arcfour_byte_string(key));
  134. },
  135.  
  136. /**
  137. Execute an encryption using pass
  138. @method doEncrypt
  139. @param {String} i_plaintext
  140. @return {String} encrypted dtaa
  141. **/
  142. doEncrypt: function (i_plaintext) {
  143. var self = this;
  144. if (i_plaintext.length > 0) {
  145. return self._arcfour_hex_encode(self._arcfour_crypt(i_plaintext, self._arcfour_hex_decode(self.m_passkey)));
  146. }
  147. },
  148.  
  149. /**
  150. Execute an decryption using pass on i_value
  151. @method doDecrypt
  152. @param {String} plaintext
  153. @return {String} decrypted i_value
  154. **/
  155. doDecrypt: function (i_value) {
  156. var self = this;
  157. var ciphertext = self._arcfour_hex_decode(i_value);
  158. if (ciphertext.length > 0) {
  159. return self._arcfour_crypt(ciphertext, self._arcfour_hex_decode(self.m_passkey));
  160. }
  161. }
  162. }
  163.  
  164.  
  165.  
  166.