基于openssl的sm4对称加密算法

基于openssl库的sm4对称加密算法

问题:解密后多出的填充字符暂未想到怎么去除。
sm4C语言实现源码

gcc sm4.c mm.c -lssl -lcrypto

运算结果:

在这里插入图片描述

sm4算法文件

//sm4.c
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/bn.h>
#include <stdio.h>
#include <stdlib.h>

#include "sm4.h"

static void sm4_show_openssl_error(int8_t* title)
{
   
	int8_t buf[1024];

	ERR_error_string_n(ERR_get_error(), buf, sizeof(buf));
	printf("%s: %s\n", title, buf);
}


static const EVP_CIPHER *sm4_get_mode(int8_t *mode)
{
   
	switch (*mode) {
   
	case '0':
		return EVP_sm4_ecb();
	case '1':
		return EVP_sm4_cbc();
	case '2':
		return EVP_sm4_cfb128();
	default:
		return NULL;
	}
}

static int32_t sm4_get_rand(uint8_t *out, uint32_t len)
{
      
    BIGNUM *rnd;
    uint8_t buf[32];
    uint32_t idx;
    
    rnd = BN_new();
    if (NULL == rnd) {
   
        return -1;
    }
    
    if (!BN_rand(rnd, 256, 1, 1)) {
   
        return -1;
    }
    
    if (BN_bn2binpad(rnd, buf, 32) != 32) {
   
        return -1;
    }
    BN_free(rnd);
    show_hex("rand", buf, 32);
    
    for (idx = 0; idx < len; idx
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值