Advertisement
Guest User

bellaAAAA

a guest
Apr 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. package progettoADE;
  2.  
  3. import org.omg.CORBA.Object;
  4.  
  5. public final class Cifrario {
  6.  
  7. public class java {
  8.  
  9. }
  10.  
  11. private Cifrario() {
  12. }
  13.  
  14. public static byte[] algoritmoA(byte[] buffer, boolean dec) {
  15. adder(buffer, 0, 1, dec);
  16. return buffer;
  17. }
  18.  
  19. public static byte[] algoritmoB(byte[] buffer, boolean dec) {
  20. adder(buffer, 0, 2, dec);
  21. return buffer;
  22. }
  23.  
  24. public static byte[] algoritmoC(byte[] buffer, boolean dec) {
  25. adder(buffer, 1, 2, dec);
  26. return buffer;
  27. }
  28.  
  29. public static byte[] algoritmoD(byte[] buffer) {
  30. byte temp;
  31.  
  32. for (int i = 0; i < buffer.length / 2; i++) {
  33. temp = buffer[i];
  34. buffer[i] = buffer[buffer.length - 1 - i];
  35. buffer[buffer.length - 1 - i] = temp;
  36. }
  37.  
  38. return buffer;
  39. }
  40.  
  41. public static byte[] algoritmoE(byte[] buffer) {
  42. int x=17;
  43. int y=byteMakes(x);
  44. String pippo="017";
  45. int z=Integer.valueOf(pippo);
  46. System.out.print("porco dio");
  47. System.out.print(z);
  48.  
  49.  
  50. byte[] v = new byte[1000];
  51. int counter = 0;
  52.  
  53. for (int i = 0; i < buffer.length; i++) {
  54. if (buffer[i] != 0) {
  55. v[counter++] = buffer[i]; System.out.println(v[counter - 1]);
  56. v[counter++] = 45; System.out.println(v[counter - 1]);
  57. v[counter++] = (byte)(byteMakes(i)); System.out.println(v[counter - 1]);
  58. for (int j = i + 1; j < buffer.length; j++) {
  59. if (buffer[j] == buffer[i]) {
  60. v[counter++] = 45; System.out.println(v[counter- 1]);
  61. v[counter++] = (byte) (j+48); System.out.println(v[counter - 1]);
  62. buffer[j] = 0;
  63. }
  64. }
  65. v[counter++] = 32; System.out.println(v[counter - 1]);
  66. }
  67. }
  68.  
  69. return v;
  70. }
  71.  
  72. private static byte[] adder(byte[] buffer, int i, int k, boolean dec) {
  73. int c;
  74.  
  75. if(dec) c = -4;
  76. else c = 4;
  77.  
  78. for (; i < buffer.length; i = i + k)
  79. buffer[i] = (byte) ((buffer[i] + c) % 256);
  80.  
  81. return buffer;
  82. }
  83. private static byte[] stampaByte(int x){
  84. String myReturn = null ;
  85. String k = String.valueOf(x);
  86. String[] myWord = k.split("");
  87. for(int i = 0; i < myWord.length; i++){
  88. int number = Integer.valueOf(myWord[i]);
  89. number+=48;
  90. myWord[i]="0"+String.valueOf(number);
  91. myReturn+=myWord[i];
  92. }
  93.  
  94. return myReturn.getBytes();
  95. }
  96. private static int byteMakes(int x){
  97. int temp = 0;
  98. int m = 1;
  99. if(x<=9)
  100. return x+=48;
  101. if(x>9 && x<=99){
  102. for(int i=0;i<2;i++){
  103. temp=temp + ((x % 10)+48)*m;
  104. x=x/10;
  105. m*=1000;
  106. }
  107. }
  108. if(x>99 && x<=999){
  109. for(int i=0;i<3;i++){
  110. temp=temp + ((x % 10)+48)*m;
  111. x=x/10;
  112. m*=1000;
  113. }
  114. }
  115. return temp;
  116. }
  117. }
  118. ----------------------------------------
  119. package progettoADE;
  120.  
  121. import java.io.File;
  122. import java.io.FileInputStream;
  123. import java.io.FileOutputStream;
  124.  
  125. public class test {
  126.  
  127. public static void main(String[] args) {
  128. String input = "C:/temp/input.txt";
  129. String output = "C:/temp/output.txt";
  130. String parolaChiave = "C:/temp/parolaChiave.txt";
  131. boolean dec = false; //voglio decifrare?
  132.  
  133. byte[] buffer = toBytesArray(input);
  134. byte[] sequenza = toBytesArray(parolaChiave);
  135.  
  136. buffer = applicaSequenza(buffer, sequenza, dec);
  137. /*dec = true;
  138. sequenza = Cifrario.algoritmoD(sequenza);
  139. applicaSequenza(buffer, sequenza, dec);*/
  140.  
  141. toTextFile(buffer, output);
  142. }
  143.  
  144. public static byte[] applicaSequenza(byte[] buffer, byte[] sequenza, boolean dec) {
  145.  
  146. for(byte x : sequenza) {
  147. switch(x) {
  148. case 65: //A
  149. buffer = Cifrario.algoritmoA(buffer, dec);
  150. break;
  151. case 66: //B
  152. buffer = Cifrario.algoritmoB(buffer, dec);
  153. break;
  154. case 67: //C
  155. buffer = Cifrario.algoritmoC(buffer, dec);
  156. break;
  157. case 68: //D
  158. buffer = Cifrario.algoritmoD(buffer);
  159. break;
  160. case 69: //E
  161. buffer = Cifrario.algoritmoE(buffer);
  162. break;
  163. }
  164. }
  165.  
  166. return buffer;
  167. }
  168.  
  169. public static byte[] toBytesArray(String path) {
  170. File file = new File(path);
  171. byte[] bytesArray = new byte[(int) file.length()];
  172.  
  173. try (FileInputStream fis = new FileInputStream(file)) {
  174. fis.read(bytesArray);
  175. } catch (Exception e) {
  176. e.printStackTrace();
  177. }
  178.  
  179. return bytesArray;
  180. }
  181.  
  182. public static void toTextFile(byte[] bytesArray, String path) {
  183.  
  184. try (FileOutputStream fos = new FileOutputStream(path)) {
  185. fos.write(bytesArray);
  186. } catch (Exception e) {
  187. e.printStackTrace();
  188. }
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement