当前位置:   article > 正文

Openssl C++ API_openssl wrong signature length evp_pkey_size 249 p

openssl wrong signature length evp_pkey_size 249 packet_remaining 256

Openssl C++ API

Base64

将8位二进制信息编码为ASCII码

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qIU7AK1Y-1623726161340)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210615091934664.png)]

openssl命令 base64 or-enc base64

EVP API EVP_EncodeBlock((unsigned char *)encodeData,sourceDta,16);

EVP

EVP_PKEY 对象用于存储公钥和(可选)私钥,以及相关的算法和参数。

支持的类型有:

  • EVP_PKEY_EC:椭圆曲线密钥(用于 ECDSA 和 ECDH) - 支持签名/验证操作和密钥派生
  • EVP_PKEY_RSA:RSA - 支持签名/验证和加密/解密
  • EVP_PKEY_DH:Diffie Hellman - 用于密钥推导
  • EVP_PKEY_DSA:用于签名/验证的 DSA 密钥
  • EVP_PKEY_HMAC:用于生成消息认证码的 HMAC 密钥
  • EVP_PKEY_CMAC:用于生成消息验证码的 CMAC 密钥

密码和消息由唯一的 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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

EVP进行非对称加密和解密

加密步骤:

  • 初始化上下文
  • 初始化密封操作,提供将使用的堆成密码,已经用于加密会话密匙的一组公钥
  • 提供要加密的信息
  • 完成加密操作
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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

解密步骤:

  • 初始化上下文
  • 初始化打开操作,提供对称密码用于解密
  • 提供要解密的消息,并使用会话密钥解密
  • 完成解密操作
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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

EVP认证加解密

通过对数据进行加密来提供机密性,并且还通过在加密数据上创建 MAC 标签来提供真实性保证。MAC 标签将确保数据在传输和存储过程中不会被意外更改或恶意篡改。

有EAX,CCM,GCM模式。

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(
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/51952
推荐阅读
相关标签
  

闽ICP备14008679号