OSDN Git Service

Change authentication from userid/password to oauth
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / utilities / AESEncrypter.java
1 /*\r
2  * This file is part of NixNote \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 string;\r
39         private final SecretKeySpec skeySpec;\r
40         private final AlgorithmParameterSpec paramSpec;\r
41         \r
42         public AESEncrypter()\r
43         {\r
44                 String key = "x331aq5wDQ8xO81v";\r
45                 skeySpec = new SecretKeySpec(key.getBytes(), "AES");\r
46                 string = new String("");\r
47                 \r
48                 // Create an 8-byte initialization vector\r
49                 byte[] iv = new byte[]\r
50                 {\r
51                         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f\r
52                 };\r
53                 \r
54                 paramSpec = new IvParameterSpec(iv);\r
55                 try\r
56                 {\r
57                         cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");\r
58                 }\r
59                 catch (Exception e)\r
60                 {\r
61                         e.printStackTrace();\r
62                 }\r
63         }\r
64         public void setString(String s) {\r
65                 string = s;\r
66         }\r
67         public String getString() {\r
68                 return string;\r
69         }\r
70         \r
71         public void encrypt(OutputStream out)\r
72         {\r
73                 // CBC requires an initialization vector\r
74                 try {\r
75                         cipher.init(Cipher.ENCRYPT_MODE, skeySpec, paramSpec);\r
76                         out = new CipherOutputStream(out, cipher);\r
77 \r
78 //                      String u = new String(userid +" " +password);\r
79                         StringBuffer u = new StringBuffer(1024);\r
80                         u.append(string);\r
81                         for (int i=u.length(); i<128; i++)\r
82                                 u.append('\0');\r
83 \r
84                         out.write(u.toString().getBytes());\r
85                         out.close();\r
86                 }\r
87                 catch (java.io.IOException e)\r
88                 {\r
89                         System.out.println("Encrypt i/o exception");\r
90                 } catch (InvalidKeyException e1) {\r
91                         e1.printStackTrace();\r
92                 } catch (InvalidAlgorithmParameterException e1) {\r
93                         e1.printStackTrace();\r
94                 }\r
95         }\r
96         \r
97         public void decrypt(InputStream in)\r
98         {\r
99                 byte[] buf = new byte[1024];\r
100                 // CBC requires an initialization vector\r
101                 try {\r
102                         cipher.init(Cipher.DECRYPT_MODE, skeySpec, paramSpec);\r
103                         // Bytes read from in will be decrypted\r
104                         in = new CipherInputStream(in, cipher);\r
105                         if (in.read(buf) >= 0)\r
106                         {\r
107                                 string = new String(buf);       \r
108                         }\r
109                         in.close();\r
110                 } catch (java.io.IOException e) {\r
111                         return;\r
112                 } catch (InvalidKeyException e1) {\r
113                         return; \r
114                 } catch (InvalidAlgorithmParameterException e1) {\r
115                         return;\r
116                 }\r
117         }\r
118 }\r