111

gcc -g sm4txtcode.c -o sm4_en_de_txt -L/usr/lib -lssl -lcrypto

#include <openssl/evp.h>
#include <openssl/err.h>
#include <string.h>

#define BUF_SIZE 1024

int main(int argc, char *argv[]) {
    // 检查命令行参数
    if (argc != 4) {
        printf("Usage: %s <input file> <encrypted output file> <decrypted output file>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    // 初始化 OpenSSL
    OpenSSL_add_all_algorithms();
    ERR_load_crypto_strings();

    // 设置加密上下文
    EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
    if (!ctx) {
        printf("Error creating context\n");
        exit(EXIT_FAILURE);
    }
    const EVP_CIPHER *cipher = EVP_sm4_ecb();
    unsigned char key[EVP_MAX_KEY_LENGTH] = {0};
    unsigned char iv[EVP_MAX_IV_LENGTH] = {0};
    char *student_id = "20201326"; // 用你自己的8位学号替换
    printf("学号%s\n",student_id);
    strncpy(key, student_id, 8);
    if (EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv) != 1) {
        printf("Error setting up encryption context\n");
        exit(EXIT_FAILURE);
    }

    // 打开输入文件
    FILE *in_file = fopen(argv[1], "rb");
    if (!in_file) {
        printf("Error opening input file\n");
        exit(EXIT_FAILURE);
    }

    // 打开加密输出文件
    FILE *enc_file = fopen(argv[2], "wb");
    if (!enc_file) {
        printf("Error opening encrypted output file\n");
        exit(EXIT_FAILURE);
    }

    // 加密输入文件并写入加密输出文件
    unsigned char in_buf[BUF_SIZE] = {0};
    unsigned char enc_buf[BUF_SIZE + EVP_MAX_BLOCK_LENGTH] = {0};
    int num_bytes_read, enc_len;
    while ((num_bytes_read = fread(in_buf, sizeof(unsigned char), BUF_SIZE, in_file)) > 0) {
        if (EVP_EncryptUpdate(ctx, enc_buf, &enc_len, in_buf, num_bytes_read) != 1) {
            printf("Error encrypting data\n");
            exit(EXIT_FAILURE);
        }
        fwrite(enc_buf, sizeof(unsigned char), enc_len, enc_file);
    }
    if (num_bytes_read < 0) {
        printf("Error reading input file\n");
        exit(EXIT_FAILURE);
    }

    // 完成加密
    if (EVP_EncryptFinal_ex(ctx, enc_buf, &enc_len) != 1) {
        printf("Error finalizing encryption\n");
        exit(EXIT_FAILURE)
#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("20201319.dat","rb");//打开待编码的文件
    if(infp == NULL)
    {
        printf("Open File \"20201319.dat\"  for Read Err.\n");
        return;
    }
    
    outfp = fopen("20201319.txt","w");//打开编码后保存的文件
    if(outfp == NULL)
    {
        printf("Open File \"20201319.txt\" For Write Err.\n");
        return;
    }
    EVP_EncodeInit(ctx);//Base64编码初始化
    printf("文件\"20201212.dat\" Base64编码后为:\n");
    //循环读取原文,并调用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("对文件\"20201319.dat\" Base64编码完成,保存到\"20201319.txt\"文件.\n\n\n");
}

//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("20201319.txt","r");//打开待解码的文件
    if(infp == NULL)
    {
        printf("Open File \"20201319.txt\"  for Read Err.\n");
        return;
    }
    outfp = fopen("20201319-1.dat","wb");//打开解码后保存的文件
    if(outfp == NULL)
    {
        printf("Open File \"20201319-1.txt\" For Write Err.\n");
        return;
    }
    EVP_DecodeInit(ctx);//Base64解码初始化
    printf("开始对文件\"20201319.txt\" Base64解码...\n\n");
    //循环读取原文,并调用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("对文件\"20201319.txt\" Base64解码完成,保存为\"20201319-1.dat\"\n\n\n");
    
}
 
int main()
{
 
    tEVP_Encode();
    tEVP_Decode();
    
    return 0;
}


posted @ 2022-11-21 08:10  20201319吴向林  阅读(35)  评论(0编辑  收藏  举报