Vanilla.PDF  1.6.0
Cross-platform toolkit for creating and modifying PDF documents
decrypt.c

Example use of encrypted files, protected with password or certificate.

#include "tools.h"
void print_decrypt_help() {
printf("Usage: decrypt -s [source file] -p [password] -k [path to PKCS#12]");
}
int process_decrypt(int argc, char *argv[]) {
FileHandle* file = NULL;
string_type password = NULL;
string_type cert_path = NULL;
string_type file_path = NULL;
integer_type i = 0;
for (i = 0; i < argc; ++i) {
// password
if (strcmp(argv[i], "-p") == 0 && (i + 1 < argc)) {
password = argv[i + 1];
i++;
// certificate
} else if (strcmp(argv[i], "-k") == 0 && (i + 1 < argc)) {
cert_path = argv[i + 1];
i++;
// source file path
} else if (strcmp(argv[i], "-s") == 0 && (i + 1 < argc)) {
file_path = argv[i + 1];
i++;
} else {
print_decrypt_help();
return VANILLAPDF_TOOLS_ERROR_INVALID_PARAMETERS;
}
}
if (file_path == NULL) {
print_decrypt_help();
return VANILLAPDF_TOOLS_ERROR_INVALID_PARAMETERS;
}
RETURN_ERROR_IF_NOT_SUCCESS(File_Open(file_path, &file));
if (password != NULL) {
RETURN_ERROR_IF_NOT_SUCCESS(File_SetEncryptionPassword(file, password));
}
if (cert_path != NULL) {
PKCS12KeyHandle* pkcs12_key = NULL;
EncryptionKeyHandle* encryption_key = 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(file, encryption_key));
RETURN_ERROR_IF_NOT_SUCCESS(EncryptionKey_Release(encryption_key));
RETURN_ERROR_IF_NOT_SUCCESS(PKCS12Key_Release(pkcs12_key));
}
// File contents are now accessible
RETURN_ERROR_IF_NOT_SUCCESS(File_Release(file));
return VANILLAPDF_TOOLS_ERROR_SUCCESS;
}
Represents encryption key other than password.
error_type CALLING_CONVENTION EncryptionKey_Release(EncryptionKeyHandle *handle)
Decrement the internal reference counter.
Represents low-level file access handle.
error_type CALLING_CONVENTION File_SetEncryptionKey(FileHandle *handle, EncryptionKeyHandle *key)
Set files encryption key.
error_type CALLING_CONVENTION File_Release(FileHandle *handle)
Decrement the internal reference counter.
error_type CALLING_CONVENTION File_Open(string_type filename, FileHandle **result)
Opens a file for reading.
error_type CALLING_CONVENTION File_SetEncryptionPassword(FileHandle *handle, string_type password)
Set encryption password.
PKCS#12 container is often used in cryptography to store asymmetric key pair.
error_type CALLING_CONVENTION PKCS12Key_Release(PKCS12KeyHandle *handle)
Decrement the internal reference counter.
error_type CALLING_CONVENTION PKCS12Key_CreateFromFile(string_type path, string_type password, PKCS12KeyHandle **result)
Uses PKCS#12 standard format to access asymmetric keys.
error_type CALLING_CONVENTION PKCS12Key_ToEncryptionKey(PKCS12KeyHandle *handle, EncryptionKeyHandle **result)
Reinterpret current object as EncryptionKeyHandle.
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