Example use of encrypted files, protected with password or certificate.
#include "tools.h"
void print_decrypt_help() {
printf("Usage: decrypt -s [source file] -d [destination file] -p [password] -k [path to PKCS#12] -l [license file]");
}
int process_decrypt(int argc, char *argv[]) {
for (i = 0; i < argc; ++i) {
if (strcmp(argv[i], "-p") == 0 && (i + 1 < argc)) {
password = argv[i + 1];
i++;
} else if (strcmp(argv[i], "-k") == 0 && (i + 1 < argc)) {
cert_path = argv[i + 1];
i++;
} else if (strcmp(argv[i], "-s") == 0 && (i + 1 < argc)) {
source_file_path = argv[i + 1];
i++;
} else if (strcmp(argv[i], "-d") == 0 && (i + 1 < argc)) {
destination_file_path = argv[i + 1];
i++;
} else if (strcmp(argv[i], "-l") == 0 && (i + 1 < argc)) {
license_file_path = argv[i + 1];
i++;
}
else {
print_decrypt_help();
return VANILLAPDF_TOOLS_ERROR_INVALID_PARAMETERS;
}
}
if (source_file_path == NULL) {
print_decrypt_help();
return VANILLAPDF_TOOLS_ERROR_INVALID_PARAMETERS;
}
if (destination_file_path == NULL) {
print_decrypt_help();
return VANILLAPDF_TOOLS_ERROR_INVALID_PARAMETERS;
}
if (license_file_path == NULL) {
print_decrypt_help();
return VANILLAPDF_TOOLS_ERROR_INVALID_PARAMETERS;
}
if (license_file_path != NULL) {
RETURN_ERROR_IF_NOT_SUCCESS(LicenseInfo_SetLicenseFile(license_file_path));
}
RETURN_ERROR_IF_NOT_SUCCESS(File_Open(source_file_path, &source_file));
RETURN_ERROR_IF_NOT_SUCCESS(Document_OpenFile(source_file, &source_document));
if (password != NULL) {
RETURN_ERROR_IF_NOT_SUCCESS(File_SetEncryptionPassword(source_file, password));
}
if (cert_path != NULL) {
RETURN_ERROR_IF_NOT_SUCCESS(PKCS12Key_CreateFromFile(cert_path, NULL, &pkcs12_key));
RETURN_ERROR_IF_NOT_SUCCESS(PKCS12Key_ToEncryptionKey(pkcs12_key, &encryption_key));
RETURN_ERROR_IF_NOT_SUCCESS(File_SetEncryptionKey(source_file, encryption_key));
RETURN_ERROR_IF_NOT_SUCCESS(EncryptionKey_Release(encryption_key));
RETURN_ERROR_IF_NOT_SUCCESS(PKCS12Key_Release(pkcs12_key));
}
RETURN_ERROR_IF_NOT_SUCCESS(Document_RemoveEncryption(source_document));
RETURN_ERROR_IF_NOT_SUCCESS(Document_Save(source_document, destination_file_path));
RETURN_ERROR_IF_NOT_SUCCESS(Document_Release(source_document));
RETURN_ERROR_IF_NOT_SUCCESS(File_Release(source_file));
return VANILLAPDF_TOOLS_ERROR_SUCCESS;
}
Represents high-level file access handle.
Represents encryption key other than password.
Represents low-level file access handle.
PKCS#12 container is often used in cryptography to store asymmetric key pair.
int32_t integer_type
32-bit signed integer
Definition c_types.h:51
const char * string_type
C-Style string.
Definition c_types.h:82