6. Encrypting & Decrypting Bytes with SAS-RCS by: saaiqSAS Last Updated: 25/2/2025 In this section, we will demonstrate how to encrypt and decrypt byte[] data using the SAS-RCS (Saaiq Abdulla Saeed's - Random Character Substitution) encryption algorithm - Text-level encryption. This process requires key reference IDs, which are int values returned by key extraction methods. A usage sample of the ROSET Java API is available on GitHub. You can view it here (ROSET Java API Usage Sample). To perform encryption or decryption with the ROSET API, you will need a Dynamic Key reference ID and an int[] array of Static Key reference IDs. These reference IDs are used to locate and identify the specific keys that have already been extracted and are stored in memory. Encrypt byte array To encrypt a byte[] using SAS-RCS, you can call the method rcsByteEncrypt(int Dynamic_Key_ID, int[] Static_Key_IDs_Array, byte[] data). This method will return the SAS-RCS encrypted byte[] data as String. Below is an example that demonstrates how to use this method for encryption. // Key Extraction int Dynamic_Key_ID = // extract Dynamic Key int Static_Key_ID_1 = // extract Static Key 1 int Static_Key_ID_2 = // extract Static Key 2 int Static_Key_ID_3 = // extract Static Key 3 // Prepare Static Key Reference ID array int[] Static_Key_IDs_Array = {Static_Key_ID_1, Static_Key_ID_2, Static_Key_ID_3}; // Set up 'Quick Processing' (OPTIONAL, BUT RECOMMENDED for better performance) boolean isSet = SAS_ROSET.setQuickProcessing(true, Dynamic_Key_ID, Static_Key_IDs_Array); // Encrypt the byte array byte[] byte_data = { (byte) 100, (byte) -35, (byte) 42, (byte) -52, (byte) 96 }; String encrypted_byte_data = SAS_ROSET.rcsByteEncrypt(Dynamic_Key_ID, Static_Key_IDs_Array, byte_data); Please note that if your Dynamic Key is using SAS-RGM base64 encoding, the byte[] length should ideally be a multiple of 3. However, if the byte[] represents the last set of bytes in a larger data store (such as a file), this rule may not apply. As a general practice, it is recommended to always pass byte[] data in multiples of 3 unless the byte set is the final portion of a larger dataset. While you can pass byte[] data of any length to rcsByteEncrypt(), if you require the encrypted output to be of a specific length, you must ensure the input byte[] is of a particular size. To determine the required length for your data, use the method rcsNumOfBytesToPass(int Dynamic_Key_ID, int preferred_string_length). // Key Extraction int Dynamic_Key_ID = // extract Dynamic Key used for encryption // Find the length of byte[] to pass for the preferred encrypted string length int preferred_length = 1000; // Maximum desired text output length int byte_array_length = SAS_ROSET.rcsNumOfBytesToPass(Dynamic_Key_ID, preferred_length); Decrypt Encrypted byte array To decrypt a SAS-RCS encrypted byte[], use the method rcsByteDecrypt(int Dynamic_Key_ID, int[] Static_Key_IDs_Array, byte[] encrypted_data). Below is an example demonstrating how to implement this decryption method. // Key Extraction int Dynamic_Key_ID = // extract Dynamic Key used for encryption int Static_Key_ID_1 = // extract Static Key 1 used for encryption int Static_Key_ID_2 = // extract Static Key 2 used for encryption int Static_Key_ID_3 = // extract Static Key 3 used for encryption // Prepare Static Key Reference ID array int[] Static_Key_IDs_Array = {Static_Key_ID_1, Static_Key_ID_2, Static_Key_ID_3}; // Set up 'Quick Processing' (OPTIONAL, BUT RECOMMENDED for better performance) boolean isSet = SAS_ROSET.setQuickProcessing(true, Dynamic_Key_ID, Static_Key_IDs_Array); // Decrypt the Encrypted byte array String encrypted_byte_data = "üģ´=}ôzĤûêĘÓğ¿Òüģ´=}¿Ò¹čIìsYãÞγe¹čIìôzĤûêĘÓğsYãÞγe"; String decrypted_byte_data = SAS_ROSET.rcsByteDecrypt(Dynamic_Key_ID, Static_Key_IDs_Array, encrypted_byte_data); Please note that the order of Static Key reference IDs in the Static_Key_IDs_Array is critical for successful decryption. Ensure the keys are provided in the same order they were used for encryption.