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 复制代码
相关推荐
凯子坚持 c7 分钟前
C语言复习概要(三)
c语言·开发语言
无限大.19 分钟前
c语言200例 067
java·c语言·开发语言
无限大.22 分钟前
c语言实例
c语言·数据结构·算法
Death20025 分钟前
Qt 中的 QListWidget、QTreeWidget 和 QTableWidget:简化的数据展示控件
c语言·开发语言·c++·qt·c#
Death2001 小时前
Qt 3D、QtQuick、QtQuick 3D 和 QML 的关系
c语言·c++·qt·3d·c#
洛临_1 小时前
【C语言】基础篇
c语言·算法
QMCY_jason2 小时前
Ubuntu 安装RUST
linux·ubuntu·rust
慕雪华年2 小时前
【WSL】wsl中ubuntu无法通过useradd添加用户
linux·ubuntu·elasticsearch
苦逼IT运维2 小时前
YUM 源与 APT 源的详解及使用指南
linux·运维·ubuntu·centos·devops
第六五2 小时前
ubuntu命令行连接wifi
服务器·ubuntu