android对文件进行加密

android对文件进行加密最开始想的是对apk进行加密,但是搜到的资料都是对dex层面的加密,后来转念一想,apk也可以被看做是一个普通的文件,普通的文件其实是可以使用AES进行加密的(AES比DES安全性和速度要更好,属于对称性加密里面很好的了),代码如下funmain(args:Array<String>){ //key也可以采用下边的FileAESUtil.getAutoCreateAESKey()方法自动生成valkey=”asdfghjkl”valcontent=.

大家好,又见面了,我是你们的朋友全栈君。

最开始想的是对apk进行加密,但是搜到的资料都是对dex层面的加密,后来转念一想,apk也可以被看做是一个普通的文件,普通的文件其实是可以使用AES进行加密的(AES比DES安全性和速度要更好,属于对称性加密里面很好的了),代码如下

fun main(args: Array<String>) { 
   
	// key也可以采用下边的FileAESUtil.getAutoCreateAESKey()方法自动生成
    val key = "asdfghjkl"
    val content = "plugin.apk"
    val encryptMode = AESUtil.aes(content, key, Cipher.ENCRYPT_MODE)
    System.out.println("加密之后:" + encryptMode)

    // 解密
    val decryptMode = AESUtil.aes(encryptMode,key,Cipher.DECRYPT_MODE);
    System.out.println("解密之后:" + decryptMode)

    // 源文件
    val sourceFile = "F:\\app-debug.apk"
    // 加密输出的目标文件
    val targetFile = "F:\\a.apk"
    // 解密输出的目标文件
    val sourceDecryptFile = "F:\\target\\plugin.apk"

    val autoCreateKey = FileAESUtil.getAutoCreateAESKey()
    FileAESUtil.aesEncryptFile(sourceFile,targetFile,autoCreateKey);
    System.out.println("加密完成")
    FileAESUtil.aesDecryptFile(targetFile,sourceDecryptFile,autoCreateKey)
    System.out.println("解密完成")

}

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class FileAESUtil { 
   

    private static String ALGORITHM_AES = "AES";
    private static int AES_KEY_LEN = 128;


    public static byte[] getAutoCreateAESKey() throws Exception { 
   

        // 实例化一个AES加密算法的密钥生成器
        KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM_AES);
        // 初始化密钥生成器,指定密钥位数为128位
        keyGenerator.init(AES_KEY_LEN, new SecureRandom());
        // 生成一个密钥
        SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    }

    /** AES加密 * * @param sourceFile 源文件 * @param encryptFile 加密文件 * @param password 密钥,128bit * @throws Exception 抛出异常 */
    public static void aesEncryptFile(String sourceFile, String encryptFile, byte[] password) throws Exception { 
   

        // 创建AES密钥
        SecretKeySpec key = new SecretKeySpec(password,  "AES");
        // 创建加密引擎(CBC模式)。Cipher类支持DES,DES3,AES和RSA加加密
        // AES:算法名称
        // CBC:工作模式
        // PKCS5Padding:明文块不满足128bits时填充方式(默认),即在明文块末尾补足相应数量的字符,
        // 且每个字节的值等于缺少的字符数。另外一种方式是ISO10126Padding,除最后一个字符值等于少的字符数
        // 其他字符填充随机数。
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        // 初始化加密器
        cipher.init(Cipher.ENCRYPT_MODE, key,
                new IvParameterSpec(new byte[cipher.getBlockSize()]));
        // 原始文件流
        FileInputStream inputStream = new FileInputStream(sourceFile);
        // 加密文件流
        FileOutputStream outputStream = new FileOutputStream(encryptFile);
        // 以加密流写入文件
        CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher);
        byte[] tmpArray = new byte[1024];
        int len;
        while((len = cipherInputStream.read(tmpArray)) != -1) { 
   

            outputStream.write(tmpArray, 0, len);
            outputStream.flush();
        }
        cipherInputStream.close();
        inputStream.close();
        outputStream.close();
    }

    /** AES解密 * * @param encryptFile 加密文件 * @param decryptFile 解密文件 * @param password 密钥,128bit * @throws Exception 抛出异常 */
    public static void aesDecryptFile(String encryptFile, String decryptFile, byte[] password) throws Exception { 
   

        // 创建AES密钥,即根据一个字节数组构造一个SecreteKey
        // 而这个SecreteKey是符合指定加密算法密钥规范
        SecretKeySpec key = new SecretKeySpec(password,   "AES");
        // 创建解密引擎(CBC模式)
        // Cipher类支持DES,DES3,AES和RSA加解密
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        // 初始化解密器
        cipher.init(Cipher.DECRYPT_MODE, key,
                new IvParameterSpec(new byte[cipher.getBlockSize()]));
        // 加密文件流
        FileInputStream fileInputStream = new FileInputStream(encryptFile);
        // 解密文件流
        FileOutputStream fileOutputStream = new FileOutputStream(decryptFile);
        // 以解密流写出文件
        CipherOutputStream cipherOutputStream =
                new CipherOutputStream(fileOutputStream, cipher);
        byte[] buffer = new byte[1024];
        int len;
        while((len = fileInputStream.read(buffer)) >= 0) { 
   

            cipherOutputStream.write(buffer, 0, len);
        }
        cipherOutputStream.close();
        fileInputStream.close();
        fileOutputStream.close();
    }

}

字符串加密

import java.security.Provider;
/** * Implementation of Provider for SecureRandom. The implementation supports the * "SHA1PRNG" algorithm described in JavaTM Cryptography Architecture, API * Specification & Reference */
public final class CryptoProvider extends Provider { 
   
    /** * Creates a Provider and puts parameters */
    public CryptoProvider() { 
   
        super("Crypto", 1.0, "HARMONY (SHA1 digest; SecureRandom; SHA1withDSA signature)");
        put("SecureRandom.SHA1PRNG",
            "org.apache.harmony.security.provider.crypto.SHA1PRNG_SecureRandomImpl");
        put("SecureRandom.SHA1PRNG ImplementedIn", "Software");
    }
}


import android.annotation.SuppressLint;
import android.os.Build;

import androidx.annotation.IntDef;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;



/** * AES 工具类 */
public class AESUtil { 
   
    private final static String SHA1PRNG = "SHA1PRNG";

    @IntDef({ 
   Cipher.ENCRYPT_MODE, Cipher.DECRYPT_MODE})
    @interface AESType { 
   }

    /** * Aes加密/解密 * * @param content 字符串 * @param password 密钥 * @param type 加密:{@link Cipher#ENCRYPT_MODE},解密:{@link Cipher#DECRYPT_MODE} * @return 加密/解密结果字符串 */
    public static String aes(String content, String password, @AESType int type) { 
   
        try { 
   
            KeyGenerator generator = KeyGenerator.getInstance("AES");

            SecureRandom secureRandom;
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
   
                secureRandom = SecureRandom.getInstance(SHA1PRNG, new CryptoProvider());
            } else if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
   
                secureRandom = SecureRandom.getInstance(SHA1PRNG, "Crypto");
            } else { 
   
                secureRandom = SecureRandom.getInstance(SHA1PRNG);
            }
            secureRandom.setSeed(password.getBytes());
            generator.init(128, secureRandom);
            SecretKey secretKey = generator.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            @SuppressLint("GetInstance") Cipher cipher = Cipher.getInstance("AES");
            cipher.init(type, key);

            if (type == Cipher.ENCRYPT_MODE) { 
   
                byte[] byteContent = content.getBytes("utf-8");
                return parseByte2HexStr(cipher.doFinal(byteContent));
            } else { 
   
                byte[] byteContent = parseHexStr2Byte(content);
                return new String(cipher.doFinal(byteContent));
            }
        } catch (NoSuchAlgorithmException | BadPaddingException | IllegalBlockSizeException |
                UnsupportedEncodingException | InvalidKeyException | NoSuchPaddingException |
                NoSuchProviderException e) { 
   
            e.printStackTrace();
        }
        return null;
    }

    /** * 二进位组转十六进制字符串 * * @param buf 二进位组 * @return 十六进制字符串 */
    public static String parseByte2HexStr(byte buf[]) { 
   
        StringBuilder sb = new StringBuilder();
        for (byte b : buf) { 
   
            String hex = Integer.toHexString(b & 0xFF);
            if (hex.length() == 1) { 
   
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    /** * 十六进制字符串转二进位组 * * @param hexStr 十六进制字符串 * @return 二进位组 */
    public static byte[] parseHexStr2Byte(String hexStr) { 
   
        if (hexStr.length() < 1) return null;
        byte[] result = new byte[hexStr.length() / 2];

        for (int i = 0; i < hexStr.length() / 2; i++) { 
   
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/145699.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • QThread介绍

    QThread介绍在程序设计中,为了不影响主程序的执行,常常把耗时操作放到一个单独的线程中执行。Qt对多线程操作有着完整的支持,Qt中通过继承QThread并重写run()方法的方式实现多线程代码的编写。针对线程之间的同步与互斥问题,Qt还提供了QMutex、QReadWriteLock、QwaitCondition、QSemaphore等多个类来实现。本篇博客将针对以下几个方面进行讲解[1]QThread的常用接口以及QThread的实现[2]QThread的信号事件[3]QThread执行完后自动释放内存

    2022年5月28日
    119
  • madvr怎么设置hdr_文字识别软件

    madvr怎么设置hdr_文字识别软件PotPlayer播放HDR视频截图PotPlayer播放HDR视频截图Windows10内置视频播放器播放HDR视频Windows10内置视频播放器播放HDR视频从去年开始,支持HDR的电视机已经卖得铺天盖地,支持4KUHDHDR的片源也变得丰富起来,动辄50GB的视频资源让广大10TB级松鼠病晚期患者们兴奋地表示家里屯的硬盘和百兆宽带终于有了用武…

    2022年9月14日
    1
  • staruml使用教程[通俗易懂]

    最近因为实验需要,得用到uml类图。找了个教程。mark下,便于以后学习。 http://blog.csdn.net/monkey_d_meng/article/details/5995610

    2022年4月12日
    49
  • 查看sql_mode_mysql命令行执行sql脚本

    查看sql_mode_mysql命令行执行sql脚本1,执行SQL查看select@@session.sql_mode;全局级别:查看select@@global.sql_mode;2,修改set@@session.sql_mode=’xx_mode’setsessionsql_mode=’xx_mode’全局级别:修改setglobalsql_mode=’xx_mode’;set@@global.sql_mode=’xx_mode’;session均可省略,默认session,仅对当前会话有效全局修改的话,

    2022年9月1日
    6
  • WinRAR去除广告,亲测可用

    WinRAR去除广告,亲测可用原文地址:最新去除中文winrar压缩软件弹出广告方法-百度经验(baidu.com) 此文用作备用文,在原文看不到的情况下,参考本文。感谢原文作者!!! 安装RAR简体中文版 下载并安装Restorator2007 找到RAR安装文件夹,可以双击打开RAR.exe看是否弹出广告 打开Restorator2007 选着左上角“文件-打开”,在“C:\ProgramFiles\WinRAR”文件夹中打开“WinRAR.exe” 打开后.

    2022年6月3日
    42
  • oSIP协议栈浅析

    oSIP协议栈浅析1.oSIP介绍2.oSIP结构分析2.1oSIP结构2.2状态机(FiniteStateMachines)模块2.2.1 概述2.2.2ICT状态机2.2.3NICT状态机2.2.4IST状态机2.2.5NIST状态机2.3解析器(Parsers)模块2.3.

    2022年6月16日
    63

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号