【无标题】

手动编码过程
将“LZJ”转换为ASCII码:

L -> 76
Z -> 90
J -> 74
将ASCII码合并成一个24位的二进制数:

L: 01001100
Z: 01011010
J: 01001010
合并后:01001100 01011010 01001010
将24位二进制数分成4组,每组6位:

010 | 011 | 001 | 010 | 1101 | 0010 | 1010
将每组6位二进制数转换为十进制数:

010 -> 2
011 -> 3
001 -> 1
010 -> 2
1101 -> 13
0010 -> 2
1010 -> 10
将十进制数转换为BASE64字符:

2 -> C
3 -> D
1 -> A
2 -> G
13 -> N
2 -> K
10 -> K
由于“LZJ”有三个字符,我们需要添加两个等号(==)来表示填充,因为BASE64编码要求输出长度必须是4的倍数。

所以,“LZJ”的BASE64编码结果是“Q0FKYg==”。

使用OpenSSL命令验证编码的正确性

echo -n “LZJ” | base64
或者使用 OpenSSL:
echo -n “LZJ” | openssl base64
这两个命令都会输出**“Q0FKYg==”**
代码如下
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <stdio.h>
#include <string.h>

#define AES_KEY_SIZE 32 // 256-bit key
#define AES_BLOCK_SIZE 16 // AES block size

// 生成随机密钥和初始化向量 (IV)
void generate_key_iv(unsigned char *key, unsigned char *iv) {
if (RAND_bytes(key, AES_KEY_SIZE) != 1) {
fprintf(stderr, “Error generating random key\n”);
exit(1);
}
if (RAND_bytes(iv, AES_BLOCK_SIZE) != 1) {
fprintf(stderr, “Error generating random IV\n”);
exit(1);
}
}

// 加密函数
void encrypt_file(const char *input_file, const char *output_file, unsigned char *key, unsigned char *iv) {
FILE *infile = fopen(input_file, “rb”);
FILE *outfile = fopen(output_file, “wb”);
if (!infile || !outfile) {
perror(“Error opening file”);
exit(1);
}
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (!ctx) {
fprintf(stderr, “Error creating cipher context\n”);
exit(1);
}
if (EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv) != 1) {
fprintf(stderr, “Error initializing encryption\n”);
exit(1);
}
unsigned char inbuf[AES_BLOCK_SIZE], outbuf[AES_BLOCK_SIZE + EVP_MAX_BLOCK_LENGTH];
int bytes_read, outlen;
while ((bytes_read = fread(inbuf, 1, sizeof(inbuf), infile)) > 0) {
if (EVP_EncryptUpdate(ctx, outbuf, &outlen, inbuf, bytes_read) != 1) {
fprintf(stderr, “Error during encryption\n”);
exit(1);
}
fwrite(outbuf, 1, outlen, outfile);
}
if (EVP_EncryptFinal_ex(ctx, outbuf, &outlen) != 1) {
fprintf(stderr, “Error finalizing encryption\n”);
exit(1);
}
fwrite(outbuf, 1, outlen, outfile);
EVP_CIPHER_CTX_free(ctx);
fclose(infile);
fclose(outfile);
}

// 解密函数
void decrypt_file(const char *input_file, const char *output_file, unsigned char *key, unsigned char *iv) {
FILE *infile = fopen(input_file, “rb”);
FILE *outfile = fopen(output_file, “wb”);
if (!infile || !outfile) {
perror(“Error opening file”);
exit(1);
}
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (!ctx) {
fprintf(stderr, “Error creating cipher context\n”);
exit(1);
}
if (EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv) != 1) {
fprintf(stderr, “Error initializing decryption\n”);
exit(1);
}
unsigned char inbuf[AES_BLOCK_SIZE], outbuf[AES_BLOCK_SIZE + EVP_MAX_BLOCK_LENGTH];
int bytes_read, outlen;
while ((bytes_read = fread(inbuf, 1, sizeof(inbuf), infile)) > 0) {
if (EVP_DecryptUpdate(ctx, outbuf, &outlen, inbuf, bytes_read) != 1) {
fprintf(stderr, “Error during decryption\n”);
exit(1);
}
fwrite(outbuf, 1, outlen, outfile);
}
if (EVP_DecryptFinal_ex(ctx, outbuf, &outlen) != 1) {
fprintf(stderr, “Error finalizing decryption\n”);
exit(1);
}
fwrite(outbuf, 1, outlen, outfile);
EVP_CIPHER_CTX_free(ctx);
fclose(infile);
fclose(outfile);
}

int main() {
unsigned char key[AES_KEY_SIZE], iv[AES_BLOCK_SIZE];
// 生成随机密钥和 IV
generate_key_iv(key, iv);
// 加密文件
encrypt_file(“sn.txt”, “sn.enc”, key, iv);
printf(“File encrypted to sn.enc\n”);
// 解密文件
decrypt_file(“sn.enc”, “sn_decrypted.txt”, key, iv);
printf(“File decrypted to sn_decrypted.txt\n”);
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值