Android集成OpenSSL实现加解密-集成

  1. 导入so
    将编译生成的 OpenSSL 动态库文件(.so 文件)复制到你的 Android 项目的 libs 目录中
    在这里插入图片描述

  2. 导入头文件
    将编译生成的include文件夹导入到项目中
    在这里插入图片描述

  3. build.gradle添加配置

       defaultConfig {
            ……
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            externalNativeBuild {
                cmake {
                    cppFlags "-std=c++11 -frtti -fexceptions"
                    //目标平台,若需要更多平台的请自行配置
                    abiFilters 'arm64-v8a'
                }
            }
        }
    
       ……
        sourceSets {
            main {
                jniLibs.srcDirs = ['src/main/jniLibs']
            }
        }
        externalNativeBuild {
            cmake {
                path file('src/main/cpp/CMakeLists.txt')
                version '3.22.1'
            }
        }
    
  4. 修改CMakeLists.txt

    cmake_minimum_required(VERSION 3.22.1)
    
    project("jnitest")
    
    set(JNI_LIBS_DIR ${CMAKE_SOURCES_DIR}/src/main/jniLibs)
    
    add_library( # Sets the name of the library.
            jnitest
    
            # Sets the library as a shared library.
            SHARED
    
            # Provides a relative path to your source file(s).
            native-lib.cpp)
    
    # 1、添加导入库,类似给每个库起个名字
    add_library(
            openssl # 模块名称,随意
            SHARED # 表示共享库
            IMPORTED # 表示当前是导入库
    )
    add_library(
            ssl # 模块名称,随意
            SHARED # 表示共享库
            IMPORTED # 表示当前是导入库
    ) 
    # 2、将上一步声明的库名字,和具体的库(so文件)关联到一起
    se t_target_properties(
             openssl # 库的名称
             PROPERTIES IMPORTED_LOCATION # 表示当前库是导入的方式
             ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libcrypto.so # so 动态库的具体路径 
    )
    set _target_properties(
            ssl # 库的名称
             PROPERTIES IMPORTED_LOCATION # 表示当前库是导入的方式
            ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libssl.so # so 动态库的具体路径 
    ) 
    
    # 3 、如果第三方库有头文件的,为了在编码的时候可以有提示,使用下面的指令进行指定
    # 指定头文件目录,指定后编译器会扫描这个目录,编码时可以进行提示
    include_directories( ${CMAKE_SOURCE_DIR}/include)
    
    find_library( # Sets the name of the path variable.
            log-lib
    
            # Specifies the name of the NDK library that
            # you want CMake to locate.
            log)
    
    target_link_libraries(jnitest openssl ssl  ${log-lib})
    
  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在iOS中使用OpenSSL实现AES-GCM和ECB解密需要以下步骤: 1. 下载OpenSSL库并将其添到iOS项目中。 2. 导入OpenSSL库头文件: ```objc #import <openssl/evp.h> #import <openssl/aes.h> ``` 3. 实现AES-GCM解密: ```objc // AES-GCM密 + (NSData *)AESGCMEncryptWithKey:(NSData *)key iv:(NSData *)iv aad:(NSData *)aad plainText:(NSData *)plainText tagLength:(NSUInteger)tagLength { EVP_CIPHER_CTX *ctx; int len, outlen; unsigned char *outbuf; NSData *tag = nil; NSData *cipherText = nil; // 初始化EVP_CIPHER_CTX ctx = EVP_CIPHER_CTX_new(); EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL); // 设置key和iv EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, (int)iv.length, NULL); EVP_EncryptInit_ex(ctx, NULL, NULL, key.bytes, iv.bytes); // 设置aad EVP_EncryptUpdate(ctx, NULL, &len, aad.bytes, (int)aad.length); // 密明文 outbuf = malloc(plainText.length + AES_BLOCK_SIZE); EVP_EncryptUpdate(ctx, outbuf, &len, plainText.bytes, (int)plainText.length); cipherText = [NSData dataWithBytes:outbuf length:len]; free(outbuf); // 获取tag outbuf = malloc(tagLength); EVP_EncryptFinal_ex(ctx, outbuf, &outlen); tag = [NSData dataWithBytes:outbuf length:outlen]; free(outbuf); // 释放EVP_CIPHER_CTX EVP_CIPHER_CTX_free(ctx); // 返回密结果和tag NSMutableData *resultData = [NSMutableData dataWithData:cipherText]; [resultData appendData:tag]; return resultData; } // AES-GCM解密 + (NSData *)AESGCMDecryptWithKey:(NSData *)key iv:(NSData *)iv aad:(NSData *)aad cipherText:(NSData *)cipherText tagLength:(NSUInteger)tagLength { EVP_CIPHER_CTX *ctx; int len, outlen; unsigned char *outbuf; NSData *tag = nil; NSData *plainText = nil; // 初始化EVP_CIPHER_CTX ctx = EVP_CIPHER_CTX_new(); EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL); // 设置key和iv EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, (int)iv.length, NULL); EVP_DecryptInit_ex(ctx, NULL, NULL, key.bytes, iv.bytes); // 设置aad EVP_DecryptUpdate(ctx, NULL, &len, aad.bytes, (int)aad.length); // 解密密文 outbuf = malloc(cipherText.length); EVP_DecryptUpdate(ctx, outbuf, &len, cipherText.bytes, (int)cipherText.length - tagLength); plainText = [NSData dataWithBytes:outbuf length:len]; free(outbuf); // 获取tag tag = [cipherText subdataWithRange:NSMakeRange(cipherText.length - tagLength, tagLength)]; EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, (int)tag.length, (void *)tag.bytes); // 验证tag if (EVP_DecryptFinal_ex(ctx, NULL, &outlen) <= 0) { return nil; // tag验证失败 } // 释放EVP_CIPHER_CTX EVP_CIPHER_CTX_free(ctx); // 返回解密结果 return plainText; } ``` 4. 实现ECB解密: ```objc // ECB密 + (NSData *)ECBEncryptWithKey:(NSData *)key plainText:(NSData *)plainText { EVP_CIPHER_CTX *ctx; int len; unsigned char *outbuf; NSData *cipherText = nil; // 初始化EVP_CIPHER_CTX ctx = EVP_CIPHER_CTX_new(); EVP_EncryptInit_ex(ctx, EVP_aes_256_ecb(), NULL, key.bytes, NULL); // 密明文 outbuf = malloc(plainText.length + AES_BLOCK_SIZE); EVP_EncryptUpdate(ctx, outbuf, &len, plainText.bytes, (int)plainText.length); cipherText = [NSData dataWithBytes:outbuf length:len]; free(outbuf); // 释放EVP_CIPHER_CTX EVP_CIPHER_CTX_free(ctx); // 返回密结果 return cipherText; } // ECB解密 + (NSData *)ECBDecryptWithKey:(NSData *)key cipherText:(NSData *)cipherText { EVP_CIPHER_CTX *ctx; int len; unsigned char *outbuf; NSData *plainText = nil; // 初始化EVP_CIPHER_CTX ctx = EVP_CIPHER_CTX_new(); EVP_DecryptInit_ex(ctx, EVP_aes_256_ecb(), NULL, key.bytes, NULL); // 解密密文 outbuf = malloc(cipherText.length); EVP_DecryptUpdate(ctx, outbuf, &len, cipherText.bytes, (int)cipherText.length); plainText = [NSData dataWithBytes:outbuf length:len]; free(outbuf); // 释放EVP_CIPHER_CTX EVP_CIPHER_CTX_free(ctx); // 返回解密结果 return plainText; } ``` 以上就是使用OpenSSL实现AES-GCM和ECB解密的步骤,需要注意的是,在iOS13及以上版本中,Apple已经将OpenSSL库废弃,推荐使用自带的CryptoKit框架实现解密

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值