#include "FS.h"
void SampleEncryptionFile(void)
{
FS_FILE * pFile;
#if FS_SUPPORT_ENCRYPTION
const U8 aKey[8] = {1, 2, 3, 4}; // Required for encryption
FS_CRYPT_OBJ CryptObj; // Required for encryption
static FS_DES_CONTEXT _Context; // Required for encryption
static int _IsInited; // Required for encryption
#endif // FS_SUPPORT_ENCRYPTION
#if FS_SUPPORT_ENCRYPTION
//
// Create the encryption object. It contains all the necessary information
// for the encryption/decryption of data. This step must be performed only once.
//
if (_IsInited == 0) { // Required for encryption
FS_CRYPT_Prepare(&CryptObj, &FS_CRYPT_ALGO_DES, // Required for encryption
&_Context, 512, aKey); // Required for encryption
_IsInited = 1; // Required for encryption
}
#endif // FS_SUPPORT_ENCRYPTION
pFile = FS_FOpen("cipher.bin", "w");
if (pFile) {
#if FS_SUPPORT_ENCRYPTION
//
// Assign the created encryption object to file handle.
//
FS_SetEncryptionObject(pFile, &CryptObj); // Required for encryption
#endif // FS_SUPPORT_ENCRYPTION
//
// Write data to file using encryption.
//
FS_Write(pFile, "This message has been encrypted using SEGGER emFile.\n", 53);
FS_FClose(pFile);
}
}