AESFile Manual

About

The class AESFile provides means to create and work with AES encrypted files. Derived from BaseFile working with AES files works exactly the same as with other files, with the only exception of opening the file. So it is recommended to read BaseFile Manual first.

Warning
For encryption using the MAXON API see Stream Conversions Manual.
// This example demonstrates encrypting a file and comparing the encrypted result with the original file.
// Let user choose a file to encrypt.
Filename fnInput;
if (!fnInput.FileSelect(FILESELECTTYPE::ANYTHING, FILESELECT::LOAD, "Open file to encrypt..."_s))
return maxon::OK;
if (!fileInput || !fileAes)
return maxon::OutOfMemoryError(MAXON_SOURCE_LOCATION);
// Open the input file.
{
return maxon::IoError(MAXON_SOURCE_LOCATION, MaxonConvert(fnInput, MAXONCONVERTMODE::NONE), "Error: Failed to open input file."_s);
}
Filename fnAes = fnInput;
fnAes.SetSuffix("aes"_s);
// Note: Password needs to be either 16, 24, or 32 characters long.
const char pw[] = { 's', 'p', 'e', 'a', 'k', ',', ' ', 'f', 'r', 'i', 'e', 'n', 'd', ',', ' ', 'a', 'n', 'd', ' ', 'e', 'n', 't', 'e', 'r' };
const Int32 keylen = sizeof(pw) * 8; // keylen is in bits
DebugAssert((keylen == 128) || (keylen == 192) || (keylen == 256));
const Int32 blocksize = 256;
if (!fileAes->Open(fnAes, pw, keylen, blocksize, 0, FILEOPEN::WRITE))
{
return maxon::IoError(MAXON_SOURCE_LOCATION, MaxonConvert(fnAes, MAXONCONVERTMODE::NONE), "Error: Failed to open AES file for writing."_s);
}
const Int fileSize = fileInput->GetLength();
Char* buffer = NewMemClear(Char, fileSize) iferr_return;
const Int readBytes = fileInput->TryReadBytes(buffer, fileSize);
if (readBytes != fileSize)
{
return maxon::UnexpectedError(MAXON_SOURCE_LOCATION, "Error: Failed to read data from input file."_s);
}
if (!fileAes->WriteBytes(buffer, fileSize))
{
return maxon::UnexpectedError(MAXON_SOURCE_LOCATION, "Error: Failed to write data to AES file."_s);
}
fileAes->Close();
fileInput->Close();
// Finally check the encryption result.
if (AESFile::CheckEncryption(fnAes, fnInput, pw, keylen, blocksize))
{
ApplicationOutput("Success: The encrypted file matches the encrypted version."_s);
return maxon::OK;
}
else
{
return maxon::UnexpectedError(MAXON_SOURCE_LOCATION, "Error: The encrypted file does not match the encrypted version."_s);
}

Allocation/Deallocation

AESFile objects are created with the usual tools, see Entity Creation and Destruction Manual (Classic).

Open

Warning
For a plugin to be cross platform then the type and creator parameters must be correctly filled for Mac.

Miscellaneous

Further Reading

BaseFile::Close
Bool Close()
BaseFile::TryReadBytes
Int TryReadBytes(void *data, Int len)
BaseFile::GetLength
Int64 GetLength()
Int
maxon::Int Int
Definition: ge_sys_math.h:64
MACTYPE_CINEMA
#define MACTYPE_CINEMA
Standard Mac type code for Cinema 4D.
Definition: ge_prepass.h:30
NewMemClear
#define NewMemClear(T, cnt)
Definition: defaultallocator.h:204
FILEDIALOG::ANY
@ ANY
Show an error dialog for any error.
AESFile::CheckEncryption
static Bool CheckEncryption(const Filename &encrypt, const Filename &decrypt, const char *key, Int32 keylen, Int32 blocksize)
BYTEORDER::V_INTEL
@ V_INTEL
Intel, little endian.
Filename
Manages file and path names.
Definition: c4d_file.h:93
maxon::OK
return OK
Definition: apibase.h:2546
FILESELECTTYPE::ANYTHING
@ ANYTHING
Any file.
MAXONCONVERTMODE::NONE
@ NONE
No check if file exists under case-sensitive drives.
iferr_return
#define iferr_return
Definition: resultbase.h:1465
MAXON_SOURCE_LOCATION
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:66
BaseFile::Open
Bool Open(const Filename &name, FILEOPEN mode=FILEOPEN::READ, FILEDIALOG error_dialog=FILEDIALOG::IGNOREOPEN, BYTEORDER order=BYTEORDER::V_MOTOROLA, Int32 type='C4DC', Int32 creator='C4D1')
Filename::SetSuffix
void SetSuffix(const maxon::String &str)
MaxonConvert
maxon::Url MaxonConvert(const Filename &fn, MAXONCONVERTMODE convertMode)
BaseFile::WriteBytes
Bool WriteBytes(const void *data, Int len)
FILEOPEN::WRITE
@ WRITE
AESFile::Open
Bool Open(const Filename &name, const char *key, Int32 keylen, Int32 blocksize, UInt32 aes_flags, FILEOPEN mode=FILEOPEN::READ, FILEDIALOG error_dialog=FILEDIALOG::IGNOREOPEN, BYTEORDER order=BYTEORDER::V_MOTOROLA, Int32 type='C4DC', Int32 creator='C4D1')
Int32
maxon::Int32 Int32
Definition: ge_sys_math.h:60
ApplicationOutput
#define ApplicationOutput(formatString,...)
Definition: debugdiagnostics.h:208
MACCREATOR_CINEMA
#define MACCREATOR_CINEMA
Standard Mac creator code for Cinema 4D.
Definition: ge_prepass.h:31
FILESELECT::LOAD
@ LOAD
Load dialog.
FILEOPEN::READ
@ READ
Open the file for reading.
AutoAlloc
Definition: ge_autoptr.h:36
DebugAssert
#define DebugAssert(condition,...)
Definition: debugdiagnostics.h:246
Char
maxon::Char Char
Definition: ge_sys_math.h:56
Filename::FileSelect
Bool FileSelect(FILESELECTTYPE type, FILESELECT flags, const maxon::String &title, const maxon::String &force_suffix=maxon::String())