View Javadoc

1   package rydeen.plugins.strenc;
2   
3   import java.security.InvalidKeyException;
4   import java.security.NoSuchAlgorithmException;
5   import java.security.Provider;
6   import java.security.Security;
7   
8   import javax.crypto.BadPaddingException;
9   import javax.crypto.Cipher;
10  import javax.crypto.IllegalBlockSizeException;
11  import javax.crypto.KeyGenerator;
12  import javax.crypto.NoSuchPaddingException;
13  import javax.crypto.SecretKey;
14  import javax.crypto.spec.SecretKeySpec;
15  
16  import rydeen.Arguments;
17  import rydeen.ProcessorException;
18  import rydeen.ProcessorInitializeException;
19  
20  /**
21   * 暗号化を管理するためのクラスです.
22   *
23   * @author Haruaki Tamada
24   */
25  public class EncryptManager{
26      private static final int DEFAULT_KEY_LENGTH = 128;
27      private String algorithm;
28      private Cipher cipher;
29      private String decryptClassName;
30      private String decryptMethodName;
31      private byte[] key;
32      private int keyLength;
33      private Provider provider;
34  
35      /**
36       * 与えられたパラメータで暗号化を管理するクラスのオブジェクトを作成します.
37       * 指定可能なパラメータは{@link StringEncryptObfuscatorService#createDefaultArguments}
38       * を参照してください.
39       */
40      public EncryptManager(Arguments args) throws ProcessorException{
41          this.algorithm = args.getValue("algorithm");
42  
43          String keyLengthString = args.getValue("keyLength");
44          if(keyLengthString != null){
45              try{
46                  keyLength = Integer.parseInt(keyLengthString);
47              } catch(NumberFormatException e){
48                  keyLength = DEFAULT_KEY_LENGTH;
49              }
50          }
51          String keyString = args.getValue("key");
52          if(keyString == null || keyString.trim().equals("")){
53              key = generateKey(keyLength);
54          }
55          else{
56              key = normalizeGivenKey(keyString, keyLength);
57          }
58  
59          String providerString = args.getValue("provider");
60          if(providerString == null || providerString.trim().equals("")){
61              provider = Security.getProvider(providerString);
62          }
63  
64          initCipher();
65      }
66  
67      public Provider getProvider(){
68          return provider;
69      }
70  
71      public String getAlgorithm(){
72          return algorithm;
73      }
74  
75      public String getKeyFieldName(){
76          return "__decryptKey";
77      }
78  
79      public byte[] getKey(){
80          byte[] newKey = new byte[key.length];
81          System.arraycopy(key, 0, newKey, 0, key.length);
82          return newKey;
83      }
84  
85      public String encrypt(String string){
86          return encodeHex(string.getBytes());
87      }
88  
89      public byte[] encode(byte[] data){
90          try{
91              return cipher.doFinal(data);
92          } catch(IllegalBlockSizeException e){
93          } catch(BadPaddingException e){
94          }
95          return null;
96      }
97  
98      private String encodeHex(byte[] data){
99          byte[] encodedData = encode(data);
100         StringBuilder sb = new StringBuilder();
101         for(int i = 0; i < encodedData.length; i++){
102             int value = encodedData[i] & 0xff;
103             if(value < 0x10) sb.append('0');
104             sb.append(Integer.toHexString(value));
105         }
106         return new String(sb);
107     }
108 
109     private byte[] generateKey(int keyLength) throws ProcessorException{
110         try{
111             KeyGenerator keyGen = KeyGenerator.getInstance(algorithm);
112             keyGen.init(keyLength);
113             SecretKey skey = keyGen.generateKey();
114             return skey.getEncoded();
115         } catch(NoSuchAlgorithmException e){
116             throw new ProcessorInitializeException(e);
117         }
118     }
119 
120     public String getDecryptClassName(){
121         return decryptClassName;
122     }
123 
124     public String getDecryptMethodName(){
125         return decryptMethodName;
126     }
127 
128     private void initCipher() throws ProcessorException{
129         try{
130             SecretKeySpec skeySpec = new SecretKeySpec(key, algorithm);
131             if(provider != null){
132                 cipher = Cipher.getInstance(algorithm, provider);
133             }
134             else{
135                 cipher = Cipher.getInstance(algorithm);
136             }
137             cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
138 
139         } catch(NoSuchAlgorithmException e){
140             throw new ProcessorInitializeException(e);
141         } catch(NoSuchPaddingException e){
142             throw new ProcessorInitializeException(e);
143         } catch(InvalidKeyException e){
144             throw new ProcessorInitializeException(e);
145         }
146     }
147 
148     private byte[] normalizeGivenKey(String keyString, int keyLength) throws ProcessorException{
149         byte[] key = keyString.getBytes();
150         byte[] skey = new byte[keyLength];
151         int minLength = skey.length;
152         if(skey.length > key.length){
153             minLength = key.length;
154         }
155         System.arraycopy(key, 0, skey, 0, minLength);
156         return skey;
157     }
158 
159     public void setDecryptClassName(String decryptClassName){
160         this.decryptClassName = decryptClassName;
161     }
162 
163     public void setDecryptMethodName(String decryptMethodName){
164         this.decryptMethodName = decryptMethodName;
165     }
166 }