OSDN Git Service

new repository
[stew/Stew4.git] / src / net / argius / stew / CipherPassword.java
1 package net.argius.stew;
2
3 import java.io.*;
4
5 import javax.crypto.*;
6
7 /**
8  * A skeletal implementation of the Password interface using ciphers.
9  */
10 public abstract class CipherPassword implements Password {
11
12     private static final Logger log = Logger.getLogger(CipherPassword.class);
13
14     private static String secretKey = "";
15
16     private String transformedString;
17
18     @Override
19     public final String getTransformedString() {
20         if (transformedString != null) {
21             return transformedString;
22         }
23         return "";
24     }
25
26     @Override
27     public final void setTransformedString(String transformedString) {
28         if (transformedString != null) {
29             this.transformedString = transformedString;
30         }
31     }
32
33     @Override
34     public final String getRawString() {
35         if (transformedString != null) {
36             return decrypt(transformedString);
37         }
38         return "";
39     }
40
41     @Override
42     public final void setRawString(String rowString) {
43         if (rowString != null) {
44             this.transformedString = encrypt(rowString);
45         }
46     }
47
48     @Override
49     public final boolean hasPassword() {
50         return transformedString != null;
51     }
52
53     /**
54      * Sets a secret key.
55      * @param secretKey
56      */
57     public static void setSecretKey(String secretKey) {
58         assert secretKey != null && secretKey.length() > 0;
59         CipherPassword.secretKey = secretKey;
60     }
61
62     /**
63      * Encrypts a password.
64      * @param rowString
65      * @return the encrypted password
66      */
67     private String encrypt(String rowString) {
68         try {
69             Cipher cipher = getCipherInstance(secretKey, Cipher.ENCRYPT_MODE);
70             byte[] encrypted = cipher.doFinal(rowString.getBytes());
71             return toHexString(encrypted);
72         } catch (Exception ex) {
73             log.warn(ex);
74             return "";
75         }
76     }
77
78     /**
79      * Decrypts a password.
80      * @param cryptedString
81      * @return the decrypted password
82      */
83     private String decrypt(String cryptedString) {
84         try {
85             Cipher cipher = getCipherInstance(secretKey, Cipher.DECRYPT_MODE);
86             byte[] decrypted = cipher.doFinal(toBytes(cryptedString));
87             return new String(decrypted);
88         } catch (Exception ex) {
89             log.warn(ex);
90             return "";
91         }
92     }
93
94     private static String toHexString(byte[] bytes) {
95         StringBuffer buffer = new StringBuffer();
96         for (byte b : bytes) {
97             buffer.append(String.format("%02X", b & 0xFF));
98         }
99         return buffer.toString();
100     }
101
102     private static byte[] toBytes(String hexString) {
103         ByteArrayOutputStream bos = new ByteArrayOutputStream();
104         for (int i = 0; i < hexString.length(); i += 2) {
105             String s = hexString.substring(i, i + 2);
106             bos.write(Integer.parseInt(s, 16));
107         }
108         return bos.toByteArray();
109     }
110
111     /**
112      * Gets a instance of Cipher class.
113      * @param key
114      * @param mode Cipher.DECRYPT_MODE or Cipher.DECRYPT_MODE
115      * @return the instance of Cipher class
116      * @see Cipher
117      */
118     protected abstract Cipher getCipherInstance(String key, int mode);
119
120 }