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 java.security.MessageDigest;
  7. import java.security.spec.AlgorithmParameterSpec;
  8. import javax.crypto.Cipher;
  9. import javax.crypto.Mac;
  10. import javax.crypto.SecretKey;
  11. import javax.crypto.SecretKeyFactory;
  12. import javax.crypto.spec.IvParameterSpec;
  13. import javax.crypto.spec.PBEKeySpec;
  14. import javax.crypto.spec.PBEParameterSpec;
  15. import javax.crypto.spec.SecretKeySpec;
  16.  
  17. /* Name of the class has to be "Main" only if the class is public. */
  18. class Ideone
  19. {
  20. public static void main (String[] args) throws java.lang.Exception
  21. {
  22. // your code goes here
  23. }
  24.  
  25. public static String tryMD2(String s)
  26. {
  27. try
  28. {
  29. MessageDigest digest = MessageDigest.getInstance("MD2");
  30. byte[] pt = s.getBytes();
  31. byte[] ct = digest.digest(pt);
  32. if (bytesToHex(ct).equals("66b6085b52d76556e012e68da00e348c"))
  33. {
  34. String result = "";
  35. for (int i : new int[] { 2, 13, 12, 6, 015 })
  36. {
  37. result = result + s.charAt(i);
  38. }
  39. return result;
  40. }
  41. return null;
  42. }
  43. catch (Exception e) {}
  44. return null;
  45. }
  46.  
  47. public static String trySHA512(String s)
  48. {
  49. try
  50. {
  51. MessageDigest digest = MessageDigest.getInstance("SHA-512");
  52. byte[] pt = s.getBytes();
  53. byte[] ct = digest.digest(pt);
  54. if (bytesToHex(ct).equals("018ba59e2008dfb245e84cc75d1492a061ffda75e4945aff2ae50794275d62bd7a79e9f79823b7de0078699a82fea921d40e0250aced272cd688f94bda8d44e3"))
  55. {
  56. String result = "";
  57. for (int i : new int[] { 6, 16, 11, 24, 4, 21, 22, 31 }) {
  58. result = result + s.charAt(i);
  59. }
  60. return result;
  61. }
  62. return null;
  63. }
  64. catch (Exception e) {}
  65. return null;
  66. }
  67.  
  68. public static String try3DES(String s)
  69. {
  70. try
  71. {
  72. String key = "";
  73. for (int i = 24; i > 0; i--) {
  74. key = key + s.charAt(i);
  75. }
  76. byte[] theKey = key.getBytes();
  77. String iv = "";
  78. for (int i : new int[] { 1, 21, 24, 19, 26, 7, 15, 29 }) {
  79. iv = iv + s.charAt(i);
  80. }
  81. byte[] theIVp = iv.getBytes();
  82. byte[] pt = s.getBytes();
  83.  
  84. Cipher cipher = Cipher.getInstance("TripleDES/CFB64/NoPadding");
  85. SecretKeySpec keySpec = new SecretKeySpec(theKey, "TripleDES");
  86. AlgorithmParameterSpec apSpec = new IvParameterSpec(theIVp);
  87. cipher.init(1, keySpec, apSpec);
  88.  
  89. byte[] ct = cipher.doFinal(pt);
  90. return bytesToHex(ct).equals(
  91. "f6e0a472124df090804f925e511aac5779027350be117cd26f02983c46e3") ? iv : null;
  92. }
  93. catch (Exception e) {}
  94. return null;
  95. }
  96.  
  97. public static String tryBlowfish(String s)
  98. {
  99. try
  100. {
  101. String key = "";
  102. for (int i : new int[] { 6, 30, 24, 19, 7, 26, 17, 31, 29 }) {
  103. key = key + s.charAt(i);
  104. }
  105. byte[] theKey = key.getBytes();
  106. byte[] theIVp = hexToBytes("1234567890ABCDEF");
  107. byte[] pt = s.getBytes();
  108.  
  109. Cipher cipher = Cipher.getInstance("Blowfish/CBC/ISO10126Padding");
  110. SecretKeySpec keySpec = new SecretKeySpec(theKey, "Blowfish");
  111. AlgorithmParameterSpec asSpec = new IvParameterSpec(theIVp);
  112. cipher.init(1, keySpec, asSpec);
  113.  
  114. byte[] ct = cipher.doFinal(pt);
  115. return bytesToHex(ct).startsWith(
  116. "2bb1ca91478b66968c154822379416fd9b26d998c80856638cf9e02cd5c09ff4") ? key :
  117. null;
  118. }
  119. catch (Exception e) {}
  120. return null;
  121. }
  122.  
  123. public static String tryHMAC(String s)
  124. {
  125. try
  126. {
  127. byte[] theKey = "cafebabecafebabe".getBytes();
  128. byte[] pt = s.getBytes();
  129.  
  130. SecretKeySpec keySpec = new SecretKeySpec(theKey, "HmacMD5");
  131. Mac mac = Mac.getInstance("HmacMD5");
  132. mac.init(keySpec);
  133.  
  134. byte[] ct = mac.doFinal(pt);
  135. if (bytesToHex(ct).equals("3298712a2cb84e1f1a36855adc68f5f1"))
  136. {
  137. String result = "";
  138. for (int i : new int[] { 0, 16, 6, 43, 37, 21, 26, 29 }) {
  139. result = result + s.charAt(i);
  140. }
  141. return result;
  142. }
  143. return null;
  144. }
  145. catch (Exception e) {}
  146. return null;
  147. }
  148.  
  149. public static String tryAES(String s)
  150. {
  151. try
  152. {
  153. byte[] theKey = "cafebabecafebabe".getBytes();
  154. byte[] theIVp = hexToBytes("0123456789ABCDEF0123456789ABCDEF");
  155. byte[] pt = s.getBytes();
  156.  
  157. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  158. SecretKeySpec keySpec = new SecretKeySpec(theKey, "AES");
  159. AlgorithmParameterSpec apSpec = new IvParameterSpec(theIVp);
  160. cipher.init(1, keySpec, apSpec);
  161.  
  162. byte[] ct = cipher.doFinal(pt);
  163. if (bytesToHex(ct).equals(
  164. "12b2de1f02647652edd2cde34cdab1ff39fc4d137616cf4b45849f07a2df73550261482ab5af84f1f5b3e4a15fab1f5adceef07ae6c42ea776d9a18b777c0733"))
  165. {
  166. String result = "";
  167. for (int i : new int[] { 12, 24, 3, 30, 48, 28, 18, 39, 45, 14, 33 }) {
  168. result = result + s.charAt(i);
  169. }
  170. return result;
  171. }
  172. return null;
  173. }
  174. catch (Exception e) {}
  175. return null;
  176. }
  177.  
  178. public static String tryPBE(String s)
  179. {
  180. try
  181. {
  182. byte[] salt = hexToBytes("CAFEBABECAFEBABE");
  183. int count = 20;
  184. int[] pos = { 16, 5, 21, 28, 4, 10, 40, 46, 26, 7, 33, 41, 49, 12 };
  185. char[] theKey = new char[pos.length];
  186. for (int i = 0; i < pos.length; i++) {
  187. theKey[i] = s.charAt(pos[i]);
  188. }
  189. byte[] pt = s.getBytes();
  190.  
  191. Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
  192. PBEKeySpec pbeKeySpec = new PBEKeySpec(theKey);
  193. SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
  194. PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count);
  195. SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
  196. cipher.init(1, pbeKey, pbeParamSpec);
  197.  
  198. byte[] ct = cipher.doFinal(pt);
  199. return bytesToHex(ct)
  200. .equals("e2697301c74bfa8064816741a11046cf3fa18f1a63afab2a5ce4368ac6e98947a195f541717b208bd9451473e483619f63c4c7ce515de56d") ? new String(theKey):null;
  201. }
  202. catch (Exception e) {}
  203. return null;
  204. }
  205.  
  206. public static String md5(String s)
  207. {
  208. try
  209. {
  210. MessageDigest digest = MessageDigest.getInstance("MD5");
  211. byte[] pt = s.getBytes();
  212. byte[] ct = digest.digest(pt);
  213. return bytesToHex(ct);
  214. }
  215. catch (Exception e) {}
  216. return null;
  217. }
  218.  
  219. public static byte[] hexToBytes(String str)
  220. {
  221. if (str == null) {
  222. return null;
  223. }
  224. int len = str.length() / 2;
  225. byte[] buffer = new byte[len];
  226. for (int i = 0; i < len; i++) {
  227. buffer[i] = ((byte)Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16));
  228. }
  229. return buffer;
  230. }
  231.  
  232. public static String bytesToHex(byte[] data)
  233. {
  234. if (data == null) {
  235. return null;
  236. }
  237. int len = data.length;
  238. StringBuffer hexString = new StringBuffer(len * 2);
  239. for (int i = 0; i < len; i++)
  240. {
  241. if ((data[i] & 0xFF) < 16) {
  242. hexString.append(0);
  243. }
  244. hexString.append(Integer.toHexString(data[i] & 0xFF));
  245. }
  246. return hexString.toString();
  247. }
  248. }
Success #stdin #stdout 0.1s 54896KB
stdin
Standard input is empty
stdout
Standard output is empty