cpp
复制代码
#include <openssl/aes.h>
#include <stdio.h>
#include <stdlib.h>
void AESEncrypt(const unsigned char *inputData, size_t dataSize, unsigned char *outputData, const unsigned char *key) {
AES_KEY aesKey;
AES_set_encrypt_key(key, 256, &aesKey);
size_t numBlocks = dataSize / 16;
for (size_t i = 0; i < numBlocks; ++i) {
AES_ecb_encrypt(inputData + i * 16, outputData + i * 16, &aesKey, AES_ENCRYPT);
}
}
void AESDecrypt(const unsigned char *inputData, size_t dataSize, unsigned char *outputData, const unsigned char *key) {
AES_KEY aesKey;
AES_set_decrypt_key(key, 256, &aesKey);
size_t numBlocks = dataSize / 16;
for (size_t i = 0; i < numBlocks; ++i) {
AES_ecb_encrypt(inputData + i * 16, outputData + i * 16, &aesKey, AES_DECRYPT);
}
}
int main() {
unsigned char key[32] = "1234567890abcdef1234567890abcdef"; // 32 字节
// Read the XML file
const char *filePath = "path_to_your_xml_file.xml";
FILE *file = fopen(filePath, "rb");
if (!file) {
perror("File open error");
return 1;
}
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
unsigned char *originalData = (unsigned char *)malloc(fileSize);
fread(originalData, 1, fileSize, file);
fclose(file);
// Allocate memory for encrypted data
unsigned char *encryptedData = (unsigned char *)malloc(fileSize);
AESEncrypt(originalData, fileSize, encryptedData, key);
// Write encrypted data back to the file
file = fopen(filePath, "wb");
if (!file) {
perror("File open error");
return 1;
}
fwrite(encryptedData, 1, fileSize, file);
fclose(file);
// Clean up
free(originalData);
free(encryptedData);
return 0;
}