• ubuntu环境下openssl库的简单使用


    安装

    sudo apt-get install libssl-dev
    
    • 1

    aes算法demo

    编译:gcc aes.c -lssl -lcrypto -o aes
    运行:./aes

    #include
    #include
    #include
    #include
     
    #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;
    }
    
    • 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
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    des算法demo

    编译:gcc des.c -lssl -lcrypto des
    运行:./des

    #include
    #include
    #include
    #include
    
    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;
    }
    
    • 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
    • 36
    • 37

    sm1算法demo

    
    
    • 1

    ssf33算法demo

    
    
    • 1

    sm4算法demo

    编译:gcc sm4.c -lssl -lcrypto -o sm4
    运行:./sm4

    #include 
    #include 
    #include 
    #include 
    #include 
    
    // 加密函数
    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;
    }
    
    
    • 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
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    sm6算法demo

    
    
    • 1
  • 相关阅读:
    【毕业设计】基于javaEE+SSM+MySql的BS架构微博系统设计与实现(毕业论文+程序源码)——BS架构微博系统
    面试官:你先回去等通知吧!这个 Java 岗位我还有机会吗?
    MogaFX—乐观的通胀报告意味着美元刚刚经历了15年来最糟糕的一天
    java毕业设计户籍管理系统(附源码、数据库)
    【架构】研发高可用架构和系统设计经验
    互联网营销的50条黄金法则
    Linux下安装mysql的几种方式
    Leetcode:349. 两个数组的交集【题解超详细】
    Wirshark学习笔记
    【前端设计模式】之访问者模式
  • 原文地址:https://blog.csdn.net/qq_38158479/article/details/136242792