ubuntu环境下openssl库的简单使用

安装

bash 复制代码
sudo apt-get install libssl-dev

aes算法demo

编译:gcc aes.c -lssl -lcrypto -o aes

运行:./aes

c 复制代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<openssl/aes.h>
 
#define AES_KEY_SIZE 128 // AES密钥长度
#define AES_BLOCK_SIZE 16 // AES分块大小
 
// 加密函数
void aes_encrypt(const unsigned char *plaintext, unsigned char *ciphertext, const unsigned char *key) 
{
    AES_KEY aes_key;
    AES_set_encrypt_key(key, AES_KEY_SIZE, &aes_key);
    AES_encrypt(plaintext, ciphertext, &aes_key);
}
 
// 解密函数
void aes_decrypt(const unsigned char *ciphertext, unsigned char *plaintext, const unsigned char *key)
{
    AES_KEY aes_key;
    AES_set_decrypt_key(key, AES_KEY_SIZE, &aes_key);
    AES_decrypt(ciphertext, plaintext, &aes_key);
}
 
int main(int argc, char **argv) 
{
    // 明文密码
    const char *plaintext_password = "my_password";
    size_t plaintext_password_len = strlen(plaintext_password);
 
    // AES密钥
    const unsigned char aes_key[] = { 0x7b, 0xf3, 0x5c, 0xd6, 0x9c, 0x47, 0x5d, 0x5e, 0x6f, 0x1d, 0x7a, 0x23, 0x18, 0x7b, 0xf9, 0x34 };
 
    // 分配加密后的密文空间
    size_t ciphertext_password_len = ((plaintext_password_len + AES_BLOCK_SIZE - 1) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
    unsigned char *ciphertext_password = malloc(ciphertext_password_len);
 
    // 对明文密码进行AES加密
    aes_encrypt((const unsigned char *)plaintext_password, ciphertext_password, aes_key);
 
    // 输出加密后的密码
    printf("加密后的密码:");
    for (size_t i = 0; i < ciphertext_password_len; i++) {
        printf("%02x", ciphertext_password[i]);
    }
    printf("\n");
 
    // 分配解密后的明文空间
    unsigned char *decrypted_password = malloc(plaintext_password_len);
 
    // 对密文密码进行AES解密
    aes_decrypt(ciphertext_password, decrypted_password, aes_key);
 
    // 输出解密后的密码
    printf("解密后的密码:%s\n", decrypted_password);
 
    // 释放空间
    free(ciphertext_password);
    free(decrypted_password); 
    return 0;
}

des算法demo

编译:gcc des.c -lssl -lcrypto des

运行:./des

c 复制代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<openssl/des.h>

int main(int argc,char **argv)
{
		DES_cblock key;
		//随机密钥
		DES_random_key(&key);
    
       	DES_key_schedule schedule;
       	//转换成schedule
       	DES_set_key_checked(&key, &schedule);
    
       	const_DES_cblock input = "hehehe";
       	DES_cblock output;
    
       	printf("cleartext: %s\n", input);
    
       	//加密
       	DES_ecb_encrypt(&input, &output, &schedule, DES_ENCRYPT);
       	printf("Encrypted!\n");
    
       	printf("ciphertext: ");
       	int i;
       	for (i = 0; i < sizeof(input); i++)
			printf("%02x", output[i]);
		printf("\n");
    
       	//解密
       	DES_ecb_encrypt(&output, &input, &schedule, DES_DECRYPT);
       	printf("Decrypted!\n");
       	printf("cleartext:%s\n", input);

       	return 0;
}

sm1算法demo

c 复制代码

ssf33算法demo

c 复制代码

sm4算法demo

编译:gcc sm4.c -lssl -lcrypto -o sm4

运行:./sm4

c 复制代码
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 加密函数
void encryptSM4(const unsigned char *plaintext, int plaintextLength, const unsigned char *key, unsigned char *ciphertext, int *ciphertextLength) {
    EVP_CIPHER_CTX *ctx;
    int len;

    // 创建并初始化上下文
    ctx = EVP_CIPHER_CTX_new();
    EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, key, NULL);

    // 加密数据
    EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintextLength);
    *ciphertextLength = len;

    // 结束加密过程
    EVP_EncryptFinal_ex(ctx, ciphertext + len, &len);
    *ciphertextLength += len;

    // 释放上下文
    EVP_CIPHER_CTX_free(ctx);
}

// 解密函数
void decryptSM4(const unsigned char *ciphertext, int ciphertextLength, const unsigned char *key, unsigned char *plaintext, int *plaintextLength) {
    EVP_CIPHER_CTX *ctx;
    int len;

    // 创建并初始化上下文
    ctx = EVP_CIPHER_CTX_new();
    EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, key, NULL);

    // 解密数据
    EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertextLength);
    *plaintextLength = len;

    // 结束解密过程
    EVP_DecryptFinal_ex(ctx, plaintext + len, &len);
    *plaintextLength += len;

    // 释放上下文
    EVP_CIPHER_CTX_free(ctx);
}

int main(int argc, char **argv)
{
    const unsigned char plaintext[] = "Hello, SM4!"; // 明文
    const unsigned char key[] = "0123456789abcdef"; // 128位密钥
    unsigned char ciphertext[256];
    unsigned char decryptedText[256];
    int ciphertextLength = 0;
    int decryptedLength = 0;

    // 加密
    encryptSM4(plaintext, sizeof(plaintext) - 1, key, ciphertext, &ciphertextLength);

    // 打印密文
    printf("%s", "Ciphertext:");
    for (int i = 0; i < ciphertextLength; ++i) {
        printf("%02x", ciphertext[i]);
    }
    printf("\n");

    // 解密
    decryptSM4(ciphertext, ciphertextLength, key, decryptedText, &decryptedLength);

    // 打印解密后的明文
    printf("%s\n", decryptedText);
    return 0;
}

sm6算法demo

c 复制代码
相关推荐
小莞尔2 天前
【51单片机】【protues仿真】基于51单片机的篮球计时计分器系统
c语言·stm32·单片机·嵌入式硬件·51单片机
小莞尔2 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
绿箭柠檬茶2 天前
Ubuntu 服务器配置转发网络访问
服务器·网络·ubuntu
风_峰2 天前
Ubuntu Linux SD卡分区操作
嵌入式硬件·ubuntu·fpga开发
liujing102329292 天前
Day03_刷题niuke20250915
c语言
第七序章2 天前
【C++STL】list的详细用法和底层实现
c语言·c++·自然语言处理·list
l1t2 天前
利用DeepSeek实现服务器客户端模式的DuckDB原型
服务器·c语言·数据库·人工智能·postgresql·协议·duckdb
太空的旅行者2 天前
告别双系统——WSL2+UBUNTU在WIN上畅游LINUX
linux·运维·ubuntu
l1t2 天前
利用美团龙猫用libxml2编写XML转CSV文件C程序
xml·c语言·libxml2·解析器
人工智能训练师2 天前
Ubuntu22.04如何安装新版本的Node.js和npm
linux·运维·前端·人工智能·ubuntu·npm·node.js