Biblioteca Java - Blame information for rev 3
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
3 | mihai | 1 | /* |
2 | * FileED.java | ||
3 | */ | ||
4 | package lab.scd.encrypt.des; | ||
5 | |||
6 | import java.io.*; | ||
7 | |||
8 | import javax.crypto.SecretKey; | ||
9 | |||
10 | /** | ||
11 | * Class created by @author Mihai HULEA. | ||
12 | * | ||
13 | * This class is part of the laborator4_2crypto project. | ||
14 | * | ||
15 | */ | ||
16 | public class FileED { | ||
17 | |||
18 | DESEncDec ed; | ||
19 | |||
20 | public FileED(String filekey){ | ||
21 | byte[] b = getBytesFromFile(filekey); | ||
22 | ed = new DESEncDec(DESEncDec.loadKey(b)); | ||
23 | } | ||
24 | |||
25 | public FileED(){ | ||
26 | ed = new DESEncDec(); | ||
27 | } | ||
28 | |||
29 | /** | ||
30 | * Criptare continut fisier sursa si salvare in alt fisier. | ||
31 | * @param source | ||
32 | * @param dest | ||
33 | */ | ||
34 | public void encriptFile(String source, String dest){ | ||
35 | String content = this.readFile(source); | ||
36 | content = ed.encrypt(content); | ||
37 | this.writeToFile(dest,content); | ||
38 | } | ||
39 | |||
40 | public void decriptFile(String source, String dest){ | ||
41 | String content = this.readFile(source); | ||
42 | content = ed.decrypt(content); | ||
43 | this.writeToFile(dest,content); | ||
44 | } | ||
45 | |||
46 | /** | ||
47 | * @param string | ||
48 | */ | ||
49 | private void saveKey(String file) { | ||
50 | String k = new String(ed.getKeyBytes()); | ||
51 | this.writeToFile(file,k); | ||
52 | } | ||
53 | |||
54 | String readFile(String filename){ | ||
55 | String content = null; | ||
56 | try { | ||
57 | BufferedReader bf = new BufferedReader(new FileReader(filename)); | ||
58 | StringBuffer c = new StringBuffer(100); | ||
59 | String line = bf.readLine(); | ||
60 | while(line!= null){ | ||
61 | c.append(line); | ||
62 | line = bf.readLine(); | ||
63 | } | ||
64 | content = c.toString(); | ||
65 | } catch (Exception e) { | ||
66 | |||
67 | e.printStackTrace(); | ||
68 | System.exit(0); | ||
69 | } | ||
70 | return content; | ||
71 | } | ||
72 | |||
73 | void writeToFile(String filename, String content){ | ||
74 | |||
75 | try { | ||
76 | FileWriter fw = new FileWriter(new File(filename)); | ||
77 | System.out.println(">"+content); | ||
78 | fw.write(content); | ||
79 | fw.close(); | ||
80 | } catch (IOException e) { | ||
81 | |||
82 | e.printStackTrace(); | ||
83 | System.exit(0); | ||
84 | } | ||
85 | } | ||
86 | |||
87 | // Returns the contents of the file in a byte array. | ||
88 | public static byte[] getBytesFromFile(String file){ | ||
89 | |||
90 | byte[] bytes=null; | ||
91 | |||
92 | try{ | ||
93 | |||
94 | |||
95 | InputStream is = new FileInputStream(file); | ||
96 | |||
97 | // Get the size of the file | ||
98 | long length = file.length(); | ||
99 | |||
100 | // You cannot create an array using a long type. | ||
101 | // It needs to be an int type. | ||
102 | // Before converting to an int type, check | ||
103 | // to ensure that file is not larger than Integer.MAX_VALUE. | ||
104 | if (length > Integer.MAX_VALUE) { | ||
105 | // File is too large | ||
106 | } | ||
107 | |||
108 | // Create the byte array to hold the data | ||
109 | bytes = new byte[(int)length]; | ||
110 | |||
111 | // Read in the bytes | ||
112 | int offset = 0; | ||
113 | int numRead = 0; | ||
114 | while (offset < bytes.length | ||
115 | && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { | ||
116 | offset += numRead; | ||
117 | } | ||
118 | |||
119 | // Ensure all the bytes have been read in | ||
120 | if (offset < bytes.length) { | ||
121 | throw new IOException("Could not completely read file "+file); | ||
122 | } | ||
123 | |||
124 | // Close the input stream and return bytes | ||
125 | is.close(); | ||
126 | }catch(Exception e){ | ||
127 | |||
128 | e.printStackTrace(); | ||
129 | System.exit(0); | ||
130 | } | ||
131 | return bytes; | ||
132 | } | ||
133 | |||
134 | public static void main(String[] args) { | ||
135 | |||
136 | /** | ||
137 | * Foloseste constructorul implicit. Va fi generata automat o cheie. | ||
138 | */ | ||
139 | FileED ed = new FileED(); | ||
140 | |||
141 | System.out.println("Genereaza fisier text pentru teste."); | ||
142 | ed.writeToFile("test.txt","Continut fisier.Fisier de test."); | ||
143 | |||
144 | System.out.println("Criptare fisier text"); | ||
145 | ed.encriptFile("test.txt","test.enc"); | ||
146 | |||
147 | System.out.println("Decriptare fisie"); | ||
148 | ed.decriptFile("test.enc","test.dec"); | ||
149 | |||
150 | System.out.println("Salveaza cheia"); | ||
151 | ed.saveKey("test.key"); | ||
152 | |||
153 | //----- | ||
154 | |||
155 | /** | ||
156 | * Foloseste o cheie existenta pentru decriptarea unui fisier | ||
157 | */ | ||
158 | FileED ed2 = new FileED("test.key"); | ||
159 | |||
160 | ed2.decriptFile("test.enc","test.dec2"); | ||
161 | |||
162 | |||
163 | } | ||
164 | |||
165 | |||
166 | } |