问题描述
要求
我是一名开发人员,但直到一周前我对 C 的经验几乎为零。对于一个项目,我需要加密/解密源代码/文本文件,并且需要为此使用 C。
到现在为止我做了什么?
我正在使用 Kokke/tony-AES-c 库,并按照 this Gist 中的说明使用 pkcs7 填充实现它。
在我的代码(如下所示)中,我有一个函数 my_encrypt
,它加密内容并返回 uint8_t
数组(包含十六进制)。我需要保存它并稍后从文件中读取以进行解密。我知道如何将字符串保存到文件中,但如何将 uint8_t
保存到文件中。
为此,我需要将 uint8_t
转换为字符串,但在尝试多种解决方案后,我无法做到。
问题
如何将 uint8_t
数组(包含十六进制)转换为 C 中的字符串。
我的代码
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <stdlib.h>
#define CBC 1
#define DECRYPT 0;
#define ENCRYPT 1;
#include "aes.h"
#include "pkcs7_padding.h"
const uint8_t * my_encrypt(char *report) {
//Initialization Vector
uint8_t iv[] = { 0x75,0x52,0x5f,0x69,0x6e,0x74,0x65,0x72,0x73,0x67,0x21,0x21 };
uint8_t i;
// char* report = "my super secret thing that needs to remain that way!";
char* key = "thisIstheKey";
int dlen = strlen(report);
int klen = strlen(key);
printf("THE PLAIN TEXT STRING = ");
for (i=0; i<dlen;i++){
printf("%c",report[i]);
}
printf("\n");
//Proper Length of report
int dlenu = dlen;
if (dlen % 16) {
dlenu += 16 - (dlen % 16);
printf("The original length of the STRING = %d and the length of the padded STRING = %d\n",dlen,dlenu);
}
//Proper length of key
int klenu = klen;
if (klen % 16) {
klenu += 16 - (klen % 16);
printf("The original length of the KEY = %d and the length of the padded KEY = %d\n",klen,klenu);
}
// Make the uint8_t arrays
uint8_t hexarray[dlenu];
uint8_t kexarray[klenu];
// Initialize them with zeros
memset( hexarray,dlenu );
memset( kexarray,klenu );
// Fill the uint8_t arrays
for (int i=0;i<dlen;i++) {
hexarray[i] = (uint8_t)report[i];
}
for (int i=0;i<klen;i++) {
kexarray[i] = (uint8_t)key[i];
}
int reportPad = pkcs7_padding_pad_buffer( hexarray,sizeof(hexarray),16 );
int keyPad = pkcs7_padding_pad_buffer( kexarray,sizeof(kexarray),16 );
printf("The padded STRING in hex is = ");
for (i=0; i<dlenu;i++){
printf("%02x",hexarray[i]);
}
printf("\n");
printf("The padded key in hex is = ");
for (i=0; i<klenu;i++){
printf("%02x",kexarray[i]);
}
printf("\n");
// In case you want to check if the padding is valid
int valid = pkcs7_padding_valid( hexarray,16 );
int valid2 = pkcs7_padding_valid( kexarray,16 );
printf("Is the pkcs7 padding valid report = %d | key = %d\n",valid,valid2);
//start the encryption
struct AES_ctx ctx;
AES_init_ctx_iv(&ctx,kexarray,iv);
// encrypt
AES_CBC_encrypt_buffer(&ctx,hexarray,dlenu);
printf("the encrypted STRING = ");
for (i=0; i<dlenu;i++){
printf("%02x",hexarray[i]);
}
printf("\n");
return hexarray;
}
int main(int argc,char *argv[]) {
// File pointer
FILE *input_file;
int operation;
char * input_file_buffer = 0;
long input_file_length;
// ////////////////////////////////////////////////////////
// Read command line arguments
// ////////////////////////////////////////////////////////
// Make sure proper command params are supplied
if (argc != 3) {
printf("Usage: %s encrypt/decrypt filename\n",argv[0]);
return -1;
}
// Define operation type
if (strcmp(argv[1],"encrypt") == 0) {
operation = ENCRYPT;
} else if (strcmp(argv[1],"decrypt") == 0) {
operation = DECRYPT;
}
// ////////////////////////////////////////////////////////
// Open File contents
// ////////////////////////////////////////////////////////
// Open input_file to encrypt/decrypt.
input_file = fopen(argv[2],"rb");
if (!input_file) {
fprintf(stderr,"ERROR: '%s' file fopen error: %s\n",argv[2],strerror(errno));
return errno;
}
// Read contents of file in buffer
fseek(input_file,SEEK_END);
input_file_length = ftell (input_file);
fseek(input_file,SEEK_SET);
input_file_buffer = malloc (input_file_length);
if (input_file_buffer)
{
fread(input_file_buffer,1,input_file_length,input_file);
}
// Close input_file
fclose(input_file);
// We have contents of file in input_file_buffer,let's print them.
printf("File contents:\n-------\n%s\n",input_file_buffer);
// Let's encrypt input_file_buffer
uint8_t result = 0;
result = my_encrypt(input_file_buffer);
// Convery result to string.
printf("main: Encrypted File contents:\n-------\n%d\n",result);
return 0;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)