Guest User

RSAGuess.java

a guest
Oct 18th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. import java.math.BigInteger;
  2.  
  3. import java.util.Date;
  4.  
  5. import java.util.HashMap;
  6.  
  7.  
  8.  
  9. /*
  10.  
  11. *
  12.  
  13. * This tool encrypts numbers with a given public key and replace similarities in the cipher text with the clear text.
  14.  
  15. * This tool only cracks the unmodified RSA algorithm with clear text sizes up to 2147483647 per number.
  16.  
  17. * It's only made for scientific purposes, not for criminal ones.
  18.  
  19. *
  20.  
  21. * @author mitosis
  22.  
  23. * @version 1.0
  24.  
  25. *
  26.  
  27. */
  28.  
  29.  
  30.  
  31. public class RSAGuess {
  32.  
  33.  
  34.  
  35. public static void main(String[] args) {
  36.  
  37. try {
  38.  
  39. String argsadded = "";
  40.  
  41. for (int i = 0; i < args.length; i++) {
  42.  
  43. argsadded = argsadded + args[i];
  44.  
  45. }
  46.  
  47.  
  48.  
  49. if (argsadded.isEmpty() || argsadded.equalsIgnoreCase("version")) {
  50.  
  51. System.out.println("RSAGuess v1.0 by mitosis <[email protected]>."
  52.  
  53. + "\nUse argument 'help' to learn how to use this tool.");
  54.  
  55. } else if (argsadded.equalsIgnoreCase("help")) {
  56.  
  57.  
  58.  
  59. System.out.println("The one and only rule: Use this tool ONLY for GOOD purposes."
  60.  
  61. + "\nType the arguments like this: N e s l c d" + "\nN - RSA modulus" + "\ne - public exponent"
  62.  
  63. + "\ns - smallest number exptected in message (the number doesn't has to exist in message!)"
  64.  
  65. + "\nl - largest number expected in message (the number doesn't has to exist in message!)"
  66.  
  67. + "\nc - ciphertext" + "\nd - delimiter" + "\n" + "\nA tip:"
  68.  
  69. + "\nMaybe you are a kind of confused what s and l argument mean. Just use 0 for s and the highest number you expect in the message for l."
  70.  
  71. + "\nAnother tip:"
  72.  
  73. + "\nSome delimiter chars won't work. I never had problems with using a minus -");
  74.  
  75.  
  76.  
  77. } else {
  78.  
  79.  
  80.  
  81. BigInteger N = new BigInteger(args[0]);
  82.  
  83. BigInteger e = new BigInteger(args[1]);
  84.  
  85.  
  86.  
  87. String m = "";
  88.  
  89.  
  90.  
  91. int begin = Integer.parseInt(args[2]);
  92.  
  93.  
  94.  
  95. int end = Integer.parseInt(args[3]);
  96.  
  97.  
  98.  
  99. String chr = args[5];
  100.  
  101.  
  102.  
  103. String c = args[4];
  104.  
  105.  
  106.  
  107. int g = begin;
  108.  
  109.  
  110.  
  111. long start = new Date().getTime();
  112.  
  113.  
  114.  
  115. Dictionary d = new Dictionary();
  116.  
  117.  
  118.  
  119. HashMap<Integer, BigInteger> cMap = new HashMap<Integer, BigInteger>();
  120.  
  121.  
  122.  
  123. while (g <= end) {
  124.  
  125.  
  126.  
  127. BigInteger cipher = new BigInteger(Integer.toString(g)).modPow(e, N);
  128.  
  129.  
  130.  
  131. d.addEntry(g, cipher);
  132.  
  133.  
  134.  
  135. g++;
  136.  
  137. }
  138.  
  139.  
  140.  
  141. for (int i = 0; i < c.split(chr).length; i++) {
  142.  
  143. cMap.put(i, new BigInteger((c.split(chr)[i])));
  144.  
  145. }
  146.  
  147.  
  148.  
  149. g = 0;
  150.  
  151. String dSize = Integer.toString(d.getDictionary().size());
  152.  
  153.  
  154.  
  155. for (int i = 0; i < cMap.size(); i++) {
  156.  
  157. for (BigInteger b = new BigInteger("0"); b.compareTo(new BigInteger(dSize)) == -1; b = b
  158.  
  159. .add(new BigInteger("1"))) {
  160.  
  161.  
  162.  
  163. if (cMap.get(i).toString().equals(d.getDictionary().get(b.intValueExact()).toString())) {
  164.  
  165.  
  166.  
  167. cMap.put(i, b);
  168.  
  169.  
  170.  
  171. break;
  172.  
  173.  
  174.  
  175. }
  176.  
  177. }
  178.  
  179. }
  180.  
  181.  
  182.  
  183. for (int i = 0; i < cMap.size(); i++) {
  184.  
  185. if (i == cMap.size() - 1) {
  186.  
  187. m = m + cMap.get(i).toString();
  188.  
  189. } else {
  190.  
  191. m = m + cMap.get(i).toString() + chr;
  192.  
  193. }
  194.  
  195. }
  196.  
  197.  
  198.  
  199. long runningTime = new Date().getTime() - start;
  200.  
  201. BigInteger bits = new BigInteger(Integer.toString(N.toString(2).length() / 2));
  202.  
  203.  
  204.  
  205. System.out.println("cracked RSA-" + bits + " in " + runningTime + "ms. m=" + m);
  206.  
  207.  
  208.  
  209. }
  210.  
  211.  
  212.  
  213. } catch (Throwable e) {
  214.  
  215. System.out.println("Something went wrong. Use argument 'help'.");
  216.  
  217. }
  218.  
  219. }
  220.  
  221. }
Advertisement
Add Comment
Please, Sign In to add comment