DER

第二题代码

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

void write_der_encoded_integer(const char *filename, uint32_t integer) {
    // 打开文件以便写入
    FILE *file = fopen(filename, "wb");
    if (file == NULL) {
        perror("Failed to open file");
        exit(EXIT_FAILURE);
    }

    // DER编码整数
    // 首先,计算整数的长度
    uint32_t temp = integer;
    int length = 0;
    while (temp > 0) {
        length++;
        temp >>= 8;
    }

    // 写入类型标签(整数的类型标签为0x02)
    uint8_t type_tag = 0x02;
    fwrite(&type_tag, 1, 1, file);

    // 写入长度
    fwrite(&length, 1, 1, file);

    // 写入整数的字节表示
    for (int i = length - 1; i >= 0; i--) {
        uint8_t byte = (integer >> (8 * i)) & 0xFF;
        fwrite(&byte, 1, 1, file);
    }

    // 关闭文件
    fclose(file);
}

int main() {
    // 要编码的整数
    uint32_t integer = 1346665;

    // 输出文件名称
    const char *filename = "20211301.der";

    // 调用函数进行编码并写入文件
    write_der_encoded_integer(filename, integer);

    printf("DER encoded integer written to %s\n", filename);
    return 0;
}

第三题代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_CMD_LEN 256

void base64_encode(const char *input_file, const char *output_file) {
    char cmd[MAX_CMD_LEN];
    snprintf(cmd, MAX_CMD_LEN, "openssl base64 -in \"%s\" -out \"%s\"", input_file, output_file);
    system(cmd);
}

void base64_decode(const char *input_file, const char *output_file) {
    char cmd[MAX_CMD_LEN];
    snprintf(cmd, MAX_CMD_LEN, "openssl base64 -d -in \"%s\" -out \"%s\"", input_file, output_file);
    system(cmd);
}

int main() {
    const char *input_file = "你的学号.der";
    const char *encoded_file = "你的学号.pem";
    const char *decoded_file = "decoded.der";

    // 编码文件
    base64_encode(input_file, encoded_file);
    printf("File %s encoded to base64 and saved as %s\n", input_file, encoded_file);

    // 解码文件
    base64_decode(encoded_file, decoded_file);
    printf("Base64 encoded file %s decoded and saved as %s\n", encoded_file, decoded_file);

    return 0;
}

第四题

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_CMD_LEN 256

void base64_encode(const char *input_file, const char *output_file) {
    char cmd[MAX_CMD_LEN];
    snprintf(cmd, MAX_CMD_LEN, "openssl base64 -in \"%s\" -out \"%s\"", input_file, output_file);
    system(cmd);
}

void base64_decode(const char *input_file, const char *output_file) {
    char cmd[MAX_CMD_LEN];
    snprintf(cmd, MAX_CMD_LEN, "openssl base64 -d -in \"%s\" -out \"%s\"", input_file, output_file);
    system(cmd);
}

int main() {
    const char *input_file = "20211301.der";
    const char *encoded_file = "20211301.pem";
    const char *decoded_file = "decoded.der";

    // 编码文件
    base64_encode(input_file, encoded_file);
    printf("File %s encoded to base64 and saved as %s\n", input_file, encoded_file);

    // 解码文件
    base64_decode(encoded_file, decoded_file);
    printf("Base64 encoded file %s decoded and saved as %s\n", encoded_file, decoded_file);

    return 0;
}
posted @ 2024-04-18 14:30  20211301郑润芃  阅读(1)  评论(0编辑  收藏  举报