Base64.as 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package
  2. {
  3. import flash.utils.ByteArray;
  4. import flash.events.Event;
  5. public class Base64
  6. {
  7. public static const version:String = "2.0.0";
  8. public static function encode(data:String):String
  9. {
  10. var bytes:ByteArray = new ByteArray();
  11. bytes.writeUTFBytes(data);
  12. return encodeByteArray(bytes);
  13. }
  14. private static const BASE64_CHARARR:Vector.<int> = new <int>[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,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,48,49,50,51,52,53,54,55,56,57,43,47,61]; ;
  15. public static function encodeByteArray(data:ByteArray):String
  16. {
  17. var output:ByteArray = new ByteArray();
  18. var opbuf0:int;
  19. var opbuf1:int;
  20. var opbuf2:int;
  21. var opbuf3:int;
  22. var dblen:int = 0;
  23. var resultlen:uint = data.length;
  24. var position:uint = 0;
  25. var oplen:uint = 0;
  26. while (resultlen > 0)
  27. {
  28. if (resultlen >= 3)
  29. {
  30. dblen = 3;
  31. resultlen -= 3;
  32. }
  33. else
  34. {
  35. dblen = resultlen;
  36. resultlen = 0;
  37. }
  38. opbuf0 = (data[position] & 0xfc) >> 2;
  39. if (dblen>=1)
  40. {
  41. opbuf1 = ((data[position] & 0x03) << 4) | ((data[position + 1]) >> 4);
  42. }
  43. else
  44. {
  45. opbuf1 = 64;
  46. }
  47. if (dblen>=2)
  48. {
  49. opbuf2 = ((data[position + 1] & 0x0f) << 2) | ((data[position + 2]) >> 6);
  50. }
  51. else
  52. {
  53. opbuf2 = 64;
  54. }
  55. if (dblen>=3)
  56. {
  57. opbuf3 = data[position + 2] & 0x3f;
  58. }
  59. else
  60. {
  61. opbuf3 = 64;
  62. }
  63. output[oplen] = BASE64_CHARARR[opbuf0];
  64. output[oplen + 1] = BASE64_CHARARR[opbuf1];
  65. output[oplen + 2] = BASE64_CHARARR[opbuf2];
  66. output[oplen + 3] = BASE64_CHARARR[opbuf3];
  67. position += 3;
  68. oplen += 4;
  69. }
  70. return output.readUTFBytes(oplen);
  71. }
  72. public static function decode(data:String)
  73. {
  74. var bytes:ByteArray = decodeToByteArray(data);
  75. return bytes.readUTFBytes(bytes.length);;
  76. }
  77. public static function decodeToByteArray(_data:String)
  78. {
  79. var output:ByteArray = new ByteArray();
  80. var data:ByteArray = new ByteArray();
  81. data.writeUTFBytes(_data);
  82. var databuf0:int;
  83. var databuf1:int;
  84. var databuf2:int;
  85. var databuf3:int;
  86. var resultlen:uint = data.length;
  87. var position:uint = 0;
  88. var oplen:uint = 0;
  89. while (resultlen>0)
  90. {
  91. databuf0 = find(data[position]);
  92. if (resultlen >= 1)
  93. {
  94. databuf1=find(data[int(position+ 1)]);
  95. }
  96. if (resultlen >= 2)
  97. {
  98. databuf2=find(data[int(position+ 2)]);
  99. }
  100. if (resultlen >= 3)
  101. {
  102. databuf3=find(data[int(position+3)]);
  103. }
  104. if (databuf1 != 64)
  105. {
  106. output[oplen] = (databuf0 << 2) + ((databuf1 & 0x30) >> 4);
  107. if (databuf2 != 64)
  108. {
  109. output[oplen + 1] = ((databuf1 & 0x0f) << 4) + ((databuf2 & 0x3c) >> 2);
  110. if (databuf3 != 64)
  111. {
  112. output[oplen+ 2] = ((databuf2 & 0x03) << 6) + databuf3;
  113. }
  114. }
  115. }
  116. position += 4;
  117. oplen += 3;
  118. resultlen = resultlen - 4;
  119. }
  120. return output;
  121. }
  122. public function MyBase64()
  123. {
  124. throw new Error("Base64 class is static container only");
  125. }
  126. public static function find(val:int):int
  127. {
  128. if (val >= 65 && val<=90)
  129. {
  130. return val-65;
  131. }
  132. else if (val >= 97 && val<=122)
  133. {
  134. return 26+val-97;
  135. }
  136. else if (val >= 48 && val<=57)
  137. {
  138. return 52+val-48;
  139. }
  140. else if (val==43)
  141. {
  142. return 62;
  143. }
  144. else if (val==47)
  145. {
  146. return 63;
  147. }
  148. else if (val==61)
  149. {
  150. return 64;
  151. }
  152. return 0;
  153. }
  154. }
  155. }