ファイルを対称的にDES暗号化


package com.ivan.security.algorithm;

/**
 *          DES  
 * @author Ivan
 * @DataTime 2006-12-11 12:34
 *
 */

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import com.iuxi.security.util.*;

public class DESUtil ...{
    
    private static String Algorithm = "DESede"; //"DESede" for Triple DES
    private static KeyGenerator keyGen = null;
    private static SecretKey desKey = null;
    private static Cipher c = null;
    private static byte[] cipherByte = null;
    
    public DESUtil() ...{
        init();
    }
    
    public static void init() ...{
        try ...{
            c = Cipher.getInstance(Algorithm);
        } catch (NoSuchAlgorithmException e) ...{
            System.err.println("     ,  :        !");
            e.printStackTrace();
        } catch (NoSuchPaddingException e) ...{
            e.printStackTrace();
        }
    }

    public static String getAlgorithm() ...{
        return Algorithm;
    }

    public static void setAlgorithm(String algorithm) ...{
        DESUtil.Algorithm = algorithm;
    }
    
    public static void setDesKey (SecretKey desKey) ...{
        DESUtil.desKey = desKey;
    }

    /** *//**
     *          
     * @param filepath     
     * @return
     */
    public static void saveKey(String filePath) ...{
        if (desKey != null) ...{
            try ...{
                FileOutputStream fos = new FileOutputStream(filePath);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(desKey);
            }catch (IOException e) ...{
                System.err.println("        !");
                e.printStackTrace();
            }
        }
        else ...{
            System.err.println("      ,     !");
        }
    }
    
    /** *//**
     *          
     * @param filePath
     * @return
     */
    public static SecretKey readKey(String filePath) ...{
        try ...{
            FileInputStream fis = new FileInputStream(filePath);
            fis = new FileInputStream(filePath);
            ObjectInputStream ois = new ObjectInputStream(fis);
            desKey = (SecretKey) ois.readObject();
            ois.close();
            fis.close();
        }catch (FileNotFoundException e) ...{
            System.err.println("        ,  :        !");
        } catch (IOException ex) ...{
            System.err.println("        ,  :IO  !");
            ex.printStackTrace();
        } catch (Exception ex) ...{
            ex.printStackTrace();
        }
        return desKey;
    }
    
    /** *//**
     *            
     * @param algorithm       
     * @return      
     */
    public static SecretKey genKey (String algorithm) ...{
        try ...{
            keyGen = KeyGenerator.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) ...{
            System.err.println("      ,  :        !");
            e.printStackTrace();
        }
        desKey = keyGen.generateKey();
        return desKey;
    }
    
    /** *//**
     *              
     * @param rawByte      
     * @return        
     */
    public static byte[] encryptData(byte[] rawByte) ...{
        if(desKey != null) ...{
            try ...{
                c.init(Cipher.ENCRYPT_MODE, desKey);
                cipherByte = c.doFinal(rawByte);
            } catch (java.security.InvalidKeyException ex) ...{
                System.err.println("    ,    !");
                ex.printStackTrace();
            } catch (javax.crypto.BadPaddingException ex) ...{
                System.err.println("    !");
                ex.printStackTrace();
            } catch (javax.crypto.IllegalBlockSizeException ex) ...{
                System.err.println("    !");
                ex.printStackTrace();
            } catch (Exception ex) ...{
                System.err.println("    !");
                ex.printStackTrace();
            }
        }
        else ...{
            System.err.println("    ,  :     !");
        }
        return cipherByte;
    }
    
    /** *//**
     *      
     * @param cipherByte      
     * @return        
     */
    public static byte[] decryptor(byte[] cipherByte) ...{
        if(desKey != null) ...{
            try ...{
                c.init(Cipher.DECRYPT_MODE, desKey);
                cipherByte = c.doFinal(cipherByte);
            } catch (java.security.InvalidKeyException ex) ...{
                ex.printStackTrace();
            } catch (javax.crypto.BadPaddingException ex) ...{
                ex.printStackTrace();
            } catch (javax.crypto.IllegalBlockSizeException ex) ...{
                ex.printStackTrace();
            } catch (Exception ex) ...{
                ex.printStackTrace();
            }
            return (cipherByte);
        }
        else ...{
            System.err.println("    ,  :     !");
            return null;
        }
    }
    
    public static void main(String[] args) throws Exception ...{
        DESUtil.init();
        DESUtil.genKey("DESede");
        DESUtil.saveKey("desKey.dat");
        byte[] cipherData = DESUtil.encryptData("This is just a test!".getBytes());
        
        String cipherString = new String(new BASE64Encoder().encode(cipherData));
        System.out.println("     :" + cipherString);
        
        byte[] cipherByte = new BASE64Decoder().decodeBuffer(cipherString);
        String rawString = new String(DESUtil.decryptor(cipherByte));
        System.out.println("       :" + rawString); 
    }

}