· 8 years ago · Oct 30, 2017, 05:30 PM
1import java.io.BufferedReader;
2import java.io.BufferedWriter;
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.FileReader;
6import java.io.FileWriter;
7import java.io.IOException;
8import java.io.PrintWriter;
9import java.nio.charset.StandardCharsets;
10import java.nio.file.Files;
11import java.nio.file.Paths;
12import java.util.Scanner;
13
14import javax.crypto.Cipher;
15import javax.crypto.spec.IvParameterSpec;
16import javax.crypto.spec.SecretKeySpec;
17
18public class CipherFeedback {
19
20 public byte[] key;
21 public String iv;
22 private static final String ALGORITHM = "AES/CTR/NoPadding"; //
23 String tekst_jawny;
24 SecretKeySpec secretKey;
25
26 public CipherFeedback(String klucz, String initVector)
27 {
28 key = klucz.getBytes(StandardCharsets.US_ASCII);
29 iv = initVector;
30 secretKey= new SecretKeySpec(key, "AES");
31 }
32
33 public String encrypt(String plainText) throws Exception
34 {
35 byte[] ivs = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF };
36 IvParameterSpec ivSpec = new IvParameterSpec(ivs);
37 Cipher cipherr = Cipher.getInstance(ALGORITHM);
38 cipherr.init(Cipher.ENCRYPT_MODE, secretKey,ivSpec);
39 byte[] crypted = cipherr.doFinal(plainText.getBytes());
40 return new String(crypted);
41 }
42
43 public String decrypt(String cipherText) throws Exception
44 {
45 byte[] ivs = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF };
46 IvParameterSpec ivSpec = new IvParameterSpec(ivs);
47 Cipher cipherr = Cipher.getInstance(ALGORITHM);
48
49 cipherr.init(Cipher.ENCRYPT_MODE, secretKey,ivSpec);
50 byte[] output = cipherr.doFinal(cipherText.getBytes());
51 return new String(output);
52 }
53 public byte[] wykonajXor(byte[] p, byte[] q)
54 {
55 for( int i = 0; i < q.length; i++)
56 {
57 q[i] = (byte) (q[i] ^ p[i]);
58 }
59 return q;
60
61 }
62 public String encryptWithCFB(String tekst_jaw) throws Exception
63 {
64 byte[] tablica_bajtow_tekstu_jawnego = tekst_jaw.getBytes(StandardCharsets.US_ASCII);
65 String cipherIV = encrypt(iv);
66
67 byte[] tablica_bajtow_iv = cipherIV.getBytes(StandardCharsets.US_ASCII);
68 https://pastebin.com/r01juKFw
69 tablica_bajtow_iv = wykonajXor(tablica_bajtow_tekstu_jawnego, tablica_bajtow_iv);
70 String wynik = new String(tablica_bajtow_iv);
71 iv = wynik;
72 return wynik;
73 }
74
75 public String decryptWithCFB(String tekst_jaw) throws Exception
76 {
77 byte[] tablica_bajtow_tekstu_jawnego = tekst_jaw.getBytes(StandardCharsets.US_ASCII);
78 String cipherIV = encrypt(iv);
79
80 byte[] tablica_bajtow_iv = cipherIV.getBytes(StandardCharsets.US_ASCII);
81 String wynikiv=new String(tablica_bajtow_tekstu_jawnego);
82 iv = wynikiv;
83 tablica_bajtow_iv = wykonajXor(tablica_bajtow_tekstu_jawnego, tablica_bajtow_iv);
84 String wynik = new String(tablica_bajtow_iv);
85
86 return wynik;
87 }
88 public String read(String filename) throws IOException
89 {
90 String content;
91 content = new String(Files.readAllBytes(Paths.get(filename)));
92 return content;
93 }
94 public void szyfrujPlik() throws Exception
95 {
96 CipherFeedback cfb = new CipherFeedback("stereokomparator", "qwertyuiopasdfgh");
97 cfb.iv="qwertyuiopasdfgh";
98
99 BufferedWriter bw = null;
100 FileWriter fw = null;
101
102
103 fw = new FileWriter("2.txt");
104 bw = new BufferedWriter(fw);
105 try {
106
107
108 cfb.tekst_jawny = read("20mb.txt");
109 //System.out.println(cfb.tekst_jawny.length());
110 for(int i=0;i<(cfb.tekst_jawny.length()/16);i++)
111 {
112 String wiadomosc = cfb.encrypt(cfb.tekst_jawny.substring(0+(16*i),16+(16*i)));
113 //System.out.println(wiadomosc);
114 bw.write(wiadomosc);
115 }
116
117
118 }
119 finally
120 {
121
122
123 }
124 bw.close();
125
126 }
127 public void deszyfrujPlik() throws Exception
128 {
129 CipherFeedback cfb = new CipherFeedback("stereokomparator", "qwertyuiopasdfgh");
130 cfb.iv="qwertyuiopasdfgh";
131
132 BufferedWriter bw = null;
133 FileWriter fw = null;
134
135
136 fw = new FileWriter("1.txt");
137 bw = new BufferedWriter(fw);
138 try {
139
140
141 cfb.tekst_jawny = read("2.txt");
142 System.out.println(cfb.tekst_jawny.length());
143 for(int i=0;i<(cfb.tekst_jawny.length()/16);i++)
144 {
145 String wiadomosc= cfb.decrypt(cfb.tekst_jawny.substring(0+(16*i),16+(16*i)));
146 //System.out.println(wiadomosc);
147 bw.write(wiadomosc);
148 }
149
150
151 }
152 finally
153 {
154
155
156 }
157 bw.close();
158
159 }
160
161
162 public static void main(String[] args) throws Exception {
163 CipherFeedback cfb = new CipherFeedback("stereokomparator","qwertyuiopasdfgh");
164 long startTime = System.nanoTime();
165
166 cfb.deszyfrujPlik();
167 long endTime = System.nanoTime();
168
169 long duration = (endTime - startTime)/1000000;
170 System.out.println(duration+ " ms");
171 //cfb.deszyfrujPlik();
172 try
173 {
174// cfb.tekst_jawny = "szymonchojnowski";
175// String wiadomosc = null;
176// for(int i=0;i<(cfb.tekst_jawny.length()/16);i++)
177// {
178// wiadomosc= cfb.encryptWithCFB(cfb.tekst_jawny.substring(0+(16*i),16+(16*i)));
179// System.out.println(wiadomosc);
180// }
181//
182// cfb.iv = "qwertyuiopasdfgh";
183// for(int i=0;i<(cfb.tekst_jawny.length()/16);i++)
184// {
185// String wiadomosc2 = cfb.encryptWithCFB(wiadomosc.substring(0+(16*i),16+(16*i)));
186// System.out.println(wiadomosc2);
187// }
188 System.out.println("success");
189 }
190 catch (Exception e) {
191 // TODO Auto-generated catch block
192 e.printStackTrace();
193 }
194
195 }
196
197}