关于Android侧使用AES进行加解密时的key和iv使用的方法
来源:互联网 发布:ug软件下载 编辑:程序博客网 时间:2024/05/01 07:56
AES的CBC加密模式,默认iv是16个0(这个称为初始化向量),由于是分组加密,所以下一组的iv,就用前一组的加密的密文来充当,我们可以指定IV来进行加解密,加大破解难度。CFB、OFB模式类似,只不过更复杂。
废话不多说,直接上代码:
package com.irisking.scanner.util;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.IOException;import android.os.Environment;public class AesUtil {private static int BUFFER_SIZE = 8192;/** * 从指定文件获取数据解密后存储到另外一个文件 * * @param filePath 需解锁文件路径 * @param destFilePath 存储文件路径 */public static void decrypt(String filePath, String destFilePath) {MoreAES aes = MoreAES.getInstances();String keyPath = Environment.getExternalStorageDirectory().getAbsoluteFile() + "/key.dat";byte[] byteArrays = getBytesFromDat(keyPath);String ivPath = Environment.getExternalStorageDirectory().getAbsoluteFile() + "/iv.dat";byte[] ivArray = getBytesFromDat(ivPath);aes.decrypt(filePath, destFilePath, byteArrays,ivArray);}/** * 获取密钥的byte数组 * @param keyPath * @return */private static byte[] getBytesFromDat(String keyPath) {FileInputStream fis = null;ByteArrayOutputStream baos = null;byte[] byteArrays = null;try {fis = new FileInputStream(keyPath);byte[] b = new byte[BUFFER_SIZE];int len;baos = new ByteArrayOutputStream();while ((len = fis.read(b)) != -1) {baos.write(b, 0, len);}byteArrays = baos.toByteArray();} catch (Exception e) {e.printStackTrace();} finally{if (fis != null) {try {fis.close();} catch (IOException e) {}}if (baos != null) {try {baos.flush();} catch (IOException e) {}try {baos.close();} catch (IOException e) {}}}return byteArrays;}}
package com.irisking.scanner.util;import java.io.BufferedInputStream;import java.io.ByteArrayOutputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.security.InvalidAlgorithmParameterException;import java.security.Key;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import javax.crypto.Cipher;import javax.crypto.CipherInputStream;import javax.crypto.CipherOutputStream;import javax.crypto.NoSuchPaddingException;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import android.os.Environment;public class MoreAES {private static String TYPE = "AES";private static int BUFFER_SIZE = 8192;private static MoreAES moreAes;private MoreAES(){}public synchronized static MoreAES getInstances(){if(moreAes == null){moreAes = new MoreAES();}return moreAes;}private static Cipher getCipher(int mode, byte[] key,byte[] iv) {// mode =Cipher.DECRYPT_MODE or Cipher.ENCRYPT_MODECipher mCipher;try {mCipher = Cipher.getInstance(TYPE + "/CBC/PKCS5Padding");Key keySpec = new SecretKeySpec(key, "AES");IvParameterSpec ivSpec = new IvParameterSpec(iv);mCipher.init(mode, keySpec, ivSpec);return mCipher;}catch (InvalidKeyException e) {e.printStackTrace();}catch (NoSuchAlgorithmException e) {e.printStackTrace();}catch (NoSuchPaddingException e) {e.printStackTrace();}catch (InvalidAlgorithmParameterException e) {e.printStackTrace();}return null;}/** * * 解密文件 * @param srcFile * @param destFile * @param privateKey */public void decrypt(String srcFile, String destFile, byte[] privateKey,byte[] privateIv) {byte[] readBuffer = new byte[BUFFER_SIZE];Cipher deCipher = getCipher(Cipher.DECRYPT_MODE, privateKey,privateIv);if (deCipher == null)return; // init failed.CipherInputStream fis = null;BufferedOutputStream fos = null;int size;try {fis = new CipherInputStream(new BufferedInputStream(new FileInputStream(srcFile)), deCipher);fos = new BufferedOutputStream(new FileOutputStream(mkdirFiles(destFile)));while ((size = fis.read(readBuffer, 0, BUFFER_SIZE)) >= 0) {fos.write(readBuffer, 0, size);}fos.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (fis != null) {try {fis.close();} catch (IOException e) {}}if (fos != null) {try {fos.flush();} catch (IOException e) {}try {fos.close();} catch (IOException e) {}}}}/** * 加密文件 * @param srcFile * @param destFile * @param privateKey */public void crypt(String srcFile, String destFile, byte[] privateKey,byte[] privateIv) {byte[] readBuffer = new byte[BUFFER_SIZE];Cipher enCipher = getCipher(Cipher.ENCRYPT_MODE, privateKey,privateIv);if (enCipher == null)return; // init failed.CipherOutputStream fos = null;BufferedInputStream fis = null;int size;try {fos = new CipherOutputStream(new BufferedOutputStream(new FileOutputStream(destFile)), enCipher);fis = new BufferedInputStream(new FileInputStream(mkdirFiles(srcFile)));while ((size = fis.read(readBuffer, 0, BUFFER_SIZE)) >= 0) {fos.write(readBuffer, 0, size);}fos.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (fis != null) {try {fis.close();} catch (IOException e) {}}if (fos != null) {try {fos.flush();} catch (IOException e) {}try {fos.close();} catch (IOException e) {}}}}private static File mkdirFiles(String filePath) throws IOException {File file = new File(filePath);if (!file.getParentFile().exists()) {file.getParentFile().mkdirs();}file.createNewFile();return file;}}
个人联系方式:15010399702@163.com
0 0
- 关于Android侧使用AES进行加解密时的key和iv使用的方法
- 使用AES对文件的加解密
- 使用OpenSSL库的AES加解密
- 【AES】使用OpenSSL库的AES加解密
- 关于Android中使用AES加密解密的问题
- android中使用AES加解密
- WebSphere下使用CA证书进行签名和加解密的方法
- 使用Python进行AES加密和解密
- 基于openssl的AES加解密 android
- android常用的AES加解密
- Java使用AES加解密
- 如何使用OPENSSL中的RSA和AES模块进行身份认证和业务加解密
- AES加解密时的 java.security.InvalidKeyException: Illegal key size 异常
- php AES 加解密方法使用函数了解
- Android 使用AES/CBC/PKCS7Padding 加解密字符串
- C++ 和 java 使用 AES CBC 128 加解密
- 对称加密算法AES------使用AES算法对文件进行加密/解密的操作(JAVA)
- 关于AES加解密
- oracle审计AUD$过大导致的数据库登录异常
- linux系统CentOS下调整home和根分区大小的方法
- 多线程基础
- 元数据
- velocity
- 关于Android侧使用AES进行加解密时的key和iv使用的方法
- iOS "_OBJC_CLASS_$_CTTelephonyNetworkInfo", referenced from:
- mysql语法
- '@P0'附近有语法错误
- 【HDU 4883】TIANKENG’s restaurant(区间更新)
- LeetCode-337. House Robber III
- oracle sqlldr控制文件模板
- GraphicsView 使用时,程序死掉总结
- HDU 4699