hmac? ?Hash-based Message Authentication Code
MAC 定義: Message Authentication Code 一種確認(rèn)完整性并進(jìn)行認(rèn)證的技術(shù)。
1.openssl基本版 加密解密 ?
#include "openssl/rand.h"
#include "openssl/md5.h"
#include "openssl/hmac.h"
#include "openssl/aes.h"
//1.向量在運(yùn)算過程中會(huì)被改變,為了之后可以正常解密,拷貝一份副本使用
void main()
{
unsigned char key[16] = "123456789ABCDEF"; //1.key
unsigned int n_keylength = strlen((char*)key); // strlen()長度15
unsigned char iv[16] = "123456789ABCDEF"; //2.偏移量
unsigned char iv_copy1[16] = "123456789ABCDEF"; //2.偏移量副本1
unsigned char iv_copy2[16] = "123456789ABCDEF"; //2.偏移量副本2
unsigned int n_ivlength = strlen((char*)iv); // strlen()長度15
unsigned char buf_in[16] = "123456789ABCDEF"; //3.明文 in
unsigned char buf_encrypt[16] = ""; //4.明文加密encrypt
unsigned char buf_out[16] = ""; //5.解密后明文out
AES_KEY aesKey;
//加密
//memcpy(iv_copy, iv, 16);
AES_set_encrypt_key(key, 128, &aesKey);
int m_bufinlen = sizeof(buf_in); // sizeof() 16
AES_cbc_encrypt(buf_in, buf_encrypt, sizeof(buf_in), &aesKey, iv_copy1, 1);
//解密
//memcpy(iv_copy, iv, 16);
AES_set_decrypt_key(key, 128, &aesKey);
int m_bufencryptlen = sizeof(buf_encrypt); // sizeof() 16
AES_cbc_encrypt(buf_encrypt, buf_out, sizeof(buf_encrypt), &aesKey, iv_copy2, 0);
}
2.?字符拼接
??
? ?2.1C中的字符拼接
//1.snprintf 長度+1
#include <iostream> // C++的頭文件不帶.h
#include <string.h> // 兼容原C語言頭文件
using namespace std;
//
int main()
{
//1. snprintf 復(fù)制拼接 size值為待拷貝字符串長度+1,保證'\0'結(jié)尾符加入進(jìn)來。
char dest1[10] = "abc"; // 指針類型無法擴(kuò)展長度,需標(biāo)識(shí)為數(shù)組形式
char *src1 = "def";
snprintf(dest1+3, strlen(src1)+1, "%s", src1 ); // dest1+3 = dest1+3*sizeof(char)
//strlen(src1)+1 因?yàn)樽詈笠粋€(gè)\0
// dest1: abcdef
//2.把 src 所指向的字符串復(fù)制到 dest,最多復(fù)制 n 個(gè)字符
// 當(dāng) src 的長度小于 n 時(shí),dest 的剩余部分將用空字節(jié)填充
char dest2[10] = "abcd";
char *src2 = "efg";
strncpy(dest2+3, src2, strlen(src2)); //dest2: abcefg
//3.
char dest3[10] = "abc";
char *src3 = "def";
strncat(dest3, src3 , strlen(src3 )+1); // dest3:abcdef
return 0;
}
2.2 C++中的字符拼接
#include <iostream> // C++的頭文件不帶.h
#include <string.h> // 兼容原C語言頭文件
using namespace std;
int main()
{
//char s1[] = {'a','b','c', '\0'}; // 定義1:不建議使用此定義方式,經(jīng)常忘記加入'\0'
//char s1[] = "abc"; // 定義2:使用C語言數(shù)組形式定義字符串
string s1 = "abc"; // 定義3:使用string類定義
string s2 = "def";
string dest;
dest = s1+s2; //1.直接+ dest 輸出 abcdef
printf("%s");
return 0;
}
3 c/c++字符串格式化輸出
?
#include <stdio.h>
int main() {
int num = 42;
printf("The answer is %d\n", num);
char dest[50];
sprintf(dest, "The answer is %d", num);
printf("%s\n", dest);
return 0;
}
sprintf sprintf_s的區(qū)別
4 雙引號(hào)
string str = "{\"OperatorID\":\"";
str = str + s_config_operatorid_;
str=str+ ",\"OperatorSecret\":\"";
str = str + s_config_operatorsecret_;
str = str + "\"}";
char csrc[256]="";
sprintf_s(csrc,256,"{\"OperatorID\":\" %s\"\",\"OperatorSecret:\"%s\"}",s_config_operatorid_.c_str(),s_config_operatorsecret_.c_str());
5 time
localtime_s | Windows |
localtime_r | linux |
//WINDOWS
void main()
{
string stime;
struct tm tnow;
time_t now = time(NULL);
localtime_s(&tnow, &now);
char buf_time[sizeof("yymmddhhmmss")] = { '\0' };
sprintf(buf_time, "%02d%02d%02d%02d%02d%02d", tnow.tm_year+1900, tnow.tm_mon+1,
tnow.tm_mday, tnow.tm_hour, tnow.tm_min, tnow.tm_sec);
}
linux
?
#include <stdio.h>
#include <time.h>
int main()
{
time_t time_seconds = time(0);
struct tm* now_time = localtime_r(&time_seconds);
printf("%d-%d-%d %d:%d:%d\n", now_time->tm_year + 1900, now_time->tm_mon + 1,
now_time->tm_mday, now_time->tm_hour, now_time->tm_min,
now_time->tm_sec);
}
g++ -std=c++11 lolcatimetest.cpp -o localtimetest
./localtimetest
6 char 轉(zhuǎn)string
// char 轉(zhuǎn)換成string 3種
void main()
{
unsigned char bufc1[5]="ABCD";
string s1((char*)bufc1,5); //1.使用 string 的構(gòu)造函數(shù)
unsigned char bufc2[5]="1234";
string s2; //2.聲明string 后將char push_back
for (int i = 0; i < 5; i++)
{
s2.push_back(bufc2[i]);
}
unsigned char bufc3[5]="9876";
stringstream ss; //3.使用stringstream
ss << bufc3;
string str3 = ss.str();
}
?
7. 對(duì)比兩個(gè)string
?
void main()
{
string A("aBcdef");
string B("AbcdEf");
string C("123456");
string D("123dfg");
string E("aBcdef");
int m = A.compare(E); //完整的A和E的比較 2個(gè)相同返回0
int l = A.compare(B); //完整的A和B的比較 2個(gè)不同返回1
int n = A.compare(1, 5, B, 0, 5); //"Bcdef"和"AbcdEf"比較 //不同返回1
int p = A.compare(1, 5, B, 4, 2); //"Bcdef"和"Ef"比較 返回ff
int q = C.compare(0, 3, D, 0, 3); //"123"和"123"比較 //相同返回0
}
8.?字符串中的字符無效
1.如果已經(jīng)分配過內(nèi)存,可能是越界導(dǎo)致的指令錯(cuò)誤。
2.可能是字符數(shù)組成員的值超出了ASCII碼表示范圍,導(dǎo)致字符無效
? ?如:arr[[1]]=0x89;
此時(shí),在調(diào)試時(shí),就會(huì)顯示"字符串中的字符無效".
格式化輸出:可以打印
void printf_buff(char* buff, int size)
{
int i = 0;
for (i = 0; i < size; i++)
{
printf("%02X ", (unsigned char)buff[i]); //每個(gè)字符以16進(jìn)制輸出
if ((i + 1) % 8 == 0) //每8個(gè)字節(jié)換行
{
printf("\n");
}
}
printf("\n\n\n\n"); //空4行
}
?
9.cout和printf 區(qū)別 ?
cout | printf | |
不需要格式 cout<<是一個(gè)函數(shù),cout<<后可以跟不同的類型 |
需要指定格式 | |
#include <iostream> | #include <stdio.h> | |
在linux中,當(dāng)遇到 \n 回車換行符時(shí),進(jìn)行IO操作輸入輸出 |
void main()
{
int a=6;
cout<<a<<endl;
printf("%d\n",a); //需要告訴他格式
}
int main()
{
cout << "Hello world!" << endl;//C++
cout.operator<<("Hello,World!");
cout.operator<<(endl);
printf("Hello world!"); //C
return 0;
}
1.printf是函數(shù)。cout是ostream對(duì)象,和<<配合使用。
2.printf是變參函數(shù),沒有類型檢查,不安全。cout是通過運(yùn)算符重載實(shí)現(xiàn)的,安全。
3.如果printf碰到不認(rèn)識(shí)的類型就沒辦法了,而cout可以自己重載進(jìn)行擴(kuò)展。
4.有時(shí)候printf比cout靈活。
c++中也能使用printf,但是c中不能使用cout
10. new malloc區(qū)別 ?
new | malloc | |
new是關(guān)鍵字,需要編譯器支持 | malloc是庫函數(shù),需要頭文件支持 | |
從自由存儲(chǔ)區(qū)(free store)上為對(duì)象動(dòng)態(tài)分配內(nèi)存空間 | 從堆上動(dòng)態(tài)分配內(nèi)存 | |
void main()
{
int* p=new int; //分配大小為sizeof(int)的空間 4字節(jié)
int* p=new int(6); //分配大小為sizeof(int)的空間,并且初始化為6
int* p=(int)malloc(sizeof(int)100);//分配可以放下100個(gè)int的內(nèi)存空間
int* ptr=new int[100];//分配100個(gè)int的內(nèi)存空間
int* p=(int)malloc(sizeof(int)100);//分配可以放下100個(gè)int的內(nèi)存空間
}
11.printf打印格式 ?
// %x 以16進(jìn)制打印一個(gè)整數(shù)(4字節(jié))
// hh: char 1個(gè)字節(jié)
// h: short 2個(gè)字節(jié)
char c[] = "12345678";
int i= 12345678;
printf("char-----%02hhx,%02hx,%02x\n", c, c, c); // e4,fce4,12ffce4
// 從后往前
printf("int -----%02hhx,%02hx,%02x\n", i, i, i); // 4e,614e,bc614e
printf("-----%s\n", c); //12345678
printf("-----%c\n", c); //@
12.字符串轉(zhuǎn)16進(jìn)制輸出 ?
char* str2hex(char* strin)
{
char* ret = NULL; //0.返回字符串
int str_len = strlen(strin); //1.輸入字符串strin
assert((str_len % 2) == 0); //2.判斷輸入長度是否為2的倍數(shù) 如果不是返回
ret = (char*)malloc(str_len / 2); //3.分配一個(gè)空間
for (int i = 0; i < str_len; i = i + 2) //4.
{
sscanf(strin + i, "%2hhx", &ret[i / 2]); //5. 格式化放入 ret中
}
// printf("%s\n", ret);
return ret;
}
void printf_buff(char* buff, int size)
{
int i = 0;
for (i = 0; i < size; i++)
{
// printf("i=%d %02X ,", i,(unsigned char)buff[i]); //每個(gè)字符以16進(jìn)制輸出
printf("%02X ", (unsigned char)buff[i]);
if ((i + 1) % 8 == 0) //每8個(gè)字節(jié)換行
{
printf("\n");
}
}
printf("\n\n\n\n"); //空4行
}
void main()
{
char in1[] = "0123456789ABCDEF0123456789\0";
char* out1 = str2hex((char*)in1);
int m_outlen1 = strlen(out1);
printf("---in1\n");
printf_buff(out1, m_outlen1);
char in2[] = "0123456789ABCDEF\0";
char *out2 = str2hex((char*)in2);
int m_outlen2 = strlen(out2);
printf("---in2\n");
printf_buff(out2, m_outlen2);
}
“%02x” 以0補(bǔ)齊2位數(shù),如果超過2位就顯示實(shí)際的數(shù);
"%hhx" 是只輸出2位數(shù),即便超了,也只顯示低2位。
“%02hhx”
只能對(duì)字符串為16進(jìn)制的數(shù)字做正確轉(zhuǎn)換,字符串個(gè)數(shù)還不對(duì)。
13.byte和char 區(qū)別
? 頭文件<windows> 定義了 byte文章來源:http://www.zghlxwxcb.cn/news/detail-767339.html
byte | char | |
有符號(hào) -128-127 |
無符號(hào) 0-65535 |
|
不可以 | 可以表中文 | |
輸出結(jié)果都會(huì)轉(zhuǎn)化成數(shù)字 | char 輸出結(jié)果都會(huì)轉(zhuǎn)化為字符 | |
?
?文章來源地址http://www.zghlxwxcb.cn/news/detail-767339.html
到了這里,關(guān)于C++-openssl-aes-加密解密的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!