Posted By

uglycom on 07/01/10


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

breign


AS RC4


 / Published in: ActionScript
 

  1. /**
  2. * Encrypts and decrypts an alleged RC4 hash.
  3. * @author Mika Palmu
  4. * @version 1.0
  5. *
  6. * Orginal Flash port by:
  7. * Gabor Penoff - http://www.fns.hu
  8. * Email: [email protected]
  9. */
  10.  
  11. class com.meychi.ascrypt.RC4 {
  12.  
  13. /**
  14. * Variables
  15. * @exclude
  16. */
  17. private static var sbox:Array = new Array(255);
  18. private static var mykey:Array = new Array(255);
  19.  
  20. /**
  21. * Encrypts a string with the specified key.
  22. */
  23. public static function encrypt(src:String, key:String):String {
  24. var mtxt:Array = strToChars(src);
  25. var mkey:Array = strToChars(key);
  26. var result:Array = calculate(mtxt, mkey);
  27. return charsToHex(result);
  28. }
  29.  
  30. /**
  31. * Decrypts a string with the specified key.
  32. */
  33. public static function decrypt(src:String, key:String):String {
  34. var mtxt:Array = hexToChars(src);
  35. var mkey:Array = strToChars(key);
  36. var result:Array = calculate(mtxt, mkey);
  37. return charsToStr(result);
  38. }
  39.  
  40. /**
  41. * Private methods.
  42. */
  43. private static function initialize(pwd:Array) {
  44. var b:Number = 0;
  45. var tempSwap:Number;
  46. var intLength:Number = pwd.length;
  47. for (var a:Number = 0; a <= 255; a++) {
  48. mykey[a] = pwd[(a%intLength)];
  49. sbox[a] = a;
  50. }
  51. for (var a:Number=0; a<=255; a++) {
  52. b = (b+sbox[a]+mykey[a]) % 256;
  53. tempSwap = sbox[a];
  54. sbox[a] = sbox[b];
  55. sbox[b] = tempSwap;
  56. }
  57. }
  58. private static function calculate(plaintxt:Array, psw:Array):Array {
  59. initialize(psw);
  60. var i:Number = 0; var j:Number = 0;
  61. var cipher:Array = new Array();
  62. var k:Number, temp:Number, cipherby:Number;
  63. for (var a:Number = 0; a<plaintxt.length; a++) {
  64. i = (i+1) % 256;
  65. j = (j+sbox[i])%256;
  66. temp = sbox[i];
  67. sbox[i] = sbox[j];
  68. sbox[j] = temp;
  69. var idx:Number = (sbox[i]+sbox[j]) % 256;
  70. k = sbox[idx];
  71. cipherby = plaintxt[a]^k;
  72. cipher.push(cipherby);
  73. }
  74. return cipher;
  75. }
  76. private static function charsToHex(chars:Array):String {
  77. var result:String = new String("");
  78. var hexes:Array = new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
  79. for (var i:Number = 0; i<chars.length; i++) {
  80. result += hexes[chars[i] >> 4] + hexes[chars[i] & 0xf];
  81. }
  82. return result;
  83. }
  84. private static function hexToChars(hex:String):Array {
  85. var codes:Array = new Array();
  86. for (var i:Number = (hex.substr(0, 2) == "0x") ? 2 : 0; i<hex.length; i+=2) {
  87. codes.push(parseInt(hex.substr(i, 2), 16));
  88. }
  89. return codes;
  90. }
  91. private static function charsToStr(chars:Array):String {
  92. var result:String = new String("");
  93. for (var i:Number = 0; i<chars.length; i++) {
  94. result += String.fromCharCode(chars[i]);
  95. }
  96. return result;
  97. }
  98. private static function strToChars(str:String):Array {
  99. var codes:Array = new Array();
  100. for (var i:Number = 0; i<str.length; i++) {
  101. codes.push(str.charCodeAt(i));
  102. }
  103. return codes;
  104. }
  105.  
  106. }

Report this snippet  

You need to login to post a comment.