ubuntu環境下openssl庫的簡單使用

安裝

sudo apt-get install libssl-dev

aes算法demo

編譯:gcc aes.c -lssl -lcrypto -o aes
運行:./aes

#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

#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;//轉換成scheduleDES_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


ssf33算法demo


sm4算法demo

編譯:gcc sm4.c -lssl -lcrypto -o sm4
運行:./sm4

#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


本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/697445.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/697445.shtml
英文地址,請注明出處:http://en.pswp.cn/news/697445.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

UNI-APP_app跳轉企業微信客服對話

uniapp打包app&#xff0c;app里點擊客服&#xff0c;跳轉企業微信客服對話。為什么是企業微信&#xff1f;因為只有微信小程序才可以通過 button 的 open-type‘share’ 打開微信客服對話框&#xff08;微信客服要在公眾號平臺配置&#xff09; 1、appId獲取 &#xff08;1&a…

OJAC近嶼智能張立賽博士揭秘GPT Store:技術創新、商業模式與未來趨勢

> - [Look&#xff01;&#x1f440;我們的大模型商業化落地產品](https://www.airecruitas.com/aigc) >- &#x1f4d6;更多AI資訊請&#x1f449;&#x1f3fe;[關注](https://mp.weixin.qq.com/s/85qwuIydaaydMQz2g0rgMA) >- [Free三天集訓營助教在線為您火熱答疑…

C#_各式各樣的參數(引用參數、輸出參數、數組參數、具名參數、可選參數)

引用參數 值參數和引用參數的區別在于傳參時是否會創建參數副本&#xff1a;值參數不會創建副本&#xff0c;而引用參數會創建副本。 換言之&#xff0c;值類型參數的參數與實體之間無直接關聯&#xff0c;修改參數不會對實體產生影響&#xff1b;引用類型參數的參數與實體可視…

6.微格式

微格式 經典真題 知道什么是微格式嗎&#xff1f;談談理解。在前端構建中應該考慮微格式嗎&#xff1f; 微格式介紹 所謂微格式&#xff0c;是建立在已有的、被廣泛采用的標準基礎之上的一組簡單的、開放的數據格式。 具體表現是把語義嵌入到 HTML 中&#xff0c;以便有助…

通過SSH 可以訪問Ubuntu Desktop嗎?

你可以在 Ubuntu Desktop 上開啟 SSH 服務&#xff0c;以便其他機器可以通過 SSH 連接到你的服務器。以下是在 Ubuntu Desktop 上開啟 SSH 服務的步驟&#xff1a; 打開終端 (Terminal) 應用程序。 輸入以下命令安裝 OpenSSH 服務器&#xff1a; sudo apt-get update sudo ap…

多任務爬蟲(多線程和多進程)

在一臺計算機中&#xff0c;我們可以同時打開多個軟件&#xff0c;例如同時瀏覽網頁、聽音樂、打字等&#xff0c;這是再正常不過的事情。但仔細想想&#xff0c;為什么計算機可以同時運行這么多軟件呢? 這就涉及計算機中的兩個名詞&#xff1a;多進程和多線程。 同樣&#xf…

通信入門系列——鎖相環、平方環、Costas環

微信公眾號上線&#xff0c;搜索公眾號小灰灰的FPGA,關注可獲取相關源碼&#xff0c;定期更新有關FPGA的項目以及開源項目源碼&#xff0c;包括但不限于各類檢測芯片驅動、低速接口驅動、高速接口驅動、數據信號處理、圖像處理以及AXI總線等 本節目錄 一、鎖相環 1、壓控振蕩…

重磅!MongoDB推出Atlas Stream Processing公共預覽版

日前&#xff0c;MongoDB宣布推出Atlas Stream Processing公共預覽版。 在Atlas平臺上有興趣嘗試這項功能的開發者都享有完全的訪問權限&#xff0c;可前往“閱讀原文”鏈接點擊了解更多詳細信息或立即開始使用。 開發者喜歡文檔型數據庫的靈活性、易用性以及Query API查詢方…

使用k-近鄰算法改進約會網站的配對效果(kNN)

目錄 谷歌筆記本&#xff08;可選&#xff09; 準備數據&#xff1a;從文本文件中解析數據 編寫算法&#xff1a;編寫kNN算法 分析數據&#xff1a;使用Matplotlib創建散點圖 準備數據&#xff1a;歸一化數值 測試算法&#xff1a;作為完整程序驗證分類器 使用算法&…

js過濾取出對象中改變的屬性和值

朋友公司的面試題 &#xff0c;取出對象中被改變的屬性和值 const obj1 { a: 1, b: 2, c: 4 }; const obj2 { a: 1, b: 2, c: 5 }; 方法1 function testFun(obj1, obj2) {const diff {};const keys1 Object.keys(obj1);const keys2 Object.keys(obj2);const allKyes keys…

【深度學習】Gemini 1.0 Pro 如何讓chatGPT扮演stable diffusion的提示詞工程師

google也出了一個chatGPT&#xff0c;免費申請使用&#xff1a; https://aistudio.google.com/app/prompts/new_chat https://github.com/google/generative-ai-docs/blob/main/site/en/tutorials/rest_quickstart.ipynb 模型信息&#xff1a; $ curl https://generativelan…

SpringCloud(14)之SpringCloud Consul

我們知道 Eureka 2.X 遇到困難停止開發了&#xff0c;所以我們需要尋找其他的替代技術替代Eureka&#xff0c;這一小 節我們就講解一個新的組件Consul。 一、Consul介紹 Consul 是 HashiCorp 公司推出的開源工具&#xff0c;用于實現分布式系統的服務發現與配置。與其它分布式…

kali xrdp

Kali Linux 使用遠程桌面連接——xrdp&xfce_kali xfce桌面-CSDN博客 Ubuntu/Debian/Kali xrdp遠程桌面黑屏/空屏/無畫面解決辦法 - 知乎 (zhihu.com) sudo apt-get install xrdp -y sudo apt-get install xfce4 -ysudo systemctl enable xrdp --now systemctl status xrd…

中級.NET開發工程師面試經歷

文章目錄 前言面試題目&#xff08;只記錄了還記得的部分&#xff09;一.簡單說下.NETCORE的生命周期&#xff1f;二.C#如何保證在并發情況下接口不會被重復觸發&#xff1f;三.引用類型和值類型有什么區別&#xff1f;四.那怎樣能讓引用類型和值類型一樣&#xff0c;在賦值的時…

【Latex】TeXstudio編譯器選項修改

1、動機 編譯國科大博士畢業答辯論文latex時報錯 Package ctable Error: You must load ctable after tikz. 2、方法 經過搜索發現是因為這是中文模板&#xff0c;編譯的選項不對&#xff0c;需要從 PDFLaTeX 調整到 XeLaTeX。于是操作如下 1&#xff09;點擊選項 2&#xf…

linux 文件目錄操作命令【重點】

目錄 ls cd cat more tail【工作中使用多】 mkdir rmdir rm ls 作用: 顯示指定目錄下的內容 語法: ls [-al] [dir] 說明: -a 顯示所有文件及目錄 (. 開頭的隱藏文件也會列出) -l 除文件名稱外&#xff0c;同時將文件型態(d表示目錄&#xff0c;-表示文件)、權限…

SpringMVC POST請求傳參 屬性名字母大寫注入失敗解決方案

問題描述&#xff1a; 我現在有一個接口通過一個實體(RequestBody)去接收一系列的參數&#xff0c;前端傳參為一個JSON字符串&#xff0c;但是當我的屬性名以大寫字母開頭(有的中間還有下劃線)&#xff0c;或者第二個字母是大寫字母的時候&#xff0c;我發現后端接收不到參數值…

Flask——基于python完整實現客戶端和服務器后端流式請求及響應

文章目錄 本地客戶端Flask服務器后端客戶端/服務器端流式接收[打字機]效果 看了很多相關博客&#xff0c;但是都沒有本地客戶端和服務器后端的完整代碼示例&#xff0c;有的也只說了如何流式獲取后端結果&#xff0c;基本沒有講兩端如何同時實現流式輸入輸出&#xff0c;特此整…

C++字符串類

C中有兩種主要的字符串類&#xff1a;std::string 和 std::wstring。 std::string std::string 是 C 標準庫中用于處理 ASCII 字符串的類。它提供了豐富的方法來操作字符串&#xff0c;包括插入、刪除、查找子串、比較等功能。使用 std::string 需要包含頭文件 <string>…

8.CSS層疊繼承規則總結

CSS 層疊繼承規則總結 經典真題 請簡述一下 CSS 中的層疊規則 CSS 中的層疊繼承規則 在前面《CSS屬性的計算過程》中&#xff0c;我們介紹了每一個元素都有都有所有的屬性&#xff0c;每一個屬性都會通過一系列的計算過程得到最終的值。 這里來回顧一下計算過程&#xff0…