对文件进行对称的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); 
    }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值