使用openssl,實現輸入和輸出都是字符串的類型,注意:輸入最好是16的倍數

頭文件crypto_util.h

#pragma once#include <string>namespace hsm{namespace mgmt{void get_md5_digest(const std::string &data,uint8_t result[16]);std::string aes_encrypt_to_string(const std::string &string,const std::string &password);std::string aes_decrypt_from_string(const std::string &string,const std::string &password);}//namespace mgmt
}//namespace hsm

代碼實現crypto_util.cpp

#include "../util/crypto_util.h"#include <cstring>
#include <memory>#include <openssl/aes.h>
#include <openssl/md5.h>namespace hsm{namespace mgmt{void get_md5_digest(const std::string &data,uint8_t result[16]){MD5_CTX md5_ctx{};MD5_Init(&md5_ctx);MD5_Update(&md5_ctx,data.c_str(),data.length());MD5_Final(result,&md5_ctx);}
/*** @brief generate a valid aes key from input password** @note AES only support keys with length 128/192/256bits* @note this implementation use md5 as a method to fix the password*/std::unique_ptr<AES_KEY> get_aes_key(const std::string &password,int flag){auto aes_key = std::make_unique<AES_KEY>();uint8_t data[16]{};get_md5_digest(password,data);if (flag == AES_ENCRYPT){AES_set_encrypt_key(data,sizeof(data)*8,aes_key.get());} else if (flag == AES_DECRYPT){AES_set_decrypt_key(data,sizeof(data)*8,aes_key.get());}return aes_key;}std::string aes_encrypt_to_string(const std::string &data,const std::string &password){auto aes_key = get_aes_key(password,AES_ENCRYPT);std::string result(data.length(),'0');auto input_offset = reinterpret_cast<const uint8_t *>(data.c_str());auto output_offset = reinterpret_cast<uint8_t *>(&result[0]);//encrypt blocksfor (size_t i = 0; i < data.length() / AES_BLOCK_SIZE ; ++i) {AES_encrypt(input_offset,output_offset,aes_key.get());input_offset += AES_BLOCK_SIZE;output_offset += AES_BLOCK_SIZE;}//write rest od data to fileauto rest_input_length = data.length() % AES_BLOCK_SIZE;if (rest_input_length > 0 ){std::memcpy(output_offset,input_offset,rest_input_length + 1);}return result;}std::string aes_decrypt_from_string(const std::string &enc_data,const std::string &password){auto aes_key = get_aes_key(password,AES_DECRYPT);std::string result(enc_data.length(),'0');auto input_offset = reinterpret_cast<const uint8_t *>(enc_data.c_str());auto output_offset = reinterpret_cast<uint8_t *>(&result[0]);//decrypt blocksfor (size_t i = 0;i < enc_data.length() / AES_BLOCK_SIZE;i++){AES_decrypt(input_offset,output_offset,aes_key.get());input_offset += AES_BLOCK_SIZE;output_offset += AES_BLOCK_SIZE;}//decrypt rest of dataauto rest_input_length = enc_data.length() % AES_BLOCK_SIZE;if (rest_input_length > 0 ){std::memcpy(output_offset,input_offset,rest_input_length + 1);}return result;}}//namespace mgmt
}//namespace hsm

?

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

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

相關文章

Android Studio 安裝ASM插件

目錄一、安裝步驟1、Android Studio -> Preferences...2、Plugins -> Browse repositories...3、搜索ASM -> 選中要安裝的插件 -> 右側點擊Install4、安裝完后點擊Restart Android Studio5、Android Studio重啟后右側會有個ASM圖標6、找一個類右鍵 -> Show Byte…

使用openssl完成aes-cbc模式的數據加解密,輸入和輸出都是字符串的形式

代碼 #include <cstring> #include <memory>#include <openssl/aes.h> #include <openssl/md5.h>namespace hsm{namespace mgmt{void get_md5_digest(const std::string &data,uint8_t result[16]){MD5_CTX md5_ctx{};MD5_Init(&md5_ctx);MD5…

Android 網絡異常

目錄前言一、UnknownHostException1、網絡斷開驗證2、DNS 服務器意外掛掉驗證3、DNS 服務器故障驗證4、所需診斷信息二、ConnectTimeoutException三、SocketTimeoutException1、子錯誤 - 讀超時2、子錯誤 - SSL 握手超時3、子錯誤 - 未知原因四、HttpHostConnectException1、服…

Android ViewRoot、DecorViewWindow淺析

目錄簡介目錄1、VeiwRoot1.1、簡介1.2、特別注意2、DecorView2.1、定義2.2、作用2.3、特別說明3、Window4、Activity5、之間關系5.1、總結5.2、之間的關系簡介 DecorView為整個Window界面的最頂層View。DecorView只有一個子元素為LinearLayout。代表整個Window界面&#xff0c;…

Java集合Stream類

Java集合Stream類 ----按條件對集合進行過濾filter public class Test {public static void main(String[] args) {List<String>allnew ArrayList<>();all.add("ghjt");all.add("ghjiiii");Stream<String>streamall.stream();List<S…

使用openssl完成aes-ecb模式的數據加解密,輸入和輸出都是字符串類型

代碼 #include <cstring> #include <memory>#include <openssl/aes.h> #include <openssl/md5.h>namespace hsm{namespace mgmt{void get_md5_digest(const std::string &data,uint8_t result[16]){MD5_CTX md5_ctx{};MD5_Init(&md5_ctx);MD5…

Java Stream MapReduce大數據開發模型

實現一個購買商品后,對數據進行處理統計的功能. 將購買的商品信息保存在Orders類中 public class Orders {private String name;private double price;private int amount;public Orders(String name, double price, int amount) {this.name name;this.price price;this.am…

隨機函數的生成

函數代碼 #include <string>bool GenerateRandom(std::string *str,unsigned int len) {srand(time(NULL));for(unsigned int i0;i<len;i){switch(rand()%3){case 1:(*str).push_back(Arand()%26);break;case 2:(*str).push_back(arand()%26);break;default:(*str).p…

Android 為控件設置陰影

在Android中設置一個陰影很簡單&#xff0c;只需要兩步&#xff1a; 設置eleavation值&#xff08;高度&#xff09;添加一個背景或者outline &#xff08;即陰影的形狀&#xff09; 說明&#xff1a; View的大小位置都是通過x&#xff0c;y確定的&#xff0c;而現在有了z軸的…

Android在代碼中設置drawableLeft(Right/Top/Bottom)

根據業務的需要&#xff0c;要在代碼中設置控件的drawableLeft&#xff0c;drawableRight&#xff0c;drawableTop&#xff0c;drawableBottom屬性。 我們知道在xml中設置的方法為&#xff1a; android:drawableLeft"drawable/xxxxx"但是在代碼中并沒有相關的setDr…

Java 冒泡排序

冒泡排序–時間復雜度n^2 對數組序列從前向后依次比較相鄰兩個元素的大小,若逆序則兩個元素交換位置如果一趟下來沒有發生交換,則說明序列有序,可以在序列中設置一個標志flag判斷元素是否發生交換,從而來減少不必要的比較(在寫完排序算法后再寫)小結:一共進行數組大小-1次的外…

使用openssl開源AES算法,實現aes、aes-cbc和aes-ecb對字符串的加解密

注意事項 對于用戶輸入的密碼進行了md5運算&#xff0c;從而保證數據格式的統一性 內部調用的隨機函數&#xff0c;參考我的其他博文 參考鏈接 頭文件crypto_util.h #pragma once#include <string>namespace hsm{namespace mgmt{void get_md5_digest(const std::strin…

Android學習指南

目錄核心分析內容1、學什么1.1、Android基礎 & 常用1.2、Android進階1.3、與時俱進、熱門技術1.4、編程語言&#xff1a;Java與Java虛擬機1.5、計算機基礎1.6、總結2、怎么學2.1、學習路徑&#xff1a;如何循序漸進、階段性的學習Android的理論知識&#xff1f;2.2、獲取途…

使用memcmp函數判斷兩個函數的前n位字節數是否相等

memcmp函數的介紹 頭文件&#xff1a;#include <string.h>定義函數&#xff1a;int memcmp (const void *s1, const void *s2, size_t n);函數說明&#xff1a;memcmp()用來比較s1 和s2 所指的內存區間前n 個字符。字符串大小的比較是以ASCII 碼表上的順序來決定&#x…

java 選擇排序

選擇排序–時間復雜度n^2 第一次從arr[0]–arr[n-1]中選出最小值,與arr[0]交換 第二次從arr[1]–arr[n-1]中選出最小值,與arr[1]交換… 最小數:假定當前這個數是最小數,然后和后面的每個數進行比較,當發現有更小的數時,重定最小數與最小數的下標 總結: 選擇排序一共有數組大…

Linux環境下實現unsigned char*向string的轉換

代碼 unsigned char input_data[input_data_length] {"This is my first encrypted plaintext hello world"}; openssl_enc_string hsm::mgmt::aes_ecb_encrypt_to_string(static_cast<string>((const char * )input_data),password);使用static_cast<st…

概率論 事件關系 古典概型與幾何概型

基本知識點 隨機試驗:1.不確定性2.可預知性3.可重復性基本事件:包含一個樣本點 必然事件:全集 不可能事件:空集 子集2^n-1-1(減去空集與真集) 事件間的關系 1.包含關系 2.和運算AUBAB,A與B至少有一個發生 3.積事件A∩BAB,AB同時發生 4.差事件A-BAB ?A-AB,A發生但B不發生 5.…

Android代碼命名規范

目錄目錄1、為什么 規范 Android 代碼命名&#xff1f;2、Android需要命名的代碼&#xff08;對象&#xff09;有哪些&#xff1f;3、具體命名規范3.1、包3.2、類3.3、變量3.4、方法3.5、參數名3.6、資源3.6.1、布局文件資源3.6.2、圖片資源3.6.3、參數值資源3.6.4、動畫資源3.…

安卓牛客專項練習2020.12.10

安卓Activity活動 Android的Activity活動中&#xff0c;我們一般可以歸結為四種狀態: 1.運行狀態Running 2.暫停狀態Paused 3.停止狀態Stopped 4.銷毀狀態Destroyed 動畫Animation 1.補間動畫TWeen Animation 其中包括: 平移Translate Animation 透明Alpha Animation 旋轉Ro…

將結構體數據存儲到一段字符串string中

核心思想 指定一段內存空間存儲結構體數據&#xff0c;然后使用一個指針強制類型轉化為結構體類型的&#xff0c;就可以使用這個指針操作結構體相關的成員函數和變量結構體內存只會為定義的變量進行分配內存空間&#xff0c;函數只進行調用&#xff0c;不占據空間 結構體 void…