001    /*
002     * Copyright (c) 2009 The openGion Project.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013     * either express or implied. See the License for the specific language
014     * governing permissions and limitations under the License.
015     */
016    package org.opengion.fukurou.security;
017    
018    
019    import javax.crypto.spec.SecretKeySpec;
020    import javax.crypto.Cipher;
021    
022    import java.security.MessageDigest;
023    import java.security.NoSuchAlgorithmException;
024    import java.nio.charset.Charset;                // 5.5.2.6 (2012/05/25)
025    
026    /**
027     * HybsCryptography は、セキュリ?強化?為の Hybs独自の暗号化クラスです?
028     *
029     * こ?クラスは、暗号化キーを受け取り?それに基づ?暗号?復号化を行います?
030     * ここでの暗号化?、秘?ー方式でバイト文字?に変換されたものを??6?アスキー?に
031     * 直して、扱って?す?よって、暗号?復号化?に、文字?として扱?とが可能です?
032     *
033     * @og.rev 4.0.0.0 (2005/08/31) 新規追?
034     * @og.group ライセンス管?
035     *
036     * @version  4.0
037     * @author   Kazuhiko Hasegawa
038     * @since    JDK5.0,
039     */
040    public final class HybsCryptography {
041            private final SecretKeySpec sksSpec ;
042            private static final String CIPHER_TYPE = "Blowfish" ;
043    
044            /**
045             * プラ?フォー?存??ォルト? Charset です?
046             * プラ?フォー?存?を?慮する場合?エンコード指定で作?しておく事をお勧めします?
047             *
048             * @og.rev 5.5.2.6 (2012/05/25) findbugs対?
049             */
050            private static final Charset DEFAULT_CHARSET = Charset.defaultCharset() ;
051    
052            // 注意:秘?ーは??倍数でな??な??
053            private static final String HYBS_CRYPT_KEY = "2a5a88891d37ae59" ;
054    
055            /**
056             * ?設定?秘?を使用して?暗号化を行うオブジェクトを構築します?
057             * ここでの暗号化?、Java標準?セキュリ?パッケージを使用して?す?
058             */
059            public HybsCryptography() {
060    //              sksSpec = new SecretKeySpec( HYBS_CRYPT_KEY.getBytes(), CIPHER_TYPE );
061                    sksSpec = new SecretKeySpec( HYBS_CRYPT_KEY.getBytes( DEFAULT_CHARSET ), CIPHER_TYPE );         // 5.5.2.6 (2012/05/25) findbugs対?
062            }
063    
064            /**
065             * 秘?の??を受け取って?暗号化を行うオブジェクトを構築します?
066             * ここでの暗号化?、Java標準?セキュリ?パッケージを使用して?す?
067             * 秘?のサイズを?8 の倍数 (32 以?448 以? にする?があります?
068             *
069             * @param       cryptKey        暗号化を行う秘?
070             */
071            public HybsCryptography( final String cryptKey ) {
072    //              sksSpec = new SecretKeySpec( cryptKey.getBytes(), CIPHER_TYPE );
073                    sksSpec = new SecretKeySpec( cryptKey.getBytes( DEFAULT_CHARSET ), CIPHER_TYPE );               // 5.5.2.6 (2012/05/25) findbugs対?
074            }
075    
076            /**
077             * セキュリ?カラ??DBTyepに対してHybs独自の暗号化を行います?
078             * 暗号化された??タは??常 byte ?ですが?6?数アスキー??に変換
079             * したも?を返します?
080             * こ?暗号化では、引数?null の場合?、ゼロ??を返します?
081             *
082             * @param       org     暗号化を行う????
083             *
084             * @return      暗号化された??(HEXADECIMAL?
085             */
086            public String encrypt( final String org ) {
087                    if( org == null || org.length() == 0 ) { return ""; }
088    
089                    try {
090                            Cipher cipher = Cipher.getInstance( CIPHER_TYPE );
091                            cipher.init( Cipher.ENCRYPT_MODE, sksSpec );
092    //                      byte[] encrypted = cipher.doFinal( org.getBytes() );
093                            byte[] encrypted = cipher.doFinal( org.getBytes( DEFAULT_CHARSET ) );           // 5.5.2.6 (2012/05/25) findbugs対?
094    
095                            return byte2hexa( encrypted );
096                    }
097                    catch( javax.crypto.IllegalBlockSizeException   ex ) { throw new RuntimeException( ex ); }
098                    catch( java.security.InvalidKeyException                ex ) { throw new RuntimeException( ex ); }
099                    catch( java.security.NoSuchAlgorithmException   ex ) { throw new RuntimeException( ex ); }
100                    catch( javax.crypto.BadPaddingException                 ex ) { throw new RuntimeException( ex ); }
101                    catch( javax.crypto.NoSuchPaddingException              ex ) { throw new RuntimeException( ex ); }
102            }
103    
104            /**
105             * セキュリ?カラ??DBTyepに対してHybs独自の復号化を行います?
106             * ここでの復号化?、encrypt で暗号化された?を戻す?合に使用します?
107             * こ?復号化では、null は復号化できな?め?ゼロ??を返します?
108             *
109             * @param       hex     復号化を行う暗号化された?6?数アスキー??
110             *
111             * @return      復号化された????
112             */
113            public String decrypt( final String hex ) {
114                    if( hex == null || hex.length() == 0 ) { return ""; }
115    
116                    try {
117                            Cipher cipher = Cipher.getInstance( CIPHER_TYPE );
118                            cipher.init( Cipher.DECRYPT_MODE, sksSpec );
119                            byte[] encrypted = hexa2byte( hex );
120                            byte[] decrypted = cipher.doFinal( encrypted );
121    //                      return new String( decrypted );
122                            return new String( decrypted,DEFAULT_CHARSET );         // 5.5.2.6 (2012/05/25) findbugs対?
123                    }
124                    catch( javax.crypto.IllegalBlockSizeException   ex ) { throw new RuntimeException( ex ); }
125                    catch( java.security.InvalidKeyException                ex ) { throw new RuntimeException( ex ); }
126                    catch( java.security.NoSuchAlgorithmException   ex ) { throw new RuntimeException( ex ); }
127                    catch( javax.crypto.BadPaddingException                 ex ) { throw new RuntimeException( ex ); }
128                    catch( javax.crypto.NoSuchPaddingException              ex ) { throw new RuntimeException( ex ); }
129            }
130            /**
131             * 数字から16??に変換する??ブルです?
132             */
133            private static final char[] hexadecimal =
134                    { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
135                      'a', 'b', 'c', 'd', 'e', 'f' };
136    
137            /**
138             * バイト?列を?6?数アスキー??に変換します?
139             *
140             * バイト?列を?文字?0??,a???アスキーに変換されます?
141             * これにより、すべての?を、アスキー化できます?
142             * アスキー化で、上位が0F以下?場合でも?0 を?すことで、固定長に変換します?
143             *
144             * よって、?力バイト???のlength()を持ったStringを作?します?
145             *
146             * @param       input バイト??
147             *
148             * @return      ?6?数アスキー??
149             */
150            public static String byte2hexa( final byte[] input ) {
151                    String rtn = null;
152                    if( input != null ) {
153                            int len = input.length ;
154                            char[] ch = new char[len*2];
155                            for( int i=0; i<len; i++ ) {
156                                    int high = ((input[i] & 0xf0) >> 4);
157                                    int low  = (input[i] & 0x0f);
158                                    ch[i*2]   = hexadecimal[high];
159                                    ch[i*2+1] = hexadecimal[low];
160                            }
161                            rtn =  new String(ch);
162                    }
163                    return rtn;
164            }
165    
166            /**
167             * ?6?数アスキー??をバイト?列に変換します?
168             *
169             * ?文字?0??,a???アスキー??を?バイト?列に変換されます?
170             *
171             * よって、?力Stringの????のlengthを持ったバイト?列を作?します?
172             *
173             * @param       input ?6?数アスキー??
174             *
175             * @return      バイト??
176             */
177            public static byte[] hexa2byte( final String input ) {
178                    byte[] rtn = null;
179                    if( input != null ) {
180                            int len = input.length() ;
181                            rtn = new byte[len/2];
182                            for( int i=0; i<len/2; i++ ) {
183                                    char ch = input.charAt( i*2 );
184                                    int high = ( ch < 'a' ) ? ch-'0' : ch-'a'+10 ;
185                                    ch = input.charAt( i*2+1 );
186                                    int low  = ( ch < 'a' ) ? ch-'0' : ch-'a'+10 ;
187                                    rtn[i] = (byte)(high << 4 | low);
188                            }
189                    }
190                    return rtn;
191            }
192    
193            /**
194             * MessageDigestにより、MD5 でハッシュした?に変換します?
195             *
196             * MD5で?6Byteのバイトに変換されますが、ここでは?del>マイナス時には,
197             * 符号を反転させて?/del>?6?数で??に変換して?す?
198             * <del>よって、このメソ?で変換した?でのみ突き合わせて正しいかど??
199             * 判断してください?/del>
200             *
201             * 変換方法?、各バイト?上?下位を?6???に変換後?連結して?す?
202             * これは、Tomcat等? digest 認証(MD5使用?と同じ変換方式です?
203             * 連結後???長は?2バイ?固?になります?
204             *
205             * @og.rev 5.2.2.0 (2010/11/01) util.StringUtil から移?
206             *
207             * @param       input 変換前???
208             *
209             * @return      MD5でハッシュした??。32バイ?固?
210             */
211            public static String getMD5( final String input ) {
212                    String rtn = null;
213                    if( input != null ) {
214                            try {
215                                    MessageDigest md5 = MessageDigest.getInstance( "MD5" );
216    //                              md5.update( input.getBytes() );
217                                    md5.update( input.getBytes( DEFAULT_CHARSET ) );        // 5.5.2.6 (2012/05/25) findbugs対?
218                                    byte[] out = md5.digest();
219                                    rtn = byte2hexa( out );
220                            }
221                            catch( NoSuchAlgorithmException ex ) {
222                                    String errMsg = "MessageDigestで失敗しました?" + input + "]"
223                                                            + ex.getMessage() ;
224                                    throw new RuntimeException( errMsg,ex );
225                            }
226                    }
227                    return rtn;
228            }
229    
230            /**
231             * 暗号化??トを行う為のメインメソ?
232             *
233             * java HybsCryptography KEY TEXT で起動します?
234             *   KEY  : 秘?(8 の倍数 (32 以?448 以???
235             *   TEXT : 変換する??
236             *
237             * @og.rev 5.2.2.0 (2010/11/01) 循環参?の解?LogWriter 削除)
238             *
239             * @param       args    引数配?
240             */
241            public static void main( final String[] args ) throws Exception {
242                    if( args.length != 2 ) {
243    //                      LogWriter.log( "java HybsCryptography KEY TEXT" );
244    //                      LogWriter.log( "  KEY  : 秘?(8 の倍数 (32 以?448 以???" );
245    //                      LogWriter.log( "  TEXT : 変換する??" );
246                            System.out.println( "java HybsCryptography KEY TEXT" );
247                            System.out.println( "  KEY  : 秘?(8 の倍数 (32 以?448 以???" );
248                            System.out.println( "  TEXT : 変換する??" );
249                            return;
250                    }
251    
252                    HybsCryptography cript = new HybsCryptography( args[0] );
253    
254                    System.out.println( "IN   TEXT : " + args[1] );
255    
256                    String hexa = cript.encrypt( args[1] );
257                    System.out.println( "HEXA TEXT : " + hexa );
258    
259                    String data = cript.decrypt( hexa );
260                    System.out.println( "OUT  DATA : " + data );
261            }
262    }