六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 37|回复: 0

对文件进行对称的DES加密

[复制链接]

升级  23.33%

21

主题

21

主题

21

主题

秀才

Rank: 2

积分
85
 楼主| 发表于 2013-1-27 05:16:36 | 显示全部楼层 |阅读模式
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);     }} 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表