• C++-openssl-aes-加密解密


    hmac   Hash-based Message Authentication Code
    MAC 定义: Message Authentication Code 一种确认完整性并进行认证的技术。


    1.openssl基本版 加密解密
     

    1. #include "openssl/rand.h"
    2. #include "openssl/md5.h"
    3. #include "openssl/hmac.h"
    4. #include "openssl/aes.h"
    5. //1.向量在运算过程中会被改变,为了之后可以正常解密,拷贝一份副本使用
    6. void main()
    7. {
    8. unsigned char key[16] = "123456789ABCDEF"; //1.key
    9. unsigned int n_keylength = strlen((char*)key); // strlen()长度15
    10. unsigned char iv[16] = "123456789ABCDEF"; //2.偏移量
    11. unsigned char iv_copy1[16] = "123456789ABCDEF"; //2.偏移量副本1
    12. unsigned char iv_copy2[16] = "123456789ABCDEF"; //2.偏移量副本2
    13. unsigned int n_ivlength = strlen((char*)iv); // strlen()长度15
    14. unsigned char buf_in[16] = "123456789ABCDEF"; //3.明文 in
    15. unsigned char buf_encrypt[16] = ""; //4.明文加密encrypt
    16. unsigned char buf_out[16] = ""; //5.解密后明文out
    17. AES_KEY aesKey;
    18. //加密
    19. //memcpy(iv_copy, iv, 16);
    20. AES_set_encrypt_key(key, 128, &aesKey);
    21. int m_bufinlen = sizeof(buf_in); // sizeof() 16
    22. AES_cbc_encrypt(buf_in, buf_encrypt, sizeof(buf_in), &aesKey, iv_copy1, 1);
    23. //解密
    24. //memcpy(iv_copy, iv, 16);
    25. AES_set_decrypt_key(key, 128, &aesKey);
    26. int m_bufencryptlen = sizeof(buf_encrypt); // sizeof() 16
    27. AES_cbc_encrypt(buf_encrypt, buf_out, sizeof(buf_encrypt), &aesKey, iv_copy2, 0);
    28. }

    2. 字符拼接


      
       2.1C中的字符拼接

    1. //1.snprintf 长度+1
    2. #include // C++的头文件不带.h
    3. #include // 兼容原C语言头文件
    4. using namespace std;
    5. //
    6. int main()
    7. {
    8. //1. snprintf 复制拼接 size值为待拷贝字符串长度+1,保证'\0'结尾符加入进来。
    9. char dest1[10] = "abc"; // 指针类型无法扩展长度,需标识为数组形式
    10. char *src1 = "def";
    11. snprintf(dest1+3, strlen(src1)+1, "%s", src1 ); // dest1+3 = dest1+3*sizeof(char)
    12. //strlen(src1)+1 因为最后一个\0
    13. // dest1: abcdef
    14. //2.把 src 所指向的字符串复制到 dest,最多复制 n 个字符
    15. // 当 src 的长度小于 n 时,dest 的剩余部分将用空字节填充
    16. char dest2[10] = "abcd";
    17. char *src2 = "efg";
    18. strncpy(dest2+3, src2, strlen(src2)); //dest2: abcefg
    19. //3.
    20. char dest3[10] = "abc";
    21. char *src3 = "def";
    22. strncat(dest3, src3 , strlen(src3 )+1); // dest3:abcdef
    23. return 0;
    24. }



    2.2 C++中的字符拼接

    1. #include // C++的头文件不带.h
    2. #include // 兼容原C语言头文件
    3. using namespace std;
    4. int main()
    5. {
    6. //char s1[] = {'a','b','c', '\0'}; // 定义1:不建议使用此定义方式,经常忘记加入'\0'
    7. //char s1[] = "abc"; // 定义2:使用C语言数组形式定义字符串
    8. string s1 = "abc"; // 定义3:使用string类定义
    9. string s2 = "def";
    10. string dest;
    11. dest = s1+s2; //1.直接+ dest 输出 abcdef
    12. printf("%s");
    13. return 0;
    14. }

    3 c/c++字符串格式化输出


     

    1. #include
    2. int main() {
    3. int num = 42;
    4. printf("The answer is %d\n", num);
    5. char dest[50];
    6. sprintf(dest, "The answer is %d", num);
    7. printf("%s\n", dest);
    8. return 0;
    9. }

    sprintf sprintf_s的区别

    4 双引号

    1. string str = "{\"OperatorID\":\"";
    2. str = str + s_config_operatorid_;
    3. str=str+ ",\"OperatorSecret\":\"";
    4. str = str + s_config_operatorsecret_;
    5. str = str + "\"}";
    6. char csrc[256]="";
    7. sprintf_s(csrc,256,"{\"OperatorID\":\" %s\"\",\"OperatorSecret:\"%s\"}",s_config_operatorid_.c_str(),s_config_operatorsecret_.c_str());

    5 time

    localtime_sWindows
    localtime_rlinux

    1. //WINDOWS
    2. void main()
    3. {
    4. string stime;
    5. struct tm tnow;
    6. time_t now = time(NULL);
    7. localtime_s(&tnow, &now);
    8. char buf_time[sizeof("yymmddhhmmss")] = { '\0' };
    9. sprintf(buf_time, "%02d%02d%02d%02d%02d%02d", tnow.tm_year+1900, tnow.tm_mon+1,
    10. tnow.tm_mday, tnow.tm_hour, tnow.tm_min, tnow.tm_sec);
    11. }

    linux
     

    1. #include
    2. #include
    3. int main()
    4. {
    5. time_t time_seconds = time(0);
    6. struct tm* now_time = localtime_r(&time_seconds);
    7. printf("%d-%d-%d %d:%d:%d\n", now_time->tm_year + 1900, now_time->tm_mon + 1,
    8. now_time->tm_mday, now_time->tm_hour, now_time->tm_min,
    9. now_time->tm_sec);
    10. }

    1. g++ -std=c++11 lolcatimetest.cpp -o localtimetest
    2. ./localtimetest

    6 char 转string

    1. // char 转换成string 3种
    2. void main()
    3. {
    4. unsigned char bufc1[5]="ABCD";
    5. string s1((char*)bufc1,5); //1.使用 string 的构造函数
    6. unsigned char bufc2[5]="1234";
    7. string s2; //2.声明string 后将char push_back
    8. for (int i = 0; i < 5; i++)
    9. {
    10. s2.push_back(bufc2[i]);
    11. }
    12. unsigned char bufc3[5]="9876";
    13. stringstream ss; //3.使用stringstream
    14. ss << bufc3;
    15. string str3 = ss.str();
    16. }






     

    7. 对比两个string


     

    1. void main()
    2. {
    3. string A("aBcdef");
    4. string B("AbcdEf");
    5. string C("123456");
    6. string D("123dfg");
    7. string E("aBcdef");
    8. int m = A.compare(E); //完整的A和E的比较 2个相同返回0
    9. int l = A.compare(B); //完整的A和B的比较 2个不同返回1
    10. int n = A.compare(1, 5, B, 0, 5); //"Bcdef"和"AbcdEf"比较 //不同返回1
    11. int p = A.compare(1, 5, B, 4, 2); //"Bcdef"和"Ef"比较 返回ff
    12. int q = C.compare(0, 3, D, 0, 3); //"123"和"123"比较 //相同返回0
    13. }

    8. 字符串中的字符无效




    1.如果已经分配过内存,可能是越界导致的指令错误。
    2.可能是字符数组成员的值超出了ASCII码表示范围,导致字符无效
       如:arr[[1]]=0x89;
    此时,在调试时,就会显示"字符串中的字符无效".

    格式化输出:可以打印

    1. void printf_buff(char* buff, int size)
    2. {
    3. int i = 0;
    4. for (i = 0; i < size; i++)
    5. {
    6. printf("%02X ", (unsigned char)buff[i]); //每个字符以16进制输出
    7. if ((i + 1) % 8 == 0) //每8个字节换行
    8. {
    9. printf("\n");
    10. }
    11. }
    12. printf("\n\n\n\n"); //空4行
    13. }


     

    9.cout和printf 区别

     

    coutprintf
    不需要格式
    cout<<是一个函数,cout<<后可以跟不同的类型
    需要指定格式
    #include #include
    在linux中,当遇到 \n 回车换行符时,进行IO操作输入输出

    1. void main()
    2. {
    3. int a=6;
    4. cout<
    5. printf("%d\n",a); //需要告诉他格式
    6. }
    7. int main()
    8. {
    9. cout << "Hello world!" << endl;//C++
    10. cout.operator<<("Hello,World!");
    11. cout.operator<<(endl);
    12. printf("Hello world!"); //C
    13. return 0;
    14. }

    1.printf是函数。cout是ostream对象,和<<配合使用。
    2.printf是变参函数,没有类型检查,不安全。cout是通过运算符重载实现的,安全。
    3.如果printf碰到不认识的类型就没办法了,而cout可以自己重载进行扩展。
    4.有时候printf比cout灵活。
    c++中也能使用printf,但是c中不能使用cout

    10. new malloc区别

     

    newmalloc
    new是关键字,需要编译器支持malloc是库函数,需要头文件支持
    从自由存储区(free store)上为对象动态分配内存空间从堆上动态分配内存

    1. void main()
    2. {
    3. int* p=new int; //分配大小为sizeof(int)的空间 4字节
    4. int* p=new int6); //分配大小为sizeof(int)的空间,并且初始化为6
    5. int* p=(int)malloc(sizeof(int)100);//分配可以放下100个int的内存空间
    6. int* ptr=new int[100];//分配100个int的内存空间
    7. int* p=(int)malloc(sizeof(int)100);//分配可以放下100个int的内存空间
    8. }

    11.printf打印格式
     

    1. // %x 以16进制打印一个整数(4字节)
    2. // hh: char 1个字节
    3. // h: short 2个字节
    4. char c[] = "12345678";
    5. int i= 12345678;
    6. printf("char-----%02hhx,%02hx,%02x\n", c, c, c); // e4,fce4,12ffce4
    7. // 从后往前
    8. printf("int -----%02hhx,%02hx,%02x\n", i, i, i); // 4e,614e,bc614e
    9. printf("-----%s\n", c); //12345678
    10. printf("-----%c\n", c); //@




    12.字符串转16进制输出
     

    1. char* str2hex(char* strin)
    2. {
    3. char* ret = NULL; //0.返回字符串
    4. int str_len = strlen(strin); //1.输入字符串strin
    5. assert((str_len % 2) == 0); //2.判断输入长度是否为2的倍数 如果不是返回
    6. ret = (char*)malloc(str_len / 2); //3.分配一个空间
    7. for (int i = 0; i < str_len; i = i + 2) //4.
    8. {
    9. sscanf(strin + i, "%2hhx", &ret[i / 2]); //5. 格式化放入 ret中
    10. }
    11. // printf("%s\n", ret);
    12. return ret;
    13. }
    14. void printf_buff(char* buff, int size)
    15. {
    16. int i = 0;
    17. for (i = 0; i < size; i++)
    18. {
    19. // printf("i=%d %02X ,", i,(unsigned char)buff[i]); //每个字符以16进制输出
    20. printf("%02X ", (unsigned char)buff[i]);
    21. if ((i + 1) % 8 == 0) //每8个字节换行
    22. {
    23. printf("\n");
    24. }
    25. }
    26. printf("\n\n\n\n"); //空4行
    27. }
    28. void main()
    29. {
    30. char in1[] = "0123456789ABCDEF0123456789\0";
    31. char* out1 = str2hex((char*)in1);
    32. int m_outlen1 = strlen(out1);
    33. printf("---in1\n");
    34. printf_buff(out1, m_outlen1);
    35. char in2[] = "0123456789ABCDEF\0";
    36. char *out2 = str2hex((char*)in2);
    37. int m_outlen2 = strlen(out2);
    38. printf("---in2\n");
    39. printf_buff(out2, m_outlen2);
    40. }

    “%02x” 以0补齐2位数,如果超过2位就显示实际的数;
    "%hhx" 是只输出2位数,即便超了,也只显示低2位。

    “%02hhx”
    只能对字符串为16进制的数字做正确转换,字符串个数还不对。

    13.byte和char 区别



      头文件 定义了 byte

    bytechar
    有符号
    -128-127
    无符号
    0-65535
    不可以可以表中文
    输出结果都会转化成数字char 输出结果都会转化为字符



     

     

  • 相关阅读:
    【SQL】索引的创建与设计原则
    智慧水务三维可视化管理系统
    大学生阅读小说网页设计模板代码 小说书籍网页作业成品 学校书籍网页制作模板 学生简单书籍阅读网站设计成品
    大数据ClickHouse进阶(十):ClickHouse的Array Join子句
    Altium Designer20 PCB规则设置
    转录组学习第三弹-下载SRR数据并转成fastq
    FlinkSQL系列01-编程入门
    消息队列 RabbitMQ入门:Linux(Docker)中安装和卸载RabbitMQ服务
    【面试宝典】MyBatis系列面试真题
    Opencv形态学——腐蚀、膨胀、开运算与闭运算、梯度运算、礼帽、黑帽
  • 原文地址:https://blog.csdn.net/aggie4628/article/details/133376767