挂海论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
 友情提示:文字/图片广告均非网站意见,请担保交易勿直接付款,由此产生的责任自负
玩游戏来117游戏网(H5不下载也能玩手游传奇,吃鸡,竞技都有)天下盾/国内/免实名/免备案CDN无视一切CC/DD攻击 找塔科夫作者TG @wuhao1954 QQ283931494 出租内核驱动读写保护,价格亲民,高品质群:530544047 →入驻S9企业发卡网各种全黑号辅助群:475351077
██【我要租此广告位】██... .
查看: 3243|回复: 1
打印 上一主题 下一主题

[其他/分享] AES加密和解密写法文件

[复制链接]
4中级会员
342/600

342

积分

129

主题

8

听众
已帮网友解决0 个问题
好评
0
贡献
213
海币
1465
交易币
0
跳转到指定楼层
楼主
发表于 2017-7-1 13:49:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
提醒:若下载的软件是收费的"请不要付款",可能是骗子,请立即联系本站举报,执意要付款被骗后本站概不负责。(任何交易请走第三方中介,请勿直接付款交易以免被骗!切记).

友情提示:文字/图片广告均非本站意见,请担保交易勿直接付款,由此产生的责任自负!!!↑↑



AES加密和解密写法文件
[Java] 纯文本查看 复制代码
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.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
 
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
 
/**
 * 
 * @author Ylca   2017-05-12   
 * 
 */
public class AES{
 
    /**
     * 加密字符串
     * 
     * @param content 需要加密的内容
     * @param password 密码
     * @return
     */
    public static byte[] encrypt(String content, String sKey) {
        try {
            // 初始化 加密器
            Cipher cipher = initAESCipher(sKey, Cipher.ENCRYPT_MODE);
            byte[] byteContent = content.getBytes("utf-8");
            byte[] result = cipher.doFinal(byteContent);
            return result; // 加密
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 解密字符串
     * 
     * @param content 
     * @param password
     * @return
     */
    public static byte[] decrypt(byte[] content, String sKey) {
        try {
            // 初始化 加密器
            Cipher cipher = initAESCipher(sKey, Cipher.DECRYPT_MODE);
            byte[] result = cipher.doFinal(content);
            return result; // 加密
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 对文件进行AES加密
     * 
     * @param sourceFile
     * @param fileType
     * @param sKey
     * @return
     */
    public static File encryptFile(File sourceFile, File encrypfile, String sKey) {
 
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = new FileInputStream(sourceFile);
 
            outputStream = new FileOutputStream(encrypfile);
            Cipher cipher = initAESCipher(sKey, Cipher.ENCRYPT_MODE);
            // 以加密流写入文件
            CipherInputStream cipherInputStream = new CipherInputStream(
                    inputStream, cipher);
            byte[] cache = new byte[1024];
            int nRead = 0;
            while ((nRead = cipherInputStream.read(cache)) != -1) {
                outputStream.write(cache, 0, nRead);
                outputStream.flush();
            }
            cipherInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace(); // To change body of catch statement use File |
                                    // Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace(); // To change body of catch statement use File |
                                    // Settings | File Templates.
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace(); // To change body of catch statement use
                                        // File | Settings | File Templates.
            }
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace(); // To change body of catch statement use
                                        // File | Settings | File Templates.
            }
        }
        return encrypfile;
    }
 
 
    /**
     *  AES方式解密文件
     * @param sourceFile 要解密的文件路径
     * @param decryptFile 解密后的文件路径
     * @param sKey
     * @return
     */
    public static File decryptFile(File sourceFile, File decryptFile,
            String sKey) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            Cipher cipher = initAESCipher(sKey, Cipher.DECRYPT_MODE);
            inputStream = new FileInputStream(sourceFile);
            outputStream = new FileOutputStream(decryptFile);
            CipherOutputStream cipherOutputStream = new CipherOutputStream(
                    outputStream, cipher);
            byte[] buffer = new byte[1024];
            int r;
            while ((r = inputStream.read(buffer)) >= 0) {
                cipherOutputStream.write(buffer, 0, r);
            }
            cipherOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace(); // To change body of catch statement use File |
                                    // Settings | File Templates.
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace(); // To change body of catch statement use
                                        // File | Settings | File Templates.
            }
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace(); // To change body of catch statement use
                                        // File | Settings | File Templates.
            }
        }
        return decryptFile;
    }
 
    /**
     * 初始化 AES Cipher
     * 
     * @param sKey
     * @param cipherMode
     * @return
     */
    public static Cipher initAESCipher(String sKey, int cipherMode) {
        // 创建Key gen
        KeyGenerator keyGenerator = null;
        Cipher cipher = null;
        try {
            keyGenerator = KeyGenerator.getInstance("AES");
            keyGenerator.init(128, new SecureRandom(sKey.getBytes()));
            SecretKey secretKey = keyGenerator.generateKey();
            byte[] codeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(codeFormat, "AES");
            cipher = Cipher.getInstance("AES");
            // 初始化
            cipher.init(cipherMode, key);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace(); // To change body of catch statement use File |
                                    // Settings | File Templates.
        } catch (NoSuchPaddingException e) {
            e.printStackTrace(); // To change body of catch statement use File |
                                    // Settings | File Templates.
        } catch (InvalidKeyException e) {
            e.printStackTrace(); // To change body of catch statement use File |
                                    // Settings | File Templates.
        }
        return cipher;
    }
 
    public static void main(String[] args) throws Exception {
         
        String cKey = "00b09e37363e596e1f25b23c78e49939238b";
        //未加密文件路径
        File oldfile = new File("C:\\Users\\Administrator\\Desktop\\1.mp4");
        //加密后的文件路径
        File encrypfile = new File("C:\\Users\\Administrator\\Desktop\\encryp.mp4");
        //解密后的文件路径
        File decrypfile  = new File("C:\\Users\\Administrator\\Desktop\\decryp.mp4");
        //加密文件
        encryptFile(oldfile,encrypfile,cKey);
        //解密文件
        decryptFile(encrypfile, decrypfile,cKey);
 
    }






联系我时,请说是在 挂海论坛 上看到的,谢谢!



上一篇:Java Killer系列之Java经典面试套路讲解
下一篇:尚学堂的JAVA视频教程(真实项目开发 办公自动化OA)
免责声明:
1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关。一切关于该内容及资源商业行为与www.52ghai.com无关。

2、本站提供的一切资源内容信息仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。

3、本站信息来自第三方用户,非本站自制,版权归原作者享有,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。

4、如果您喜欢该程序,请支持正版,购买注册,得到更好的正版服务。如有侵犯你版权的,请邮件与我们联系删除(邮箱:[email protected]),本站将立即改正。

1

积分

0

主题

3

听众
已帮网友解决0 个问题
好评
0
贡献
1
海币
21
交易币
0
沙发
发表于 2017-7-1 15:16:55 | 只看该作者
AES加密 AES加密和解密写法文件和解密写法文件
个人签名
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

免责声明|Archiver|手机版|小黑屋|挂海论坛

GMT+8, 2024-4-28 11:07 , Processed in 0.333528 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.2

本站资源来自互联网用户收集发布,如有侵权请邮件与我们联系处理。xhzlw@foxmail.com

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