KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > io > RaesFileUtils


1 /*
2  * RaesFileUtils.java
3  *
4  * Created on 16. Dezember 2005, 16:34
5  */

6 /*
7  * Copyright 2005 Schlichtherle IT Services
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package de.schlichtherle.io;
23
24 import de.schlichtherle.crypto.io.raes.KeyManagerRaesParameters;
25 import de.schlichtherle.crypto.io.raes.RaesParameters;
26 import de.schlichtherle.key.KeyManager;
27 import de.schlichtherle.crypto.io.raes.RaesOutputStream;
28 import de.schlichtherle.crypto.io.raes.Type0RaesParameters;
29 import de.schlichtherle.crypto.io.raes.RaesReadOnlyFile;
30 import de.schlichtherle.io.rof.ReadOnlyFileInputStream;
31
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.io.OutputStream JavaDoc;
35
36 /**
37  * Provides RAES file utility methods like encryption or decryption of
38  * arbitrary files into and from RAES files.
39  *
40  * @author Christian Schlichtherle
41  * @version @version@
42  */

43 public class RaesFileUtils {
44
45     /**
46      * Encrypts the given plain file to the given RAES file.
47      * This version uses the default ArchiveDetector to detect any archive
48      * files in its parent directory path except the files themselves, which
49      * are not recognized as archive files.
50      */

51     public static void encrypt(
52             final String JavaDoc plainFilePath,
53             final String JavaDoc raesFilePath)
54     throws IOException JavaDoc {
55         encrypt(plainFilePath, raesFilePath, File.getDefaultArchiveDetector());
56     }
57
58     /**
59      * Encrypts the given plain file to the given RAES file,
60      * using the provided ArchiveDetector to detect any archive files in its
61      * parent directory path except the files themselves, which are not
62      * recognized as archive files.
63      */

64     public static void encrypt(
65             final String JavaDoc plainFilePath,
66             final String JavaDoc raesFilePath,
67             final ArchiveDetector archiveDetector)
68     throws IOException JavaDoc {
69         final File plainFile = createNonArchiveFile(plainFilePath, archiveDetector);
70         final InputStream in = archiveDetector.createFileInputStream(plainFile);
71         final RaesOutputStream out;
72         try {
73             final File raesFile = createNonArchiveFile(raesFilePath, archiveDetector);
74             final RaesParameters params = new KeyManagerRaesParameters(
75                     raesFile.getCanonicalPath()); // use canonical path name
76
out = RaesOutputStream.getInstance(
77                     archiveDetector.createFileOutputStream(raesFile, false),
78                     params);
79         } catch (IOException JavaDoc failure) {
80             in.close();
81             throw failure;
82         }
83         File.cp(in, out);
84     }
85
86     /**
87      * Decrypts the given RAES file to the given plain file.
88      * This version uses the default ArchiveDetector to detect any archive
89      * files in its parent directory path except the files themselves, which
90      * are not recognized as archive files.
91      */

92     public static void decrypt(
93             final String JavaDoc raesFilePath,
94             final String JavaDoc plainFilePath,
95             final boolean strongAuthentication)
96     throws IOException JavaDoc {
97         decrypt(raesFilePath, plainFilePath, strongAuthentication,
98                 File.getDefaultArchiveDetector());
99     }
100
101     /**
102      * Decrypts the given RAES file to the given plain file,
103      * using the provided ArchiveDetector to detect any archvie files in its
104      * parent directory path except the files themselves, which are not
105      * recognized as archive files.
106      *
107      * @param strongAuthentication If this is <tt>true</tt>, the whole
108      * contents of encrypted file get authenticated, which can be a
109      * time consuming operation.
110      * Otherwise, only the key/password and the file length get
111      * authenticated.
112      */

113     public static void decrypt(
114             final String JavaDoc raesFilePath,
115             final String JavaDoc plainFilePath,
116             final boolean strongAuthentication,
117             final ArchiveDetector archiveDetector)
118     throws IOException JavaDoc {
119         final File raesFile = createNonArchiveFile(raesFilePath, archiveDetector);
120         final RaesParameters params = new KeyManagerRaesParameters(
121                 raesFile.getCanonicalPath()); // use canonical path name
122
final RaesReadOnlyFile rrof = RaesReadOnlyFile.getInstance(
123                 raesFile, params);
124         final InputStream in;
125         final OutputStream out;
126         try {
127             if (strongAuthentication)
128                 rrof.authenticate();
129             in = new ReadOnlyFileInputStream(rrof);
130             final File plainFile = createNonArchiveFile(plainFilePath, archiveDetector);
131             out = archiveDetector.createFileOutputStream(plainFile, false);
132         } catch (IOException JavaDoc failure) {
133             rrof.close();
134             throw failure;
135         }
136         File.cp(in, out);
137     }
138
139     /**
140      * Creates a file object which uses the provided ArchiveDetector,
141      * but does not recognize its own pathname as an archive file.
142      * Please note that this method just creates a file object,
143      * and does not actually touch the file system.
144      */

145     private static final File createNonArchiveFile(
146             final String JavaDoc path,
147             final ArchiveDetector archiveDetector) {
148         final File file = archiveDetector.createFile(path);
149         return ArchiveDetector.NULL.createFile(file.getParentFile(), file.getName());
150     }
151
152     /** You cannot instantiate this class. */
153     protected RaesFileUtils() {
154     }
155 }
156
Popular Tags