加密解密的常用方法总结

下面直接贴代码,拷贝过去直接用,方便。

public class DesUtils

{

    /**

     * 对指定的文件加密

     * @param file

     * @return

     * @throws FileNotFoundException 

     */

    public static boolean desCrypFile(File infile, File outFiles,

            byte[] password) throws FileNotFoundException

    {

        if (!infile.exists())

        {

            throw new FileNotFoundException();

        }

        InputStream in = new FileInputStream(infile);

        return desCrypFile(in, outFiles, password);

    }


    /**

     * 对指定的输入流中的数据加密

     * @param in 输入流

     * @param outFiles 输出文件

     * @param password 密码

     * @return  

     * @throws FileNotFoundException

     */

    public static boolean desCrypFile(InputStream in, File outFiles,

            byte[] password) throws FileNotFoundException

    {

        byte[] b;

        FileOutputStream out = null;

        try

        {

            b = new byte[in.available()];

            in.read(b);

            in.close();

            b = crypto(b, password);


            // 写出文件

            if (!outFiles.exists())

            {

                outFiles.createNewFile();

                out = new FileOutputStream(outFiles);

                out.write(b);

                out.flush();

            }

        }

        catch (IOException e)

        {

            e.printStackTrace();

            return false;

        }

        finally

        {

            if(out != null)

            {

                try

                {

                    out.close();

                }

                catch (IOException e)

                {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                }

            }

        }

        return true;

    }


    /**

     * 将字符串加密到指定的文件

     * @param outbyte 需要加密的字符串

     * @param outFiles

     * @param password

     * @return

     */

    public static boolean desCrypFile(byte[] outbyte, File outFiles,

            byte[] password)

    {

        FileOutputStream out = null;

        try

        {

            outbyte = crypto(outbyte, password);

            // 写出文件

            if (!outFiles.exists())

            {

                outFiles.createNewFile();

                out = new FileOutputStream(outFiles);

                if (outbyte != null)

                {

                    out.write(outbyte);

                    out.flush();

                }

                else

                {

                    return false;

                }

            }

        }

        catch (IOException e)

        {

            e.printStackTrace();

            return false;

        }

        finally

        {

            if(out != null)

            {

                try

                {

                    out.close();

                }

                catch (IOException e)

                {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                }

            }

        }

        return true;

    }


    /**

     * 将指定的文件使用密码加密,返回的是加密之后的字节数组

     * @param infile 需要加密的文件

     * @param password 加密使用的密码

     * @return 返回加密之后的字节数组

     */

    public static byte[] desDecrypFile(File infile, byte[] password)

            throws FileNotFoundException

    {

        if (!infile.exists())

        {

            throw new FileNotFoundException();

        }

        InputStream in = new FileInputStream(infile);

        byte[] b;

        try

        {

            b = new byte[in.available()];

            in.read(b);

            in.close();

            b = decrypt(b, password);

            return b;

        }

        catch (IOException e)

        {

            e.printStackTrace();

        }

        catch (Exception e)

        {

            e.printStackTrace();

        }

        finally

        {

            if(in != null)

            {

                try

                {

                    in.close();

                }

                catch (IOException e)

                {

                    e.printStackTrace();

                }

            }

        }

        return null;

    }


    /**

     * 对指定的字节数组加密

     * @param src 要加密的原字符串

     * @param password

     * @return

     */

    public static byte[] desCrypByte(byte[] src, byte[] password)

    {

        return crypto(src, password);

    }


    /**

     * 将给定的数据解密

     * @param src

     * @param password

     * @return

     * @throws Exception

     */

    public static byte[] desDeCrypByte(byte[] src, byte[] password)

            throws Exception

    {

        return decrypt(src, password);

    }

    

    /**

     * 加密

     * @param datasource

     * @param password

     * @return

     */

    private static byte[] crypto(byte[] datasource, byte[] password)

    {

        try

        {

            SecureRandom random = new SecureRandom();

            DESKeySpec desKey = new DESKeySpec(password);

            // 创建一个密匙工厂,然后用它把DESKeySpec转换成

            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");

            SecretKey securekey = keyFactory.generateSecret(desKey);

            // Cipher对象实际完成加密操作

            Cipher cipher = Cipher.getInstance("DES");

            // 用密匙初始化Cipher对象

            cipher.init(Cipher.ENCRYPT_MODE, securekey, random);

            // 现在,获取数据并加密

            // 正式执行加密操作

            return cipher.doFinal(datasource);

        }

        catch (Throwable e)

        {

            e.printStackTrace();

        }

        return null;

    }


    /**

     * 解密

     * @param src

     * @param password

     * @return

     * @throws Exception

     */

    private static byte[] decrypt(byte[] src, byte[] password) throws Exception

    {

        // DES算法要求有一个可信任的随机数源

        SecureRandom random = new SecureRandom();

        // 创建一个DESKeySpec对象

        DESKeySpec desKey = new DESKeySpec(password);

        // 创建一个密匙工厂

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");

        // 将DESKeySpec对象转换成SecretKey对象

        SecretKey securekey = keyFactory.generateSecret(desKey);

        // Cipher对象实际完成解密操作

        Cipher cipher = Cipher.getInstance("DES");

        // 用密匙初始化Cipher对象

        cipher.init(Cipher.DECRYPT_MODE, securekey, random);

        // 真正开始解密操作

        return cipher.doFinal(src);

    }

}


转载于:https://my.oschina.net/u/1244156/blog/270897

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值