当前位置: 首页 > news >正文

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”

http://www.mrgr.cn/news/57721.html

相关文章:

  • 【深入学习Redis丨第八篇】详解Redis数据持久化机制
  • Java工具类--OkHttp工具类
  • learn C++ NO.28——C++11
  • 使用原生HTML和css制作一个箭头步骤条
  • PyTorch 中各类损失函数介绍
  • @Autowired和@Resource的用法与区别
  • 真AI遇到招聘管理系统,帮助企业打造新质生产力
  • Vue3:横向滑动导航组件路由跳转保留滚动(条)量
  • HKC双模显示器评测报告 - HKC G27H7Pro
  • 1688API商品详情接口如何获取
  • 解锁PDF权限密码
  • 腾讯地图SDK 手势失效或冲突的解决办法
  • 没有基础,学习HCIE难吗?
  • 【多商户商城适用于哪些行业】
  • (北京餐饮满意度调查公司)餐饮企业顾客满意度调查,赢得口碑的关键
  • Selenium 流程自动化
  • 简单解析WebAPI与WebService的区别
  • 数据连接池的工作机制
  • 无线领夹麦克风怎么挑选,麦克风行业常见踩坑点,避雷不专业产品
  • HarmonyOS 鸿蒙面试第一弹
  • NumPy(by千锋教育)
  • 无废话、光速上手 React-Router
  • ORA-12170: TNS: 连接超时,oracle透过防火墙windows设置USE_SHARED_SOCKET=TRUE
  • 牛客网热度最高的17套一线大厂Java面试八股文!面面俱到,太全了
  • 国家眼部疾病临床病例集《眼视光案例荟》首刊在三亚隆重发布
  • 070_基于springboot的Q高中素质评价档案系统