2 * This file is part of NeverNote
\r
3 * Copyright 2009 Randy Baumgarte
\r
5 * This file may be licensed under the terms of of the
\r
6 * GNU General Public License Version 2 (the ``GPL'').
\r
8 * Software distributed under the License is distributed
\r
9 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
\r
10 * express or implied. See the GPL for the specific language
\r
11 * governing rights and limitations.
\r
13 * You should have received a copy of the GPL along with this
\r
14 * program. If not, go to http://www.gnu.org/licenses/gpl.html
\r
15 * or write to the Free Software Foundation, Inc.,
\r
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
\r
19 package cx.fbn.nevernote.utilities;
\r
21 //RSA - Rivest, Shamir, & Adleman
\r
23 import java.io.InputStream;
\r
24 import java.io.OutputStream;
\r
25 import java.security.InvalidAlgorithmParameterException;
\r
26 import java.security.InvalidKeyException;
\r
27 import java.security.spec.AlgorithmParameterSpec;
\r
29 import javax.crypto.Cipher;
\r
30 import javax.crypto.CipherInputStream;
\r
31 import javax.crypto.CipherOutputStream;
\r
32 import javax.crypto.spec.IvParameterSpec;
\r
33 import javax.crypto.spec.SecretKeySpec;
\r
35 public class AESEncrypter
\r
37 private Cipher cipher;
\r
38 private String password;
\r
39 private String userid;
\r
40 private final SecretKeySpec skeySpec;
\r
41 private final AlgorithmParameterSpec paramSpec;
\r
43 public AESEncrypter()
\r
45 String key = "x331aq5wDQ8xO81v";
\r
46 skeySpec = new SecretKeySpec(key.getBytes(), "AES");
\r
47 password = new String("");
\r
48 userid = new String("");
\r
50 // Create an 8-byte initialization vector
\r
51 byte[] iv = new byte[]
\r
53 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
\r
56 paramSpec = new IvParameterSpec(iv);
\r
59 cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
\r
63 e.printStackTrace();
\r
67 public void setPassword(String s) {
\r
70 public String getPassword() {
\r
73 public void setUserid(String s) {
\r
76 public String getUserid() {
\r
80 public void encrypt(OutputStream out)
\r
82 // CBC requires an initialization vector
\r
84 cipher.init(Cipher.ENCRYPT_MODE, skeySpec, paramSpec);
\r
85 out = new CipherOutputStream(out, cipher);
\r
87 // String u = new String(userid +" " +password);
\r
88 StringBuffer u = new StringBuffer(1024);
\r
89 u.append("Userid:" +userid+ " " +password);
\r
90 for (int i=u.length(); i<128; i++)
\r
93 out.write(u.toString().getBytes());
\r
96 catch (java.io.IOException e)
\r
98 System.out.println("Encrypt i/o exception");
\r
99 } catch (InvalidKeyException e1) {
\r
100 e1.printStackTrace();
\r
101 } catch (InvalidAlgorithmParameterException e1) {
\r
102 e1.printStackTrace();
\r
106 public void decrypt(InputStream in)
\r
108 byte[] buf = new byte[1024];
\r
109 // CBC requires an initialization vector
\r
111 cipher.init(Cipher.DECRYPT_MODE, skeySpec, paramSpec);
\r
112 // Bytes read from in will be decrypted
\r
113 in = new CipherInputStream(in, cipher);
\r
114 if (in.read(buf) >= 0)
\r
116 String line = new String(buf);
\r
117 int offset = line.indexOf(" ");
\r
119 userid = line.substring(line.indexOf(":")+1, offset);
\r
120 password = line.substring(offset+1);
\r
121 password = password.trim();
\r
126 } catch (java.io.IOException e) {
\r
128 } catch (InvalidKeyException e1) {
\r
130 } catch (InvalidAlgorithmParameterException e1) {
\r