OSDN Git Service

Correct problem parsing notes where carriage returns did not separate some XML lines...
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / utilities / AESEncrypter.java
1 /*\r
2  * This file is part of NeverNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * \r
5  * This file may be licensed under the terms of of the\r
6  * GNU General Public License Version 2 (the ``GPL'').\r
7  *\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
12  *\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
17  *\r
18 */\r
19 package cx.fbn.nevernote.utilities;\r
20 \r
21 //RSA - Rivest, Shamir, & Adleman\r
22 \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
28 \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
34  \r
35 public class AESEncrypter\r
36 {\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
42         \r
43         public AESEncrypter()\r
44         {\r
45                 String key = "x331aq5wDQ8xO81v";\r
46                 skeySpec = new SecretKeySpec(key.getBytes(), "AES");\r
47                 password = new String("");\r
48                 userid = new String("");\r
49                 \r
50                 // Create an 8-byte initialization vector\r
51                 byte[] iv = new byte[]\r
52                 {\r
53                         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f\r
54                 };\r
55                 \r
56                 paramSpec = new IvParameterSpec(iv);\r
57                 try\r
58                 {\r
59                         cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");\r
60                 }\r
61                 catch (Exception e)\r
62                 {\r
63                         e.printStackTrace();\r
64                 }\r
65         }\r
66                 \r
67         public void setPassword(String s) {\r
68                 password = s;\r
69         }\r
70         public String getPassword() {\r
71                 return password;\r
72         }\r
73         public void setUserid(String s) {\r
74                 userid = s;\r
75         }\r
76         public String getUserid() {\r
77                 return userid;\r
78         }\r
79         \r
80         public void encrypt(OutputStream out)\r
81         {\r
82                 // CBC requires an initialization vector\r
83                 try {\r
84                         cipher.init(Cipher.ENCRYPT_MODE, skeySpec, paramSpec);\r
85                         out = new CipherOutputStream(out, cipher);\r
86 \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
91                                 u.append('\0');\r
92 \r
93                         out.write(u.toString().getBytes());\r
94                         out.close();\r
95                 }\r
96                 catch (java.io.IOException e)\r
97                 {\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
103                 }\r
104         }\r
105         \r
106         public void decrypt(InputStream in)\r
107         {\r
108                 byte[] buf = new byte[1024];\r
109                 // CBC requires an initialization vector\r
110                 try {\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
115                         {\r
116                                 String line = new String(buf);\r
117                                 int offset = line.indexOf(" ");\r
118                                 if (offset > 0) {\r
119                                         userid = line.substring(line.indexOf(":")+1, offset);\r
120                                         password = line.substring(offset+1);\r
121                                         password = password.trim();\r
122                                 }\r
123                                 \r
124                         }\r
125                         in.close();\r
126                 } catch (java.io.IOException e) {\r
127                         return;\r
128                 } catch (InvalidKeyException e1) {\r
129                         return; \r
130                 } catch (InvalidAlgorithmParameterException e1) {\r
131                         return;\r
132                 }\r
133         }\r
134 }\r