pastebin light

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

pastebin light //misacek

Posted by kas-4diff on Mon 16 Jan 2012 23:33:42 CET
download | new post

  1. import java.io.BufferedReader;
  2. import java.io.DataInputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.InputStreamReader;
  6. import java.util.*;
  7. import java.math.*;
  8.  
  9. class desifrator{
  10. final static String confPath = "conf.txt";
  11.  
  12. static char[] freq = new char[26];
  13. static char[] abc = new char[26];
  14. static String A = "";
  15. static String B = "";
  16. static String cestaA = " ";
  17. static String cestaB = " ";
  18. static Scanner freqSada = new Scanner(System.in);
  19. static Scanner slovnik = new Scanner(System.in);
  20. static Scanner delkaSlov = new Scanner(System.in);
  21. static Scanner presnostSlov = new Scanner(System.in);
  22. static Scanner slovnikAuto = new Scanner(System.in);
  23. static String volbaSady = "cze1";
  24. static String volbaSlovniku = "ne";
  25. static int delka = 4;
  26. static double presnost = 0.75;
  27. static String auto = "ne";
  28. static String pred = " ";
  29. static String bude = " ";
  30.  
  31. public static void otevriSoubory(){
  32. Scanner confFile;
  33. File conf = new File(confPath);
  34.  
  35. if(conf.exists()){
  36. System.out.println("... konfiguracni soubor cest nalezen" + "\t \"" + confPath + "\"");
  37.  
  38. try{
  39. confFile = new Scanner(new File(confPath));
  40. cestaA = confFile.next();
  41. cestaB = confFile.next();
  42.  
  43. }catch(Exception e){
  44. System.out.println("FAIL");
  45. }
  46.  
  47. }else{
  48. System.out.println("... nenalezen konfiguracni soubor cest !");
  49. System.exit(0);
  50. }
  51.  
  52.  
  53. File ciphertext = new File(cestaA);
  54. File slovnik = new File(cestaB);
  55.  
  56.  
  57. if(ciphertext.exists()){
  58. System.out.println("... text pro analyzu nalezen" + "\t\t \"" + cestaA + "\"");
  59.  
  60. }else{
  61. System.out.println("... nenalezen soubor pro analyzu !");
  62. System.exit(0);
  63. }
  64.  
  65.  
  66. if(slovnik.exists()){
  67. System.out.println("... slovnik pro analyzu nalezen" + "\t\t \"" + cestaB + "\"");
  68.  
  69. }else{
  70. System.out.println("... slovnik pro analyzu nenalezen !");
  71. System.exit(0);
  72. }
  73. }
  74.  
  75. public static void frekvencniAnalyza(){
  76. int freqPos = 0;
  77. String cele = "";
  78. String text = "";
  79. Scanner soubor;
  80. Map hm = new HashMap();
  81. int pomocna = 0;
  82. int max = 0;
  83. int maxChar = 0;
  84.  
  85.  
  86. try{
  87. soubor = new Scanner(new File(cestaA));
  88. while(soubor.hasNext()){
  89. cele = cele + soubor.next();
  90. cele = cele.toLowerCase();
  91. }
  92. soubor.close();
  93.  
  94. }catch(Exception e){
  95. System.out.println("FAIL");
  96. }
  97.  
  98. try{
  99. soubor = new Scanner(new File(cestaA));
  100. while(soubor.hasNext()){
  101. text = text + soubor.next() + " ";
  102. }
  103. soubor.close();
  104.  
  105. }catch(Exception e){
  106. System.out.println("FAIL");
  107. }
  108.  
  109.  
  110. //System.out.println(cele);
  111. System.out.println("--------------------------------------------------------------------------------");
  112. System.out.println("Frekvencni analyza na zaklade cestnosti znaku pro cesky jazyk se sadou " + "\"" + volbaSady + "\":");
  113. System.out.println();
  114. System.out.println(text);
  115. System.out.println();
  116. A = text;
  117. for(int i = 0; i < cele.length(); i++){
  118.  
  119. if(hm.containsKey(cele.charAt(i))){
  120. pomocna = (Integer) hm.get(cele.charAt(i));
  121. hm.put(cele.charAt(i), pomocna + 1);
  122. }else{
  123. hm.put(cele.charAt(i),1);
  124. }
  125. }
  126.  
  127. while(hm.size() != 0){
  128. for(int i = 97; i < 123; i++){
  129. if(hm.get((char)i) != null){
  130. pomocna = (Integer) hm.get((char)i);
  131. if(pomocna > max){
  132. max = pomocna;
  133. maxChar = i;
  134. }
  135. }
  136. }
  137. abc[freqPos]=(Character.toUpperCase((char)maxChar));
  138. text=text.replace(Character.toUpperCase(((char)maxChar)),freq[freqPos]);
  139. max = 0;
  140. freqPos++;
  141. hm.remove((char)maxChar);
  142. }
  143. System.out.println(text);
  144. B = text;
  145. System.out.println();
  146. vypisTabulky();
  147. System.out.println("--------------------------------------------------------------------------------");
  148. System.out.println();
  149. }
  150.  
  151. public static void vypisTabulky(){
  152. System.out.print("| ");
  153. for(int i=0; i <= 25; i++){
  154.  
  155. System.out.print(Character.toUpperCase(abc[i]) + " ");
  156. }
  157. System.out.println();
  158. System.out.print("| ");
  159. for(int i=0; i <= 25; i++){
  160.  
  161. System.out.print((freq[i]) + " ");
  162. }
  163. System.out.println();
  164. }
  165.  
  166. public static void zmenText(){
  167. B = A;
  168.  
  169. for(int i = 0; i <= 25; i++){
  170. B = B.replace(abc[i],freq[i]);
  171. }
  172. }
  173.  
  174. public static void vypisTexty(){
  175. System.out.println(A);
  176. System.out.println(B);
  177. }
  178.  
  179. public static void vypisText(){
  180. System.out.println(B);
  181. System.out.println("--------------------------------------------------------------------------------");
  182. }
  183.  
  184. public static void zamenPismena(char a, char b){
  185. int poziceA = 0;
  186. int poziceB = 0;
  187.  
  188. for(int i = 0; i <= 25; i++){
  189. if((int)freq[i] == (int)a){
  190. poziceA = i;
  191. }
  192. }
  193.  
  194. for(int i = 0; i <= 25; i++){
  195. if((int)freq[i] == (int)b){
  196. poziceB = i;
  197. }
  198. }
  199.  
  200. freq[poziceA]=b;
  201. freq[poziceB]=a;
  202.  
  203. }
  204.  
  205. public static void slovnik(){ try {
  206. Scanner soubor;
  207. HashMap<String, Integer> cm = new HashMap<String, Integer>();
  208. int pomocna = 0;
  209. int hledane = 0;
  210. int nalezene = 0;
  211. double usp = 0;
  212. int navrhSum = 0;
  213. String navrhStr = "";
  214. char z = ' ';
  215. char na = ' ';
  216.  
  217. System.out.println("Provadim prohledavani slovniku a parovani slov dle zadanych parametru.");
  218.  
  219. ArrayList<String> slova = new ArrayList<String>();
  220.  
  221. Scanner tokenize = new Scanner(B);
  222. while (tokenize.hasNext()) {
  223. slova.add(tokenize.next());
  224. }
  225.  
  226. for(int i=0; i < slova.size(); i++){
  227. String s = (String)slova.get(i);
  228. int sl = s.length();
  229. int match = 0;
  230. String nejlepsi = "";
  231. double predchozi = 0;
  232.  
  233. if(sl >= delka){
  234. hledane++;
  235. try{
  236. BufferedReader slovnik = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream("cze.txt"))));
  237. String word;
  238. while((word = slovnik.readLine()) != null){
  239.  
  240. if(word.length() == sl){
  241. for(int y=0; y < sl; y++){
  242. if((int)(s.charAt(y)) == (int)(word.charAt(y))){
  243. match++;
  244. }
  245. }
  246. double pomer = ((double)(match) / (double)(sl));
  247. if(pomer >= presnost){
  248. if(pomer > predchozi){
  249. nejlepsi = word;
  250. predchozi = pomer;
  251. }
  252. }
  253. match=0;
  254. pomer=0;
  255. }
  256.  
  257. }
  258. //System.out.println(nejlepsi);
  259. System.out.print(".");
  260. if(nejlepsi != ""){
  261. usp = usp + predchozi;
  262. nalezene++;
  263. for(int y=0; y < sl; y++){
  264. if((int)(s.charAt(y)) != (int)(nejlepsi.charAt(y))){
  265. String zmena = Character.toString((s.charAt(y))) + Character.toString((nejlepsi.charAt(y)));
  266.  
  267. if(cm.containsKey(zmena)){
  268. pomocna = (Integer) cm.get(zmena);
  269. cm.put(zmena, pomocna + 1);
  270. }else{
  271. cm.put(zmena,1);
  272. }
  273. }
  274. }
  275. }
  276. }catch(Exception e){
  277. System.out.println("FAIL");
  278. }
  279.  
  280. }
  281. }
  282.  
  283. for(Map.Entry<String, Integer> e : cm.entrySet()){
  284. //System.out.println(e.getKey() + " " + e.getValue());
  285. if(e.getValue() > navrhSum){
  286. navrhStr = e.getKey();
  287. navrhSum = e.getValue();
  288. }
  289. }
  290.  
  291. na = navrhStr.charAt(1);
  292. z = navrhStr.charAt(0);
  293. usp = usp / nalezene;
  294. System.out.println();
  295. System.out.println();
  296.  
  297. bude = z + "" + na;
  298.  
  299.  
  300. double uspUpr = usp * 100;
  301. System.out.println("Pocet uspesne sparovanych slov:\t\t " + nalezene + " z " + hledane);
  302. System.out.printf("Prumerna presnost rozpoznani slov:\t %.1f", uspUpr); System.out.print(" %");System.out.println();
  303. if(auto.equalsIgnoreCase("ne")){
  304. if((navrhSum >= 3) && (((double)nalezene/(double)hledane) > 0.4) && (usp < 0.99)){
  305. System.out.println("Rada slovniku pro dalsi zmenu:\t\t " + z + " -> " + na);
  306. }else{
  307. System.out.println("Rada slovniku pro dalsi zmenu:\t\t " + "... slovnik neni schopen poradit");
  308. }
  309. manual();
  310. }else{
  311. if((((double)nalezene/(double)hledane) > 0.5) && (usp < 0.99) && (usp > 0.80) && (navrhSum > 4) && (!bude.equals(pred))){
  312. System.out.println("Automaticky provadim zmenu znaku:\t " + z + " -> " + na);
  313. System.out.println();
  314. pred = z + "" + na;
  315. zamenPismena(Character.toLowerCase(z),Character.toLowerCase(na));
  316. vypisTabulky();
  317. zmenText();
  318. System.out.println();
  319. vypisText();
  320. slovnik();
  321.  
  322. }else{
  323.  
  324. if(bude.equals(pred)){
  325. zamenPismena(Character.toLowerCase(na),Character.toLowerCase(z));
  326. zmenText();
  327. System.out.println("!!! Riziko zacykleni neprovadim zmenu " + z + " -> " + na + " !!!");
  328. System.out.println();
  329. vypisText();
  330.  
  331. }else{
  332. if((navrhSum >= 3) && (((double)nalezene/(double)hledane) > 0.4) && (usp < 0.99)){
  333. System.out.println("Rada slovniku pro dalsi zmenu:\t\t " + z + " -> " + na);
  334. }else{
  335. System.out.println("Rada slovniku pro dalsi zmenu:\t\t " + "... slovnik neni schopen poradit");
  336. }
  337. }
  338. manual();
  339. }
  340.  
  341. } } catch (Exception e) { }
  342. }
  343.  
  344. public static void manual(){
  345. System.out.println();
  346. Scanner vstupA = new Scanner(System.in);
  347. Scanner vstupB = new Scanner(System.in);
  348. String A;
  349. String B;
  350. System.out.print("Zadejte pismeno, ktere si prejete zmenit:\t");
  351. A = vstupA.nextLine();
  352. System.out.print("Zadejte pismeno, ktere za nej chcete dosadit:\t");
  353. B = vstupB.nextLine();
  354. System.out.println();
  355. zamenPismena(Character.toLowerCase(A.charAt(0)),Character.toLowerCase(B.charAt(0)));
  356.  
  357. vypisTabulky();
  358. zmenText();
  359. System.out.println();
  360. vypisText();
  361.  
  362. if(volbaSlovniku.equalsIgnoreCase("ano")){
  363. slovnik();
  364. }
  365. manual();
  366. }
  367.  
  368. public static void menu(){
  369.  
  370. System.out.println("********************************************************************************");
  371. System.out.println(" Vitejte v programu pro kryptoanalyzu monoalfabeticke substitucni sifry ");
  372. System.out.println("********************************************************************************");
  373. otevriSoubory();
  374. System.out.println("--------------------------------------------------------------------------------");
  375. System.out.print("Zvolte sadu cestnosti znaku daneho jazyka (cze0,cze1):\t ");
  376. volbaSady = freqSada.nextLine();
  377. if(volbaSady.equalsIgnoreCase("cze0")){
  378. char [] freqCh = {'e','a','o','i','n','s','t','r','v','u','l','z','d','k','p','m','c','y','h','j','b','g','f','x','w','q'};
  379. freq = freqCh;
  380. }else{
  381. char [] freqCh = {'e','t','o','a','n','i','r','s','h','d','l','c','f','u','m','p','y','w','g','b','v','k','j','z','q','x'};
  382. freq = freqCh;
  383. }
  384.  
  385. System.out.print("Prejete si vyuzit slovnik ? (ano/ne):\t\t\t ");
  386. volbaSlovniku = slovnik.nextLine();
  387. if(volbaSlovniku.equalsIgnoreCase("ne")){
  388. frekvencniAnalyza();
  389. manual();
  390. }else{
  391. System.out.print("Jaka je minimalni delka slov pro porovnavani ? (3-9):\t ");
  392. delka = delkaSlov.nextInt();
  393. System.out.print("Jakou min. presnost u slov zvolite ? (napr. 0,80 = 80%): ");
  394. presnost = presnostSlov.nextDouble();
  395. System.out.print("Moznost automatickych zmen dle slozniku ? (ano/ne):\t ");
  396. auto = slovnikAuto.nextLine();
  397.  
  398. frekvencniAnalyza();
  399. slovnik();
  400.  
  401. }
  402.  
  403.  
  404. }
  405.  
  406.  
  407. }
  408.  
  409. public class kas {
  410. public static void main(String[] args) {
  411. desifrator novy = new desifrator();
  412.  
  413. novy.menu();
  414.  
  415.  
  416. }
  417. }
  418.  

Syntax highlighting: