iOS RSA,AES加密(使用openssl)

话不多说直接来。

https://github.com/x2on/OpenSSL-for-iPhone下载下来后打开终端cd到这个目录,直接./build-libssl.sh运行脚本。

等待个几分钟,当看到 

Build library for iOS…
Build library for tvOS…
Done

这里我们就编译好需要的静态文件了,这里不得不感谢大神做的贡献.

iOS RSA,AES加密(使用openssl)_第1张图片

把这两个.a和include拖入项目中,然后设置Header Search paths,文件手动拖入进去就会生成路径了,接下来撸代码。

RSA 

@interface RSAEn : NSObject
int init();
int public_encrypt_to_hex(const char* src, int strlength, char* dest);
int private_decrypt_from_hex(const char* src, int srcLength, char* dest);
@end
#import "RSAEn.h"
#include "rsa.h"
#include "pem.h"
#include "aes.h"
#include "ssl.h"
@implementation RSAEn

enum RSA_KEY_TYPE 
{
    RSA_PUBLIC_KEY = 0,
    RSA_PRIVATE_KEY = 1
};


RSA* rsa_public;
RSA* rsa_private;

RSA* create_rsa_from_file(const char *file_name, int k_type)
{
    FILE *fp = fopen(file_name, "rb");
    if (fp == NULL)
    {
        printf("file [%s] open failed!\n", file_name);
    }
    
    RSA *rsa = RSA_new();
    if (k_type == RSA_PUBLIC_KEY)
    {
        rsa = PEM_read_RSA_PUBKEY(fp, &rsa, NULL, NULL);
    }
    else if (k_type == RSA_PRIVATE_KEY)
    {
        rsa = PEM_read_RSAPrivateKey(fp, &rsa, NULL, NULL);
    }
    else
    {
        return NULL;
    }
    
    return rsa;
}

int init()
{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"public.pem" ofType:nil];
    const char * puclicFile = [path UTF8String];
    rsa_public = create_rsa_from_file(puclicFile, RSA_PUBLIC_KEY);
    if (rsa_public == NULL)
    {
        printf("Failed to create public RSA.");
        return -1;
    }
    
    
    
    NSString *privatePath = [[NSBundle mainBundle]pathForResource:@"private.pem" ofType:nil];
    const char * privateFile = [privatePath UTF8String];

    rsa_private = create_rsa_from_file(privateFile, RSA_PRIVATE_KEY);
    if (rsa_private == NULL)
    {
        printf("Failed to create private RSA.");
        return -1;
    }
    
    return 0;
}

RSA* create_rsa(unsigned char *key_str, int k_type)
{
    RSA* rsa = NULL;
    BIO* keybio = BIO_new_mem_buf(key_str, -1);
    if (keybio == NULL)
    {
        return NULL;
    }
    
    if (k_type == RSA_PRIVATE_KEY)
    {
        rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL);
    }
    else if (k_type == RSA_PUBLIC_KEY)
    {
        rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa, NULL, NULL);
    }
    else
    {
        return NULL;
    }
    return NULL;
}

int bin_to_hex(const unsigned char *src, int srcLength, char *dest)
{
    const char *alphabet = "0123456789abcdef";
    int nDestLength = 0;
    for (int i = 0; i < srcLength; i++)
    {
        unsigned char ch = src[i];
        unsigned char low = ch & 0x0f;
        unsigned char high = (ch >> 4) & 0x0f;
        dest[2 * i] = alphabet[high];
        dest[(2 * i) + 1] = alphabet[low];
        
        nDestLength += 2;
    }
    return nDestLength;
}

char char_to_bin(char ch)
{
    if (ch >= '0' && ch <= '9')
    {
        ch = ch - '0';
    }
    else if (ch >= 'a' && ch <= 'z')
    {
        ch = ch - 'a' + 10;
    }
    else if (ch >= 'A' && ch <= 'Z')
    {
        ch = ch - 'A' + 10;
    }
    
    return ch;
}
int hex_to_bin(const char *src, int srcLength, unsigned char *dest)
{
    int nDestLength = 0;
    for (int i = 0; i< srcLength; i += 2)
    {
        char h = char_to_bin(src[i]);
        char l = char_to_bin(src[i + 1]);
        
        dest[i / 2] = h;
        dest[i / 2] = dest[i / 2] << 4;
        dest[i / 2] |= l;
        
        nDestLength++;
    }
    return nDestLength;
}

int public_encrypt_to_hex(const char* src, int strlength, char* dest)
{
    unsigned char destTemp[128] = { 0 };
    
    int res = RSA_public_encrypt(strlength, (unsigned char*)src, destTemp, rsa_public, RSA_PKCS1_PADDING);
    if (res == -1)
    {
        return -1;
    }
    
    char cipher_hex[512] = { 0 };
    int cipher_hex_len = 0;
    int nLen = bin_to_hex(destTemp, res, dest);
    
    return nLen;
}

int private_decrypt_from_hex(const char* src, int srcLength, char* dest)
{
    unsigned char destTemp[512] = { 0 };
    unsigned char plain_txt[512] = { 0 };
    
    int cipher_bin_len = 0;
    int plain_txt_len = 0;
    
    int nLen = hex_to_bin(src, srcLength, destTemp);
    
    int res = RSA_private_decrypt(nLen, destTemp, (unsigned char*)dest, rsa_private, RSA_PKCS1_PADDING);
    if (res == -1)
    {
        return -1;
    }
    return 0;
}

@end
RSA测试方式
    init();
    char* key = "ceshiwenben";
    char szBufEn[1024] = { 0 };
    char szBufDe[1024] = { 0 };
   int nLenEn = public_encrypt_to_hex(key, strlen(key), szBufEn);
   
   int nLenDe = private_decrypt_from_hex(szBufEn, nLenEn, szBufDe);

AES

@interface AESEn : NSObject

int evp_encrypt(const unsigned char *inData, int in_len, const unsigned char *key, unsigned char *outData);
int evp_decrypt(const unsigned char *inData, int in_len, const unsigned char *key, unsigned char *outData);

@end
#import "AESEn.h"
#import "evp.h"

@implementation AESEn
//加密
int evp_encrypt(const unsigned char *inData, int in_len, const unsigned char *key, unsigned char *outData)
{
    unsigned char iv[16] = { 0 };
    int inLen = in_len;//strlen((char *)inData);
    int encLen = 0;
    int outlen = 0;
    unsigned char encData[1024];
    
    EVP_CIPHER_CTX *ctx;
    ctx = EVP_CIPHER_CTX_new();
    
    EVP_CipherInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, iv, 1);
    EVP_CipherUpdate(ctx, outData, &outlen, inData, inLen);
    encLen = outlen;
    EVP_CipherFinal(ctx, outData + outlen, &outlen);
    encLen += outlen;
    EVP_CIPHER_CTX_free(ctx);
    
    return encLen;
}

//解密
int evp_decrypt(const unsigned char *inData, int in_len, const unsigned char *key, unsigned char *outData)
{
    unsigned char iv[16] = { 0 };
    int inLen = in_len;//strlen((char *)inData);
    int outlen = 0;
    unsigned char encData[1024];
    int decLen = 0;
    unsigned char decData[1024];
    EVP_CIPHER_CTX *ctx2;
    ctx2 = EVP_CIPHER_CTX_new();
    EVP_CipherInit_ex(ctx2, EVP_aes_128_ecb(), NULL, key, iv, 0);
    EVP_CipherUpdate(ctx2, outData, &outlen, inData, inLen);
    decLen = outlen;
    EVP_CipherFinal(ctx2, outData + outlen, &outlen);
    decLen += outlen;
    EVP_CIPHER_CTX_free(ctx2);
    
    outData[decLen] = '\0';
    printf("decrypt: %s\n", outData);
    return decLen;
}
@end
AES测试方法
    char key[16] = "ceshiwenben";
     
    char* indata = "231232";
    char szBufEn1[1024] = { 0 };
    char szBufDe1[1024] = { 0 };
    
    int len =  evp_encrypt(indata, strlen(indata), key, szBufEn1);
   
    evp_decrypt(szBufEn1,len, key, szBufDe1);




你可能感兴趣的:(Objective-C)