赞
踩
将8位二进制信息编码为ASCII码。
openssl命令 base64
or-enc base64
EVP API EVP_EncodeBlock((unsigned char *)encodeData,sourceDta,16);
EVP_PKEY 对象用于存储公钥和(可选)私钥,以及相关的算法和参数。
支持的类型有:
密码和消息由唯一的 EVP_CIPHER 和 EVP_MD 对象标识。使用下列的函数返回。
const EVP_CIPHER *EVP_aes_128_ctr(void);
const EVP_CIPHER *EVP_aes_128_ccm(void);
const EVP_CIPHER *EVP_aes_128_gcm(void);
const EVP_CIPHER *EVP_aes_128_xts(void);
const EVP_CIPHER *EVP_aes_192_ecb(void);
const EVP_CIPHER *EVP_aes_192_cbc(void);
const EVP_MD *EVP_md2(void);
const EVP_MD *EVP_md4(void);
const EVP_MD *EVP_md5(void);
const EVP_MD *EVP_sha1(void);
const EVP_MD *EVP_sha224(void);
const EVP_MD *EVP_sha256(void);
const EVP_MD *EVP_sha384(void);
const EVP_MD *EVP_sha512(void);
加密步骤:
int envelope_seal(EVP_PKEY **pub_key, unsigned char *plaintext, int plaintext_len, unsigned char **encrypted_key, int *encrypted_key_len, unsigned char *iv, unsigned char *ciphertext) { EVP_CIPHER_CTX *ctx; int ciphertext_len; int len; /*Step1:初始化上下文 */ if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); /*Step2:初始化密封操作,为提供密码生成一个密钥,对密钥进行多次加密,对pub_key提供的每个公钥进行一次加密*/ if(1 != EVP_SealInit(ctx, EVP_aes_256_cbc(), encrypted_key, encrypted_key_len, iv, pub_key, 1)) handleErrors(); /*Step3:提供要加密的信息,并获取加密后的输出*/ if(1 != EVP_SealUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) handleErrors(); ciphertext_len = len; /*Step4:完成加密,密文字节写入*/ if(1 != EVP_SealFinal(ctx, ciphertext + len, &len)) handleErrors(); ciphertext_len += len; /*Step5:清理上下文*/ EVP_CIPHER_CTX_free(ctx); return ciphertext_len; }
解密步骤:
int envelope_open(EVP_PKEY *priv_key, unsigned char *ciphertext, int ciphertext_len, unsigned char *encrypted_key, int encrypted_key_len, unsigned char *iv, unsigned char *plaintext) { EVP_CIPHER_CTX *ctx; int len; int plaintext_len; /*Step1:初始化上下文*/ if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); /*Step2:初始化解密操作,提供非对称私钥和priv_key,加密会话保存在encrypted_key中 */ if(1 != EVP_OpenInit(ctx, EVP_aes_256_cbc(), encrypted_key, encrypted_key_len, iv, priv_key)) handleErrors(); /*Step3:提供解密的消息,获得明文输出*/ if(1 != EVP_OpenUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) handleErrors(); plaintext_len = len; /*Step4:完成解密操作 */ if(1 != EVP_OpenFinal(ctx, plaintext + len, &len)) handleErrors(); plaintext_len += len; /*Step5:清除上下文*/ EVP_CIPHER_CTX_free(ctx); return plaintext_len; }
通过对数据进行加密来提供机密性,并且还通过在加密数据上创建 MAC 标签来提供真实性保证。MAC 标签将确保数据在传输和存储过程中不会被意外更改或恶意篡改。
有EAX,CCM,GCM模式。
int gcm_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *aad, int aad_len, unsigned char *key, unsigned char *iv, int iv_len, unsigned char *ciphertext, unsigned char *tag) { EVP_CIPHER_CTX *ctx; int len; int ciphertext_len; /* 创建和初始化上下文 */ if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); /* 初始化加密操作 */ if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL)) handleErrors(); /* * 设置IV长度,默认为12字节 */ if(1 != EVP_CIPHER_CTX_ctrl(
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。