fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import javax.crypto.Cipher;
  7. import java.security.*;
  8. import javax.crypto.spec.*;
  9.  
  10.  
  11.  
  12. /* Name of the class has to be "Main" only if the class is public. */
  13. class Ideone
  14. {
  15.  
  16. public static class Base64 {
  17. /**
  18. * encode
  19. *
  20. * coverts a byte array to a string populated with base64 digits. It steps
  21. * through the byte array calling a helper method for each block of three
  22. * input bytes
  23. *
  24. * @param raw
  25. * The byte array to encode
  26. * @return A string in base64 encoding
  27. */
  28. public static String encode(byte[] raw) {
  29. StringBuffer encoded = new StringBuffer();
  30. for (int i = 0; i < raw.length; i += 3) {
  31. encoded.append(encodeBlock(raw, i));
  32. }
  33. return encoded.toString();
  34. }
  35.  
  36. /*
  37. * encodeBlock
  38. *
  39. * creates 4 base64 digits from three bytes of input data. we use an
  40. * integer, block, to hold the 24 bits of input data.
  41. *
  42. * @return An array of 4 characters
  43. */
  44. protected static char[] encodeBlock(byte[] raw, int offset) {
  45. int block = 0;
  46. // how much space left in input byte array
  47. int slack = raw.length - offset - 1;
  48. // if there are fewer than 3 bytes in this block, calculate end
  49. int end = (slack >= 2) ? 2 : slack;
  50. // convert signed quantities into unsigned
  51. for (int i = 0; i <= end; i++) {
  52. byte b = raw[offset + i];
  53. int neuter = (b < 0) ? b + 256 : b;
  54. block += neuter << (8 * (2 - i));
  55. }
  56.  
  57. // extract the base64 digits, which are six bit quantities.
  58. char[] base64 = new char[4];
  59. for (int i = 0; i < 4; i++) {
  60. int sixbit = (block >>> (6 * (3 - i))) & 0x3f;
  61. base64[i] = getChar(sixbit);
  62. }
  63. // pad return block if needed
  64. if (slack < 1)
  65. base64[2] = '=';
  66. if (slack < 2)
  67. base64[3] = '=';
  68. // always returns an array of 4 characters
  69. return base64;
  70. }
  71.  
  72. /*
  73. * getChar
  74. *
  75. * encapsulates the translation from six bit quantity to base64 digit
  76. */
  77. protected static char getChar(int sixBit) {
  78. if (sixBit >= 0 && sixBit <= 25)
  79. return (char) ('A' + sixBit);
  80. if (sixBit >= 26 && sixBit <= 51)
  81. return (char) ('a' + (sixBit - 26));
  82. if (sixBit >= 52 && sixBit <= 61)
  83. return (char) ('0' + (sixBit - 52));
  84. if (sixBit == 62)
  85. return '+';
  86. if (sixBit == 63)
  87. return '/';
  88. return '?';
  89. }
  90.  
  91. /**
  92. * decode
  93. *
  94. * convert a base64 string into an array of bytes.
  95. *
  96. * @param base64
  97. * A String of base64 digits to decode.
  98. * @return A byte array containing the decoded value of the base64 input
  99. * string
  100. */
  101. public static byte[] decode(String base64) {
  102. // how many padding digits?
  103. int pad = 0;
  104. for (int i = base64.length() - 1; base64.charAt(i) == '='; i--)
  105. pad++;
  106. // we know know the length of the target byte array.
  107. int length = base64.length() * 6 / 8 - pad;
  108. byte[] raw = new byte[length];
  109. int rawIndex = 0;
  110. // loop through the base64 value. A correctly formed
  111. // base64 string always has a multiple of 4 characters.
  112. for (int i = 0; i < base64.length(); i += 4) {
  113. int block = (getValue(base64.charAt(i)) << 18)
  114. + (getValue(base64.charAt(i + 1)) << 12)
  115. + (getValue(base64.charAt(i + 2)) << 6)
  116. + (getValue(base64.charAt(i + 3)));
  117. // based on the block, the byte array is filled with the
  118. // appropriate 8 bit values
  119. for (int j = 0; j < 3 && rawIndex + j < raw.length; j++)
  120. raw[rawIndex + j] = (byte) ((block >> (8 * (2 - j))) & 0xff);
  121. rawIndex += 3;
  122. }
  123. return raw;
  124. }
  125.  
  126. /*
  127. * getValue
  128. *
  129. * translates from base64 digits to their 6 bit value
  130. */
  131. protected static int getValue(char c) {
  132. if (c >= 'A' && c <= 'Z')
  133. return c - 'A';
  134. if (c >= 'a' && c <= 'z')
  135. return c - 'a' + 26;
  136. if (c >= '0' && c <= '9')
  137. return c - '0' + 52;
  138. if (c == '+')
  139. return 62;
  140. if (c == '/')
  141. return 63;
  142. if (c == '=')
  143. return 0;
  144. return -1;
  145. }
  146. }
  147. public static void main (String[] args) throws java.lang.Exception
  148. {
  149. try {
  150. final Cipher instance = Cipher.getInstance("AES/ECB/PKCS5Padding");
  151. instance.init(2, new SecretKeySpec("ThisChallengeIsA".getBytes(), "AES"));
  152.  
  153. System.out.println(new String(instance.doFinal(Base64.decode("Hm4iMnl2crfdzCmta3oGNw=="))));
  154. }catch (Exception e){
  155. System.out.println(e.toString());
  156. }
  157. }
  158. }
Success #stdin #stdout 0.24s 58836KB
stdin
Standard input is empty
stdout
win_on_