zoukankan      html  css  js  c++  java
  • 2021.5.6 课堂测试

    目录

    BASE64

    SM3

    BASE64

    一、使用工具(如bc,计算机器等)把20181234转化为16进制,提交转化过程和结果截图

    1.计算器

    2.bc

     

    二、使用工具(如echo -e, ultraedit等)把上面转化的结果写入二进制文件“20181234.dat”中,并用工具`od -tx1 20181234.dat`,提交命令运行

    三、使用OpenSSL的base64命令对"20181234.dat"进行编码解码,提交过程截图

    四、使用OpenSSL编程对"20181234.dat"进行编码解码,提交代码和运行结果截图

    #include <stdio.h>
    #include <string.h>
    #include <openssl/evp.h>
    #include <openssl/x509.h>
    
    //Base64编码
    void tEVP_Encode()
    {
        EVP_ENCODE_CTX *ctx;
            ctx = EVP_ENCODE_CTX_new();                //EVP编码结构体
        unsigned char in[1024];            //输入数据缓冲区
        int inl;                        //输入数据长度
        char out[2048]={0};                //输出数据缓冲区
        int outl;                        //输出数据长度
        FILE *infp;                        //输入文件句柄
        FILE *outfp;                    //输出文件句柄
    
        infp = fopen("20181234.dat","rb");//打开待编码的文件
        if(infp == NULL)
        {
            printf("Open File "20181234.dat"  for Read Err.
    ");
            return;
        }
        
        outfp = fopen("20181234.txt","w");//打开编码后保存的文件
        if(outfp == NULL)
        {
            printf("Open File "20181234.txt" For Write Err.
    ");
            return;
        }
        EVP_EncodeInit(ctx);//Base64编码初始化
        printf("文件"20181234.dat" Base64编码后为:
    ");
        //循环读取原文,并调用EVP_EncodeUpdate计算Base64编码
        while(1)
        {
            inl = fread(in,1,1024,infp);
            if(inl <= 0)
                break;
            EVP_EncodeUpdate(ctx,out,&outl,in,inl);//编码
            fwrite(out,1,outl,outfp);//输出编码结果到文件
            printf("%s",out);
        } 
        EVP_EncodeFinal(ctx,out,&outl);//完成编码,输出最后的数据。
        fwrite(out,1,outl,outfp);
        printf("%s",out);
        fclose(infp);
        fclose(outfp);    
        printf("对文件"20181234.dat" Base64编码完成,保存到"20181234.txt"文件.
    
    
    ");
    }
    
    //Base64解码
    void tEVP_Decode()
    {
        EVP_ENCODE_CTX *ctx;
            ctx = EVP_ENCODE_CTX_new();            //EVP编码结构体
        char in[1024];                    //输入数据缓冲区
        int inl;                        //输入数据长度
        unsigned char out[1024];        //输出数据缓冲区
        int outl;                        //输出数据长度
        FILE *infp;                        //输入文件句柄
        FILE *outfp;                    //输出文件句柄
        
        infp = fopen("20181234.txt","r");//打开待解码的文件
        if(infp == NULL)
        {
            printf("Open File "20181234.txt"  for Read Err.
    ");
            return;
        }
        outfp = fopen("20181234-1.dat","wb");//打开解码后保存的文件
        if(outfp == NULL)
        {
            printf("Open File "20181234-1.txt" For Write Err.
    ");
            return;
        }
        EVP_DecodeInit(ctx);//Base64解码初始化
        printf("开始对文件"20181234.txt" Base64解码...
    
    ");
        //循环读取原文,并调用EVP_DecodeUpdate进行Base64解码
        while(1)
        {
            inl = fread(in,1,1024,infp);
            if(inl <= 0)
                break;
            EVP_DecodeUpdate(ctx,out,&outl,in,inl);//Base64解码
            fwrite(out,1,outl,outfp);//输出到文件
        } 
        EVP_DecodeFinal(ctx,out,&outl);//完成解码,输出最后的数据。
        fwrite(out,1,outl,outfp);
        fclose(infp);
        fclose(outfp);    
        printf("对文件"20181234.txt" Base64解码完成,保存为"20181234-1.dat"
    
    
    ");
        
    }
     
    int main()
    {
     
        tEVP_Encode();
        tEVP_Decode();
        
        return 0;
    }

    # 命令行

    echo -e -n "133F0F2" > 20181234.dat
    cat 20181234.dat
    
    echo "obase=16;20181234" > bc
    echo "obase=16;20181234" | bc
    
    openssl base64 -in 20181234.dat
    openssl base64 -in 20181234.dat -out 20181234bak
    cat 20181234bak
    openssl base64 -d -in 20181234bak 
    openssl base64 -d -in 20181234bak | od -tx1
    
    gcc -o base64 testbase64.c -I /root/zyxopenssl/include -L /root/zyxopenssl/lib -ldl -lpthread -lcrypto
    ./base64

    SM3

    一、使用OpenSSL的命令计算你的8位学号的摘要值(SM3),提交截图

    二、使用OpenSSL编程对计算"你的8位学号"SM3摘要值,提交代码和运行结果截图

    三、使用OpenSSL编程对计算内容为"所有同学的8位学号"的文件的SM3摘要值,提交代码和运行结果截图(选做(10’))

    将所有同学的学号放入sno.txt中

    #include <stdio.h>
    #include <string.h>
    #include <openssl/evp.h>
    void tDigest()
    {
            unsigned char sm3_value[EVP_MAX_MD_SIZE]; 
            int sm3_len, i;
            EVP_MD_CTX *sm3ctx; 
            sm3ctx = EVP_MD_CTX_new();
            char msg1[10000] = ""; 
            FILE *file;
            char line[32];
            char *ret;
            file = fopen("sno.txt", "r");
            if (!file) {
                    printf("文件打开失败!
    ");
                    return 1;
            }
            while (ret = fgets(line, sizeof(line), file)) {
                    strcat(msg1,line);
            }
    printf(
    "sno.txt中的内容为 %s ",msg1); fclose(file); EVP_MD_CTX_init(sm3ctx); EVP_DigestInit_ex(sm3ctx, EVP_sm3(), NULL); EVP_DigestUpdate(sm3ctx, msg1, strlen(msg1)); EVP_DigestFinal_ex(sm3ctx, sm3_value, &sm3_len); EVP_MD_CTX_reset(sm3ctx); printf("sno.txt的摘要值为: "); for(i = 0; i < sm3_len; i++) { printf("0x%02x ", sm3_value[i]); } printf(" "); } int main() { OpenSSL_add_all_algorithms(); tDigest(); return 0; }

    编译指令:gcc sno_sm3.c -o sno_sm3 -I ../zyxopenssl/include -L ../zyxopenssl/lib -ldl -lpthread -lcrypto

  • 相关阅读:
    SQL Server创建索引的技巧分析
    SQL Server创建索引
    kmp算法的应用
    相交环的面积
    Rebranding
    Olympiad
    找新朋友
    卡特兰数
    越狱
    Wolf and Rabbit
  • 原文地址:https://www.cnblogs.com/ZYX-bk-412/p/14733702.html
Copyright © 2011-2022 走看看