<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    夢幻之旅

    DEBUG - 天道酬勤

       :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks

    加密類:

    package com.framework.commons.util;

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.security.Key;
    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;
    import java.security.Security;

    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;

    /**
     * <ul>
     * <li>Title:[DESEncryptUtil]</li>
     * <li>Description: [加密碼解密類]</li>
     * <li>Copyright 2009 RoadWay Co., Ltd.</li>
     * <li>All right reserved.</li>
     * <li>Created by [Huyvanpull] [Jul 19, 2010]</li>
     * <li>Midified by [修改人] [修改時間]</li>
     * </ul>
     * 
     * 
    @version 1.0
     
    */

    public class DESEncryptUtil
    {
        
    public static void main(String[] args) throws Exception
        
    {
            
    /** 生成KEY */
            String operatorType 
    = "key";
            String keyFilePath 
    = "D:/key.k";
            DESEncryptUtil.test(keyFilePath, 
    null, operatorType);
            
            
    /** 加密 */
            operatorType 
    = "encrypt";
            String sourceFilePath 
    = "D:/jdbc_official.properties";
            DESEncryptUtil.test(keyFilePath, sourceFilePath, operatorType);
            
            
    /** 解密 */
            operatorType 
    = "decrypt";
            sourceFilePath 
    = "D:/en_jdbc_official.properties";
            DESEncryptUtil.test(keyFilePath, sourceFilePath, operatorType);
        }

        
    /**
         * <ul>
         * <li>Description:[創建一個密鑰]</li>
         * <li>Created by [Huyvanpull] [Jul 19, 2010]</li>
         * <li>Midified by [修改人] [修改時間]</li>
         * </ul>
         * 
         * 
    @return
         * 
    @throws NoSuchAlgorithmException
         
    */

        
    public static Key createKey() throws NoSuchAlgorithmException
        
    {
            Security.insertProviderAt(
    new com.sun.crypto.provider.SunJCE(), 1);
            KeyGenerator generator 
    = KeyGenerator.getInstance("DES");
            generator.init(
    new SecureRandom());
            Key key 
    = generator.generateKey();
            
    return key;
        }

        
        
    /**
         * <ul>
         * <li>Description:[根據流得到密鑰]</li>
         * <li>Created by [Huyvanpull] [Jul 19, 2010]</li>
         * <li>Midified by [修改人] [修改時間]</li>
         * </ul>
         * 
         * 
    @param is
         * 
    @return
         
    */

        
    public static Key getKey(InputStream is)
        
    {
            
    try
            
    {
                ObjectInputStream ois 
    = new ObjectInputStream(is);
                
    return (Key) ois.readObject();
            }

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

        }

        
        
    /**
         * <ul>
         * <li>Description:[對數據進行加密]</li>
         * <li>Created by [Huyvanpull] [Jul 19, 2010]</li>
         * <li>Midified by [修改人] [修改時間]</li>
         * </ul>
         * 
         * 
    @param key
         * 
    @param data
         * 
    @return
         
    */

        
    private static byte[] doEncrypt(Key key, byte[] data)
        
    {
            
    try
            
    {
                Cipher cipher 
    = Cipher.getInstance("DES/ECB/PKCS5Padding");
                cipher.init(Cipher.ENCRYPT_MODE, key);
                
    byte[] raw = cipher.doFinal(data);
                
    return raw;
            }

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

        }

        
        
    /**
         * <ul>
         * <li>Description:[對數據進行解密]</li>
         * <li>Created by [Huyvanpull] [Jul 19, 2010]</li>
         * <li>Midified by [修改人] [修改時間]</li>
         * </ul>
         * 
         * 
    @param key
         * 
    @param in
         * 
    @return
         
    */

        
    public static InputStream doDecrypt(Key key, InputStream in)
        
    {
            
    try
            
    {
                Cipher cipher 
    = Cipher.getInstance("DES/ECB/PKCS5Padding");
                cipher.init(Cipher.DECRYPT_MODE, key);
                ByteArrayOutputStream bout 
    = new ByteArrayOutputStream();
                
    byte[] tmpbuf = new byte[1024];
                
    int count = 0;
                
    while ((count = in.read(tmpbuf)) != -1)
                
    {
                    bout.write(tmpbuf, 
    0, count);
                    tmpbuf 
    = new byte[1024];
                }

                in.close();
                
    byte[] orgData = bout.toByteArray();
                
    byte[] raw = cipher.doFinal(orgData);
                ByteArrayInputStream bin 
    = new ByteArrayInputStream(raw);
                
    return bin;
            }

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

        }

        
        
    private static void test(String keyFilePath, String sourceFilePath,
                String operatorType) 
    throws Exception
        
    {
            
    // 提供了Java命令使用該工具的功能
            if (operatorType.equalsIgnoreCase("key"))
            
    {
                
    // 生成密鑰文件
                Key key = DESEncryptUtil.createKey();
                ObjectOutputStream oos 
    = new ObjectOutputStream(new FileOutputStream(keyFilePath));
                oos.writeObject(key);
                oos.close();
                System.out.println(
    "成功生成密鑰文件" + keyFilePath);
            }

            
    else if (operatorType.equalsIgnoreCase("encrypt"))
            
    {
                
    // 對文件進行加密
                File file = new File(sourceFilePath);
                FileInputStream in 
    = new FileInputStream(file);
                ByteArrayOutputStream bout 
    = new ByteArrayOutputStream();
                
    byte[] tmpbuf = new byte[1024];
                
    int count = 0;
                
    while ((count = in.read(tmpbuf)) != -1)
                
    {
                    bout.write(tmpbuf, 
    0, count);
                    tmpbuf 
    = new byte[1024];
                }

                in.close();
                
    byte[] orgData = bout.toByteArray();
                Key key 
    = getKey(new FileInputStream(keyFilePath));
                
    byte[] raw = DESEncryptUtil.doEncrypt(key, orgData);
                file 
    = new File(file.getParent() + "\\en_" + file.getName());
                FileOutputStream out 
    = new FileOutputStream(file);
                out.write(raw);
                out.close();
                System.out.println(
    "成功加密,加密文件位于:" + file.getAbsolutePath());
            }

            
    else if (operatorType.equalsIgnoreCase("decrypt"))
            
    {
                
    // 對文件進行解密
                File file = new File(sourceFilePath);
                FileInputStream fis 
    = new FileInputStream(file);
                
                Key key 
    = getKey(new FileInputStream(keyFilePath));
                InputStream raw 
    = DESEncryptUtil.doDecrypt(key, fis);
                ByteArrayOutputStream bout 
    = new ByteArrayOutputStream();
                
    byte[] tmpbuf = new byte[1024];
                
    int count = 0;
                
    while ((count = raw.read(tmpbuf)) != -1)
                
    {
                    bout.write(tmpbuf, 
    0, count);
                    tmpbuf 
    = new byte[1024];
                }

                raw.close();
                
    byte[] orgData = bout.toByteArray();
                file 
    = new File(file.getParent() + "\\rs_" + file.getName());
                FileOutputStream fos 
    = new FileOutputStream(file);
                fos.write(orgData);
                System.out.println(
    "成功解密,解密文件位于:" + file.getAbsolutePath());
            }

        }

    }

    DecryptPropertyPlaceholderConfigurer.java
    package com.framework.spring;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.security.Key;
    import java.util.Properties;

    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
    import org.springframework.core.io.Resource;
    import org.springframework.util.DefaultPropertiesPersister;
    import org.springframework.util.PropertiesPersister;

    import com.framework.commons.util.DESEncryptUtil;

    public class DecryptPropertyPlaceholderConfigurer extends
            PropertyPlaceholderConfigurer
    {
        
    private Resource[] locations;
        
        
    private Resource keyLocation;
        
        
    private String fileEncoding;
        
        
    public void setKeyLocation(Resource keyLocation)
        
    {
            
    this.keyLocation = keyLocation;
        }

        
        
    public void setLocations(Resource[] locations)
        
    {
            
    this.locations = locations;
        }

        
        
    public void loadProperties(Properties props) throws IOException
        
    {
            
    if (this.locations != null)
            
    {
                PropertiesPersister propertiesPersister 
    = new DefaultPropertiesPersister();
                
    for (int i = 0; i < this.locations.length; i++)
                
    {
                    Resource location 
    = this.locations[i];
                    
    if (logger.isInfoEnabled())
                    
    {
                        logger.info(
    "Loading properties file from " + location);
                    }

                    InputStream is 
    = null;
                    
    try
                    
    {
                        is 
    = location.getInputStream();
                        Key key 
    = DESEncryptUtil.getKey(keyLocation.getInputStream());
                        is 
    = DESEncryptUtil.doDecrypt(key, is);
                        
    if (fileEncoding != null)
                        
    {
                            propertiesPersister.load(props, 
    new InputStreamReader(
                                    is, fileEncoding));
                        }

                        
    else
                        
    {
                            propertiesPersister.load(props, is);
                        }

                    }

                    
    finally
                    
    {
                        
    if (is != null)
                        
    {
                            is.close();
                        }

                    }

                }

            }

        }

    }


    配置文件:
    <!-- 加密碼屬性文件 -->
        
    <bean id="myPropertyConfigurer"
            class
    ="com.framework.spring.DecryptPropertyPlaceholderConfigurer">
            
    <property name="locations">
                
    <list><value>classpath*:spring_config/jdbc_official.databaseinfo</value></list>
            
    </property>
            
    <property name="fileEncoding" value="UTF-8"/>
            
    <property name="keyLocation" value="classpath:spring_config/key.key" />
        
    </bean>
    posted on 2010-07-19 17:22 HUIKK 閱讀(1753) 評論(0)  編輯  收藏 所屬分類: Spring
    主站蜘蛛池模板: 久久久久亚洲Av片无码v| 国产免费久久精品99久久| 亚洲av中文无码乱人伦在线r▽ | 亚洲精品视频在线观看免费| 四虎影视永久免费观看地址| 我的小后妈韩剧在线看免费高清版| 亚洲AV第一页国产精品| 国产乱子伦精品免费女| 无码一区二区三区AV免费| 日本高清免费观看| 免费精品久久久久久中文字幕| 伊人亚洲综合青草青草久热| 成人永久免费高清| 无人在线观看免费高清视频 | 91麻豆国产免费观看| 在线观看人成视频免费无遮挡| 亚洲午夜久久久久久噜噜噜| 亚洲成A人片在线观看中文| 精品国产免费一区二区| 免费不卡视频一卡二卡| 国产在线观看麻豆91精品免费| 亚洲成av人片天堂网无码】| 亚洲国产日韩女人aaaaaa毛片在线| 日韩一级在线播放免费观看| 无码日韩人妻av一区免费| 麻豆国产精品免费视频| 一级成人a毛片免费播放| 久久国产精品免费视频| 免费在线看黄网站| 性无码免费一区二区三区在线| 亚洲欧美不卡高清在线| 亚洲乱亚洲乱妇24p| 亚洲欧美日韩中文字幕在线一区| 中文字幕亚洲乱码熟女一区二区| 亚洲黄色片免费看| 中文字幕免费高清视频| 免费h片在线观看网址最新| 91香蕉成人免费网站| 无限动漫网在线观看免费| 在线免费视频一区| 免费人成视频x8x8入口|