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

Report this snippet  

You need to login to post a comment.