01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.crypto;
23:
24: import java.io.InputStream;
25: import java.io.IOException;
26: import java.io.OutputStream;
27: import java.net.Socket;
28: import java.util.Arrays;
29: import javax.crypto.Cipher;
30: import javax.crypto.CipherInputStream;
31: import javax.crypto.CipherOutputStream;
32: import javax.crypto.spec.IvParameterSpec;
33: import javax.crypto.SecretKey;
34:
35: /**
36: *
37: * @author Scott.Stark@jboss.org
38: */
39: public class CipherSocket extends Socket {
40: private Cipher cipher;
41: private Socket delegate;
42: String algorithm;
43: SecretKey key;
44:
45: /** Creates a new instance of CipherSocket */
46: public CipherSocket(String host, int port, String algorithm,
47: SecretKey key) throws IOException {
48: super (host, port);
49: this .algorithm = algorithm;
50: this .key = key;
51: }
52:
53: public CipherSocket(Socket delegate, String algorithm, SecretKey key)
54: throws IOException {
55: this .delegate = delegate;
56: this .algorithm = algorithm;
57: this .key = key;
58: }
59:
60: public InputStream getInputStream() throws IOException {
61: InputStream is = delegate == null ? super .getInputStream()
62: : delegate.getInputStream();
63: Cipher cipher = null;
64: try {
65: cipher = Cipher.getInstance(algorithm);
66: int size = cipher.getBlockSize();
67: byte[] tmp = new byte[size];
68: Arrays.fill(tmp, (byte) 15);
69: IvParameterSpec iv = new IvParameterSpec(tmp);
70: cipher.init(Cipher.DECRYPT_MODE, key, iv);
71: } catch (Exception e) {
72: e.printStackTrace();
73: throw new IOException("Failed to init cipher: "
74: + e.getMessage());
75: }
76: CipherInputStream cis = new CipherInputStream(is, cipher);
77: return cis;
78: }
79:
80: public OutputStream getOutputStream() throws IOException {
81: OutputStream os = delegate == null ? super .getOutputStream()
82: : delegate.getOutputStream();
83: Cipher cipher = null;
84: try {
85: cipher = Cipher.getInstance(algorithm);
86: int size = cipher.getBlockSize();
87: byte[] tmp = new byte[size];
88: Arrays.fill(tmp, (byte) 15);
89: IvParameterSpec iv = new IvParameterSpec(tmp);
90: cipher.init(Cipher.ENCRYPT_MODE, key, iv);
91: } catch (Exception e) {
92: throw new IOException("Failed to init cipher: "
93: + e.getMessage());
94: }
95: CipherOutputStream cos = new CipherOutputStream(os, cipher);
96: return cos;
97: }
98: }
|