function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Krishnaveni A 2Krishnaveni A 2 

Encrypt certain Files and restrict the file access to specific users via Apex!

My requirement is to encrypt few files while uploading and restrict the view access to specific users for the same files. There will be a checkbox available based on which the files should be encrypted or not. If the checkbox is TRUE, encrypt the file while uploading and also restrict the view access to specific users for the same file. I know it is possible with configuration but here I need to achieve this via apex code. 
In order to restrict view access for users I tried the below code:

ContentDocumentLink cdl = new ContentDocumentLink();
cdl.ContentDocumentId = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id ='0687F000006FlIjQAK'].ContentDocumentId;
cdl.LinkedEntityId = '0057F000002yWGe';
cdl.ShareType = 'V';
insert cdl;

When I tried this, it worked. Only if I give sharetype = 'V' only the files are visisble to the user. The user here is LinkedEntityId. 
I am not sure whether  this approach is correct.

In order to encypt a file I tried using CRYPTO class methods available in Apex. But I was not able to fetch the content of the file using this. How do fetch the file content and encrypt it? Also the view access should be restricted.

Please help me with both the requirements.
Thanks in Advance!