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 复制代码
相关推荐
Navigator_Z9 小时前
LeetCode //C - 1252. Cells with Odd Values in a Matrix
c语言·算法·leetcode
是隼人11 小时前
buuctf-pwn [NewStarCTF 2023 公开赛道]stack migration(64位栈迁移)题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
Logic10114 小时前
C语言/数据结构贪心算法题解:买卖股票的最佳时机——一次交易最大利润(O(n)时间O(1)空间)
c语言·数据结构·贪心算法·数组·时间复杂度·算法题·股票交易
Logic10114 小时前
C语言/数据结构位运算题解:异或XOR找出货币交易中的“独特面值“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
aaaameliaaa16 小时前
结构体 结构体
c语言·笔记·算法
是隼人17 小时前
buuctf-pwn PWN8题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
GG-_-Bond17 小时前
9.1kv存储持久化模拟面试
linux·c语言·数据结构·c++
无敌贵点大王18 小时前
RTThread学习记录10——C语言实现面向对象的编程
c语言·stm32·学习·链表
en.en..19 小时前
TCP与UDP对比
c语言
指尖的爷20 小时前
【地狱级难度】ubuntu系统GOCV编译安装
linux·运维·ubuntu