【Android 安全】DEX 加密 ( 代理 Application 开发 | 项目中配置 OpenSSL 开源库 | 使用 OpenSSL 开源库解密 dex 文件 )(二)

简介: 【Android 安全】DEX 加密 ( 代理 Application 开发 | 项目中配置 OpenSSL 开源库 | 使用 OpenSSL 开源库解密 dex 文件 )(二)

二、OpenSSL 开源库解密参考代码


OpenSSL 加密解密代码参考 OpenSSL 源码跟目录下 demos/evp/aesccm.c 官方示例代码 , 在 Android 的 jni 代码中按照下面示例代码中的 aes_ccm_decrypt 方法解密即可 ;


/*
 * Simple AES CCM test program, uses the same NIST data used for the FIPS
 * self test but uses the application level EVP APIs.
 */
#include <stdio.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
/* AES-CCM test data from NIST public test vectors */
static const unsigned char ccm_key[] = {
    0xce, 0xb0, 0x09, 0xae, 0xa4, 0x45, 0x44, 0x51, 0xfe, 0xad, 0xf0, 0xe6,
    0xb3, 0x6f, 0x45, 0x55, 0x5d, 0xd0, 0x47, 0x23, 0xba, 0xa4, 0x48, 0xe8
};
static const unsigned char ccm_nonce[] = {
    0x76, 0x40, 0x43, 0xc4, 0x94, 0x60, 0xb7
};
static const unsigned char ccm_adata[] = {
    0x6e, 0x80, 0xdd, 0x7f, 0x1b, 0xad, 0xf3, 0xa1, 0xc9, 0xab, 0x25, 0xc7,
    0x5f, 0x10, 0xbd, 0xe7, 0x8c, 0x23, 0xfa, 0x0e, 0xb8, 0xf9, 0xaa, 0xa5,
    0x3a, 0xde, 0xfb, 0xf4, 0xcb, 0xf7, 0x8f, 0xe4
};
static const unsigned char ccm_pt[] = {
    0xc8, 0xd2, 0x75, 0xf9, 0x19, 0xe1, 0x7d, 0x7f, 0xe6, 0x9c, 0x2a, 0x1f,
    0x58, 0x93, 0x9d, 0xfe, 0x4d, 0x40, 0x37, 0x91, 0xb5, 0xdf, 0x13, 0x10
};
static const unsigned char ccm_ct[] = {
    0x8a, 0x0f, 0x3d, 0x82, 0x29, 0xe4, 0x8e, 0x74, 0x87, 0xfd, 0x95, 0xa2,
    0x8a, 0xd3, 0x92, 0xc8, 0x0b, 0x36, 0x81, 0xd4, 0xfb, 0xc7, 0xbb, 0xfd
};
static const unsigned char ccm_tag[] = {
    0x2d, 0xd6, 0xef, 0x1c, 0x45, 0xd4, 0xcc, 0xb7, 0x23, 0xdc, 0x07, 0x44,
    0x14, 0xdb, 0x50, 0x6d
};
void aes_ccm_encrypt(void)
{
    EVP_CIPHER_CTX *ctx;
    int outlen, tmplen;
    unsigned char outbuf[1024];
    printf("AES CCM Encrypt:\n");
    printf("Plaintext:\n");
    BIO_dump_fp(stdout, ccm_pt, sizeof(ccm_pt));
    ctx = EVP_CIPHER_CTX_new();
    /* Set cipher type and mode */
    EVP_EncryptInit_ex(ctx, EVP_aes_192_ccm(), NULL, NULL, NULL);
    /* Set nonce length if default 96 bits is not appropriate */
    EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, sizeof(ccm_nonce),
                        NULL);
    /* Set tag length */
    EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, sizeof(ccm_tag), NULL);
    /* Initialise key and IV */
    EVP_EncryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce);
    /* Set plaintext length: only needed if AAD is used */
    EVP_EncryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_pt));
    /* Zero or one call to specify any AAD */
    EVP_EncryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata));
    /* Encrypt plaintext: can only be called once */
    EVP_EncryptUpdate(ctx, outbuf, &outlen, ccm_pt, sizeof(ccm_pt));
    /* Output encrypted block */
    printf("Ciphertext:\n");
    BIO_dump_fp(stdout, outbuf, outlen);
    /* Finalise: note get no output for CCM */
    EVP_EncryptFinal_ex(ctx, outbuf, &outlen);
    /* Get tag */
    EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16, outbuf);
    /* Output tag */
    printf("Tag:\n");
    BIO_dump_fp(stdout, outbuf, 16);
    EVP_CIPHER_CTX_free(ctx);
}
void aes_ccm_decrypt(void)
{
    EVP_CIPHER_CTX *ctx;
    int outlen, tmplen, rv;
    unsigned char outbuf[1024];
    printf("AES CCM Derypt:\n");
    printf("Ciphertext:\n");
    BIO_dump_fp(stdout, ccm_ct, sizeof(ccm_ct));
    ctx = EVP_CIPHER_CTX_new();
    /* Select cipher */
    EVP_DecryptInit_ex(ctx, EVP_aes_192_ccm(), NULL, NULL, NULL);
    /* Set nonce length, omit for 96 bits */
    EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, sizeof(ccm_nonce),
                        NULL);
    /* Set expected tag value */
    EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
                        sizeof(ccm_tag), (void *)ccm_tag);
    /* Specify key and IV */
    EVP_DecryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce);
    /* Set ciphertext length: only needed if we have AAD */
    EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_ct));
    /* Zero or one call to specify any AAD */
    EVP_DecryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata));
    /* Decrypt plaintext, verify tag: can only be called once */
    rv = EVP_DecryptUpdate(ctx, outbuf, &outlen, ccm_ct, sizeof(ccm_ct));
    /* Output decrypted block: if tag verify failed we get nothing */
    if (rv > 0) {
        printf("Plaintext:\n");
        BIO_dump_fp(stdout, outbuf, outlen);
    } else
        printf("Plaintext not available: tag verify failed.\n");
    EVP_CIPHER_CTX_free(ctx);
}
int main(int argc, char **argv)
{
    aes_ccm_encrypt();
    aes_ccm_decrypt();
}





三、解密 dex 文件的 Java 代码


该类仅作为调用 Jni 中的 OpenSSL 解密函数的桥梁 ;


package kim.hsl.multipledex;
import java.io.File;
import java.io.RandomAccessFile;
public class OpenSSL {
    static {
        System.loadLibrary("openssl");
    }
    /**
     * 从文件中读取 Byte 数组
     * @param file
     * @return
     * @throws Exception
     */
    public static byte[] getBytes(File file) throws Exception {
        RandomAccessFile r = new RandomAccessFile(file, "r");
        byte[] buffer = new byte[(int) r.length()];
        r.readFully(buffer);
        r.close();
        return buffer;
    }
    /**
     * 调用 OpenSSL 解密 dex 文件
     * @param data
     * @param path
     */
    public static native void decrypt(byte[] data, String path);
}








四、解密 dex 文件的 Jni 代码


解密 dex 文件的方法 , 调用 OpenSSL 开源库中的 api ;


#include <jni.h>
#include <stdio.h>
#include <android/log.h>
#include <malloc.h>
#include <string.h>
#include <openssl/evp.h>
#include "logging_macros.h"
//密钥
static uint8_t *userkey = "abcdefghijklmnop";
JNIEXPORT void JNICALL
Java_kim_hsl_multipledex_OpenSSL_decrypt(JNIEnv *env, jclass clazz, jbyteArray data, jstring path) {
    // 将 Java Byte 数组转为 C 数组
    jbyte *src = (*env)->GetByteArrayElements(env, data, NULL);
    // 将 Java String 字符串转为 C char* 字符串
    const char *filePath = (*env)->GetStringUTFChars(env, path, 0);
    // 获取 Java Byte 数组长度
    int srcLen = (*env)->GetArrayLength(env, data);
    /*
     * 下面的代码是从 OpenSSL 源码跟目录下 demos/evp/aesccm.c 中拷贝并修改
     */
    // 加密解密的上下文
    EVP_CIPHER_CTX *ctx;
    int outlen, tmplen;
    unsigned char outbuf[1024];
    // 创建加密解密上下文
    ctx = EVP_CIPHER_CTX_new();
    /* Select cipher 配置上下文解码参数
     * 配置加密模式 :
     * Java 中的加密算法类型 "AES/ECB/PKCS5Padding" , 使用 ecb 模式
     * EVP_aes_192_ecb() 配置 ecb 模式
     * AES 有五种加密模式 : CBC、ECB、CTR、OCF、CFB
     * 配置密钥 :
     * Java 中定义的密钥是 "kimhslmultiplede"
     */
    EVP_DecryptInit_ex(ctx, EVP_aes_192_ecb(), NULL, "kimhslmultiplede", NULL);
    // 申请解密输出数据内存, 申请内存长度与密文长度一样即可
    // AES 加密密文比明文要长
    uint8_t *out = malloc(srcLen);
    // 将申请的内存设置为 0
    memset(out, 0, srcLen);
    // 记录解密总长度
    int totalLen = 0;
    /*
     * 解密操作
     * int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
                                 int *outl, const unsigned char *in, int inl);
     * 解密 inl 长度的 in , 解密为 outl 长度的 out
     * 解密的输入数据是 src, 长度为 srcLen 字节, 注意该长度是 int 类型
     * 解密的输出数据是 out, 长度为 srcLen 字节, 注意该长度是 int* 指针类型
     */
    EVP_DecryptUpdate(ctx, out, &outlen, src, srcLen);
    totalLen += outlen; //更新总长度
    /*
     * int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm,
                                   int *outl);
     * 解密时, 每次解密 16 字节, 如果超过了 16 字节 , 就会剩余一部分无法解密,
     * 之前的 out 指针已经解密了 outlen 长度, 此时接着后续解密, 指针需要进行改变 out + outlen
     * 此时需要调用该函数 , 解密剩余内容
     */
    EVP_DecryptFinal_ex(ctx, out + outlen, &outlen);
    totalLen += outlen; //更新总长度, 此时 totalLen 就是总长度
    // 解密完成, 释放上下文对象
    EVP_CIPHER_CTX_free(ctx);
    // 将解密出的明文, 写出到给定的 Java 文件中
    FILE *file = fopen(path, "wb");
    // 写出 out 指针指向的数据 , 写出个数 totalLen * 1 , 写出到 file 文件中
    fwrite(out, totalLen, 1, file);
    // 关闭文件
    fclose(file);
    // 释放解密出的密文内存
    free(out);
    // 释放 Java 引用
    (*env)->ReleaseByteArrayElements(env, data, src, 0);
    (*env)->ReleaseStringUTFChars(env, path, path);
}




目录
相关文章
|
安全 算法 Java
在Spring Boot中应用Jasypt以加密配置信息。
通过以上步骤,可以在Spring Boot应用中有效地利用Jasypt对配置信息进行加密,这样即使配置文件被泄露,其中的敏感信息也不会直接暴露给攻击者。这是一种在不牺牲操作复杂度的情况下提升应用安全性的简便方法。
1909 10
|
安全 Java 数据库
Jasypt加密数据库配置信息
本文介绍了使用 Jasypt 对配置文件中的公网数据库认证信息进行加密的方法,以提升系统安全性。主要内容包括:1. 背景介绍;2. 前期准备,如依赖导入及版本选择;3. 生成密钥并实现加解密测试;4. 在配置文件中应用加密后的密码,并通过测试接口验证解密结果。确保密码安全的同时,保障系统的正常运行。
1103 3
Jasypt加密数据库配置信息
|
存储 缓存 安全
php项目源码加密之Zend Guard Loader加密的代码解密可能性很小-优雅草卓伊凡
php项目源码加密之Zend Guard Loader加密的代码解密可能性很小-优雅草卓伊凡
739 2
|
人工智能 安全 Java
Spring Boot yml 配置敏感信息加密
本文介绍了如何在 Spring Boot 项目中使用 Jasypt 实现配置文件加密,包含添加依赖、配置密钥、生成加密值、在配置中使用加密值及验证步骤,并提供了注意事项,确保敏感信息的安全管理。
1970 1
|
存储 安全 Cloud Native
云原生安全必修课:RDS透明加密(TDE)与数据脱敏联动实施方案
云原生环境下,数据泄露风险日益严峻,传统安全方案面临加密与脱敏割裂、保护不连续、权限控制粗放三大挑战。本方案融合TDE透明加密与动态数据脱敏技术,构建存储-传输-计算全链路防护体系,通过SQL级加密与角色化脱敏规则,实现细粒度数据保护。结合密钥管理、权限控制与多云适配,提升安全性与性能,广泛适用于金融、医疗等高安全要求场景。
|
人工智能 算法 前端开发
打造一款属于我的在线加密实验室 —— CodeBuddy 实现 CryptoXLab 项目实录
打造一款属于我的在线加密实验室 —— CodeBuddy 实现 CryptoXLab 项目实录
364 15
|
存储 安全 数据安全/隐私保护
Hyper V文件复制安全:加密与访问控制
在Hyper-V环境中,确保文件复制的安全性至关重要。主要措施包括:启用数据加密、使用HTTPS协议和磁盘加密技术(如BitLocker)保护数据传输和存储;通过身份验证、权限管理和审核日志控制访问;定期更新补丁、实施网络隔离及制定备份恢复策略。这些多层次的安全措施共同防止未经授权的访问和数据泄露,保障数据安全。
Hyper V文件复制安全:加密与访问控制

热门文章

最新文章