Base64编码

完成下面任务(14分)

1. 在 Ubuntu 或 openEuler 中完成任务(推荐openEuler)

2. 手动对“你的姓名的首字母”进行 BASE64 编码,给出编码过程。(5 分)

比如WZY
ascii码对应的是
87 90 89
0x57 0x5A 0x59
01010111 01011010 01011001

010101 110101 101001 011001
0x15 0x35 0x29 0x19
base64编码为
V1pz

3. 使用OpenSSL 命令或者 Linux base64 命令验证你的编码的正确性(4 分)

[wzy@LAPTOP-PRC71A0C test2]$ echo -n "WZY" | openssl base64
V1pZ

4. 使用OpenSSL编程对sn.txt进行加密解密,提交代码或代码链接,以及编译运行过程(文本或截图)(5 分)

加密

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

// Base64编码表
static const char base64_chars[] = 
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "abcdefghijklmnopqrstuvwxyz"
    "0123456789+/";

size_t base64_encode(const unsigned char *data, size_t input_length, char *encoded_data) {
    size_t output_length = 0;
    unsigned char i;
    int bits;
    int idx;
    unsigned char c;

    while (input_length--) {
        i = *data++;

        // 处理第一个字节
        bits = (i & 0xFC) >> 2;
        c = (unsigned char)base64_chars[bits];
        encoded_data[output_length++] = c;

        bits = (i & 0x03) << 4;
        if (input_length) {
            i = *data++;
            bits |= (i & 0xF0) >> 4;
            c = (unsigned char)base64_chars[bits];
            encoded_data[output_length++] = c;

            bits = (i & 0x0F) << 2;
            if (input_length) {
                i = *data++;
                bits |= (i & 0xC0) >> 6;
                c = (unsigned char)base64_chars[bits];
                encoded_data[output_length++] = c;

                bits = i & 0x3F;
                c = (unsigned char)base64_chars[bits];
                encoded_data[output_length++] = c;
            } else {
                c = (unsigned char)base64_chars[bits];
                encoded_data[output_length++] = c;
                encoded_data[output_length++] = '=';
            }
        } else {
            c = (unsigned char)base64_chars[bits];
            encoded_data[output_length++] = c;
            encoded_data[output_length++] = '=';
        }
    }

    encoded_data[output_length] = '\0';
    return output_length;
}

int main() {
    const char *message = "Kimi";
    // 输出buffer需要有足够的空间来存储编码后的字符串和空终止符
    char *encoded_message = malloc(strlen(message) * 2 + 5);
    if (encoded_message == NULL) {
        perror("malloc");
        return 1;
    }

    size_t encoded_length = base64_encode((const unsigned char *)message, strlen(message), encoded_message);
    printf("Encoded message: %s\n", encoded_message);
    printf("Encoded length: %zu\n", encoded_length);

    free(encoded_message);
    return 0;
}
[wzy@LAPTOP-PRC71A0C test2]$ vim base.c
[wzy@LAPTOP-PRC71A0C test2]$ gcc -o base base.c -lcrypto
base.c: In function ‘main’:
base.c:61:29: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration]
   61 |     char *encoded_message = malloc(strlen(message) * 2 + 5);
      |                             ^~~~~~
base.c:3:1: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
    2 | #include <string.h>
  +++ |+#include <stdlib.h>
    3 |
base.c:61:29: warning: incompatible implicit declaration of built-in function ‘malloc’ [-Wbuiltin-declaration-mismatch]
   61 |     char *encoded_message = malloc(strlen(message) * 2 + 5);
      |                             ^~~~~~
base.c:61:29: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
base.c:71:5: warning: implicit declaration of function ‘free’ [-Wimplicit-function-declaration]
   71 |     free(encoded_message);
      |     ^~~~
base.c:71:5: note: include ‘<stdlib.h>’ or provide a declaration of ‘free’
base.c:71:5: warning: incompatible implicit declaration of built-in function ‘free’ [-Wbuiltin-declaration-mismatch]
base.c:71:5: note: include ‘<stdlib.h>’ or provide a declaration of ‘free’

[wzy@LAPTOP-PRC71A0C test2]$ ./base
Encoded message: V1pZAG1hbA=
Encoded length: 11

提交要求 (1’)

  1. 记录实践过程和 AI 问答过程,尽量不要截图,给出文本内容
  2. (选做)推荐所有作业托管到 gitee或 github 上
  3. (必做)提交作业 markdown文档,命名为“学号-姓名-作业题目.md”
  4. (必做)提交作业 markdown文档转成的 PDF 文件,命名为“学号-姓名-作业题目.pdf”
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值