AES加密简介(含demo,下载可用)
来源:互联网 发布:郭德纲 陈宝国 知乎 编辑:程序博客网 时间:2024/06/05 02:50
AES加密是一种对称加密方式(何为对称加密可以百度),加密工具已经被集成于jdk中,所以开发时不必再导入第三方jar.
加密/解密中需要秘钥key,就是说解密一份AES加密过的文件需要使用和加密时相同的key. key可以是128位的,也可以是256位的,上代码:
/** * 用password生成key * @param password 用来生成key的字符串,加密解密同一份文件要使用相同的key * * */ private SecretKeySpec getKeys(String password) { int keyLength = 256; //根据需要可以设置成128位或者256位 byte[] keyBytes = new byte[keyLength / 16]; Arrays.fill(keyBytes, (byte) 0x0); byte[] passwordBytes = new byte[0]; SecretKeySpec key = null; try { passwordBytes = password.getBytes("UTF-8"); int length = passwordBytes.length < keyBytes.length ? passwordBytes.length : keyBytes.length; System.arraycopy(passwordBytes, 0, keyBytes, 0, length); key = new SecretKeySpec(keyBytes, "AES"); ; } catch (Exception e) { e.printStackTrace(); } return key; }
加密和解密的过程也非常简单:
/** * <p> * 文件加密 * </p> * * @param sourceFilePath * @param destFilePath * @throws Exception */ public boolean encryptFile(String sourceFilePath, String destFilePath) { File sourceFile = new File(sourceFilePath); File destFile = new File(destFilePath); try { if (sourceFile.exists() && sourceFile.isFile()) { if (!destFile.getParentFile().exists()) { destFile.getParentFile().mkdirs(); } destFile.createNewFile(); InputStream in = new FileInputStream(sourceFile); OutputStream out = new FileOutputStream(destFile); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, getKeys(Constant.AES_KEY)); CipherInputStream cin = new CipherInputStream(in, cipher); byte[] cache = new byte[1024]; int nRead = 0; while ((nRead = cin.read(cache)) != -1) { out.write(cache, 0, nRead); out.flush(); } out.close(); cin.close(); in.close(); } } catch (Exception e) { e.printStackTrace(); return false; } return true; } /** * <p> * 文件解密 * </p> * * @param sourceFilePath * @param destFilePath * @throws Exception */ public void decryptFile(String sourceFilePath, String destFilePath) throws Exception { File sourceFile = new File(sourceFilePath); File destFile = new File(destFilePath); if (sourceFile.exists() && sourceFile.isFile()) { if (!destFile.getParentFile().exists()) { destFile.getParentFile().mkdirs(); } destFile.createNewFile(); FileInputStream in = new FileInputStream(sourceFile); FileOutputStream out = new FileOutputStream(destFile); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, getKeys(Constant.AES_KEY)); CipherOutputStream cout = new CipherOutputStream(out, cipher); byte[] cache = new byte[1024]; int nRead = 0; while ((nRead = in.read(cache)) != -1) { cout.write(cache, 0, nRead); cout.flush(); } cout.close(); out.close(); in.close(); } }
其中使用的常量大家可以自行定义.
demo下载地址: http://download.csdn.net/download/qq_40983782/10111384
demo使用了一位博主的部分部分代码,只是后来找不到这位博主的博客了,在此一并致谢.
在demo里我是先把一份本地的文件复制到sdCard,然后在同一个文件夹下生成加密和解密的文件,效果如下:
如果读者在开发过程中发现有什么问题可以联系本人 odm.lee@foxmail.com
阅读全文
0 0
- AES加密简介(含demo,下载可用)
- java aes加密demo
- 高级加密标准(AES)简介
- AES 后台加密 CryptoJS 前台解密demo
- 【加密与解密】AES加密算法原理简介
- Android Studio Jni开发详细入门,Aes加密demo
- Android Studio ndk-Jni开发详细入门,Aes加密demo
- Android Studio ndk-Jni开发详细入门,Aes加密demo
- AES加密
- AES加密
- AES加密
- AES加密
- AES 加密
- AES加密
- AES 加密
- AES加密
- AES加密
- AES加密
- 突破10万高并发的nginx性能优化经验(含内核参数优化)
- DrawerLayout的使用
- Ubuntu中docker容器运行apt-get update失败 Err:1 http://archive.ubuntu.com/ubuntu xenial InRelease
- 由configure文件生成Makefile文件简单介绍
- vue-cli的线上部署 run build
- AES加密简介(含demo,下载可用)
- HDU 2844-Coins
- Android Studio3.0爬坑日记
- linux\mac 日常入门命令行使用——文件以及文件夹操作
- (算法分析Week10)ZigZag Conversion[Medium]
- Java自学笔记之struts2 文件上传
- js遍历键值对数组
- 使用 VSCode 编辑器来编译 Sass
- 【noip2016】换教室