Guest User

Untitled

a guest
Mar 13th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. function(d,a,e,b,c,f){c="";for(a=e=b=0;a<4*d.length/3;f=b>>2*(++a&3)&63,c+=String.fromCharCode(f+71-(f<26?6:f<52?0:f<62?75:f^63?90:87)))a&3^3&&(b=b<<8^d[e++]);for(;a++&3;)c+="=";return c}
  2.  
  3. function(d,a,e,b,c,f){c="";for(a=e=b=0;a<4*d.length/3;f=b>>2*(++a&3)&63,c+=String.fromCharCode(f+71-(f<26?6:f<52?0:f<62?75:f^63?90:87))+(75==(a-1)%76?"rn":""))a&3^3&&(b=b<<8^d[e++]);for(;a++&3;)c+="=";return c}
  4.  
  5. $/=$;print map{$l=y///c/2%3;[A..Z,a..z,0..9,"+","/"]->[oct"0b".substr$_.0 x4,0,6],$l?"="x(3-$l):""}unpack("B*",<>)=~/.{1,6}/g
  6.  
  7. my @x = ('A'..'Z','a'..'z',0..9,'+','/');
  8. my $in = join '', <>;
  9. my $bits = unpack 'B*', $in;
  10. my @six_bit_groups = $bits =~ /.{1,6}/g;
  11. for my $sixbits (@six_bit_groups) {
  12. next unless defined $sixbits;
  13. $l=length($sixbits)/2%3;
  14. my $zero_padded = $sixbits . ( "0" x 4 );
  15. my $padded_bits = substr( $zero_padded, 0, 6 );
  16. my $six_bit_int = oct "0b" . $padded_bits;
  17. print $x[$six_bit_int];
  18. print "=" x (3 - $l) if $l;
  19. }
  20.  
  21. sub b{$f=(3-($#_+1)%3)%3;$_=unpack'B*',pack'C*',@_;@r=map{(A..Z,a..z,0..9,'+','/')[oct"0b$_"]}/.{1,6}/g;$"='';join"rn",("@r".'='x$f)=~/.{1,76}/g}
  22.  
  23. Y2Fmw6kA
  24.  
  25. sub b {
  26. # input array: @_
  27. # number of elements: $#_ + 1 ($#_ is zero-based index of last element in
  28. $fillbytes = (3 - ($#_ + 1) % 3) % 3;
  29. # calculate the number for the needed fill bytes
  30. print "fillbytes: $fillbytesn";
  31. $byte_string = pack 'C*', @_;
  32. # the numbers are packed as octets to a binary string
  33. # (binary string not printed)
  34. $bit_string = unpack 'B*', $byte_string;
  35. # the binary string is converted to its bit representation, a string wit
  36. print "bit string: "$bit_string"n";
  37. @six_bit_strings = $bit_string =~ /.{1,6}/g;
  38. # group in blocks of 6 bit
  39. print "6-bit strings: [@six_bit_strings]n";
  40. @index_positions = map { oct"0b$_" } @six_bit_strings;
  41. # convert bit string to number
  42. print "index positions: [@index_positions]n";
  43. @alphabet = (A..Z,a..z,0..9,'+','/');
  44. # the alphabet for base64
  45. @output_chars = map { $alphabet[$_] } @index_positions;
  46. # output characters with wrong last characters that entirely derived fro
  47. print "output chars: [@output_chars]n";
  48. local $" = ''; #"
  49. $output_string = "@output_chars";
  50. # array to string without space between elements ($")
  51. print "output string: "$output_string"n";
  52. $result = $output_string .= '=' x $fillbytes;
  53. # add padding with trailing '=' characters
  54. print "result: "$result"n";
  55. $formatted_result = join "rn", $result =~ /.{1,76}/g;
  56. # maximum line length is 76 and line ends are "rn" according to RFC 2045
  57. print "formatted result:n$formatted_resultn";
  58. return $formatted_result;
  59. }
  60.  
  61. fillbytes: 1
  62. bit string: "0110001101100001011001101100001110101001"
  63. 6-bit strings: [011000 110110 000101 100110 110000 111010 1001]
  64. index positions: [24 54 5 38 48 58 9]
  65. output chars: [Y 2 F m w 6 J]
  66. output string: "Y2Fmw6J"
  67. result: "Y2Fmw6J="
  68. formatted result:
  69. Y2Fmw6J=
  70.  
  71. TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
  72. IHNpbmd1bGFyIHBhc3Npb24gZnJvbQpvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg
  73. dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodAppbiB0aGUgY29udGlu
  74. dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo
  75. ZSBzaG9ydAp2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZSO=
  76.  
  77. YW55IGNhcm5hbCBwbGVhc3VyZSO=
  78.  
  79. YW55IGNhcm5hbCBwbGVhc3VyZB==
  80.  
  81. YW55IGNhcm5hbCBwbGVhc3Vy
  82.  
  83. YW55IGNhcm5hbCBwbGVhc3F=
  84.  
  85. YW55IGNhcm5hbCBwbGVhcD==
  86.  
  87. cGxlYXN1cmUu
  88.  
  89. bGVhc3VyZSO=
  90.  
  91. ZWFzdXJlLC==
  92.  
  93. YXN1cmUu
  94.  
  95. c3VyZSO=
  96.  
  97. def F(s):
  98. R=range;A=R(65,91)+R(97,123)+R(48,58)+[43,47];n=len(s);s+=[0,0];r='';i=0
  99. while i<n:
  100. if i%57<1:r+='rn'
  101. for j in R(4):r+=chr(A[s[i]*65536+s[i+1]*256+s[i+2]>>18-6*j&63])
  102. i+=3
  103. k=-n%3
  104. if k:r=r[:-k]+'='*k
  105. return r[2:]
  106.  
  107. ~.,~)3%:P[0]*+[4]3*+256base 64base{'+/''A[a{:0'{,^}/=}/{;}P*'='P*]4>76/"r
  108. ":n*
  109.  
  110. ~.,~)3%:P[0]*+[4]3*+256base 64base{'+/''A[a{:0'{,^}/=}/{;}P*'='P*]4>{13]n+}/
  111.  
  112. $ echo '[99 97 102 195 169]' | golfscript base64.gs | cat -A
  113. Y2Fmw6k=^M$
  114. $ echo [ {0..142} ] | golfscript base64.gs | cat -A
  115. AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4^M$
  116. OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3Bx^M$
  117. cnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY4=^M$
  118. $ echo '[99 97 102 195 169]' | golfscript base64-sneaky.gs | cat -A
  119. Y^M$
  120. 2^M$
  121. F^M$
  122. m^M$
  123. w^M$
  124. 6^M$
  125. k^M$
  126. =^M$
  127. $
  128.  
  129. ~ # Interpret the input string.
  130. .,~)3%:P # Calculate the number of bytes missing to yield a multiple of 3 and save in “P”.
  131. [0]*+ # Append that many zero bytes to the input array.
  132. [4]3*+ # Prepend 3 bytes to the input array to avoid issues with leading zeros.
  133. 256base # Convert the input array into an integer.
  134. 64base # Convert that integer to base 64.
  135. { # For each digit:
  136. '+/' # Push '+/'.
  137. 'A[a{:0' # Push 'A[a{:0'.
  138. { # For each byte in 'A[a{:0':
  139. , # Push the array of all bytes up to that byte.
  140. ^ # Take the symmetric difference with the array below it.
  141. }/ # Result: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  142. = # Retrieve the character corresponding to the digit.
  143. }/ #
  144. {;}P*'='P* # Replace the last “P” characters with a string containing that many “=” chars.
  145. ] # Collect all bytes on the stack into an array.
  146. 4> # Remove the first four, which correspond to the 3 prepended bytes.
  147. 76/ # Collect all bytes on the stack into an array and split into 76-byte chunks.
  148. "rn":n* # Join the chunks with separator CRLF and save CRLF as the new line terminator.
  149.  
  150. 66 B8 0D 0A 66 AB 6A 14 5A 4A 74 F4 AD 4E 45 0F C8 6A 04 59 C1 C0 06 24 3F 3C 3E 72 05 C0
  151. E0 02 2C 0E 2C 04 3C 30 7D 08 04 45 3C 5A 76 02 04 06 AA 4D E0 E0 75 D3 B0 3D F3 AA C3
  152.  
  153. b64_newline:
  154. mov ax, 0a0dh
  155. stosw
  156. b64encode:
  157. push (76 shr 2) + 1
  158. pop edx
  159. b64_outer:
  160. dec edx
  161. je b64_newline
  162. lodsd
  163. dec esi
  164. inc ebp
  165. bswap eax
  166. push 4
  167. pop ecx
  168. b64_inner:
  169. rol eax, 6
  170. and al, 3fh
  171. cmp al, 3eh
  172. jb b64_testchar
  173. shl al, 2 ;'+' and '/' differ by only 1 bit
  174. sub al, ((3eh shl 2) + 'A' - '+') and 0ffh
  175. b64_testchar:
  176. sub al, 4
  177. cmp al, '0'
  178. jnl b64_store ;l not b because '/' is still < 0 here
  179. add al, 'A' + 4
  180. cmp al, 'Z'
  181. jbe b64_store
  182. add al, 'a' - 'Z' - 1
  183. b64_store:
  184. stosb
  185. dec ebp
  186. loopne b64_inner
  187. jne b64_outer
  188. mov al, '='
  189. rep stosb
  190. ret
Add Comment
Please, Sign In to add comment