corner.util.crypto.Cryptor.java Source code

Java tutorial

Introduction

Here is the source code for corner.util.crypto.Cryptor.java

Source

// Copyright 2007 the original author or authors.
// site: http://www.bjmaxinfo.com
// file: $Id: Cryptor.java 3919 2008-01-16 04:36:52Z xf $
// created at:2005-10-18
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package corner.util.crypto;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * .
 * @author <a href="http://wiki.java.net/bin/view/People/JunTsai">Jun Tsai</a>
 * @version $Revision:3677 $
 */
public class Cryptor {
    private static final Log log = LogFactory.getLog(Cryptor.class);

    /**
     * IO?,IO??,.
     * @param outFileName ??. 
     * @param keyFile ??.
     * @return ??.
     */
    public static OutputStream encryptFileIO(String outFileName, String keyFile) {
        if (keyFile == null) {
            try {
                return new FileOutputStream(outFileName);
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
        SecretKey key = null;

        //?
        ObjectInputStream keyis;
        try {
            keyis = new ObjectInputStream(new FileInputStream(keyFile));
            key = (SecretKey) keyis.readObject();
            keyis.close();
        } catch (FileNotFoundException e) {
            log.error("file not found!", e);
            throw new RuntimeException("file not found", e);
        } catch (IOException e) {
            log.error("io occour exception", e);
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            log.error("Class Not Found  exception", e);
            throw new RuntimeException(e);
        }

        //keyCipher
        Cipher cipher = null;
        //?Cipher?

        try {
            cipher = Cipher.getInstance("DES");
            //?

            cipher.init(Cipher.ENCRYPT_MODE, key);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        }

        //???

        //
        CipherOutputStream out = null;
        try {
            out = new CipherOutputStream(new BufferedOutputStream(new FileOutputStream(outFileName)), cipher);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }

        return out;

    }

    /**
     * ?IO?.IO?,.
     * @param inputFileName ????.
     * @param keyFile ??.
     * @return ?IO?.
     */
    public static InputStream dencryptFileIO(String inputFileName, String keyFile) {
        if (keyFile == null) {
            try {
                return new FileInputStream(new File(inputFileName));
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
        SecretKey key = null;

        ObjectInputStream keyis;
        try {
            keyis = new ObjectInputStream(new FileInputStream(keyFile));
            key = (SecretKey) keyis.readObject();

            keyis.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        //keyCipher
        Cipher cipher = null;

        try {
            //,
            cipher = Cipher.getInstance("DES");
            // ?
            cipher.init(Cipher.DECRYPT_MODE, key);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        }

        File file = new File(inputFileName);

        try {

            //?
            CipherInputStream in = new CipherInputStream(new BufferedInputStream(new FileInputStream(file)),
                    cipher);
            return in;

        } catch (Exception e) {
            throw new RuntimeException(e);

        }

    }
}