7. Encrypting & Decrypting Bytes with SAS-RBS by: saaiqSAS Last Updated: 25/2/2025 In this section, we will demonstrate how to encrypt and decrypt byte[] data using the SAS-RBS (Saaiq Abdulla Saeed's - Random Binary Substitution) encryption algorithm - Binary-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. Do note that the SAS-RBS strictly supports only key lengths that are powers of two (binary powers - 256, 1024, etc). Use the method keyLengthForBits(int bits) to determine the key length required for a specific bit encryption using SAS-RBS. It returns the corresponding key length as an int. SAS_ROSET.keyLengthForBits(int bits); Range for 'bits' is between 7 and 20, inclusive. Usage Examples: int length_256 = SAS_ROSET.keyLengthForBits(8); int length_1024 = SAS_ROSET.keyLengthForBits(10); int length_65536 = SAS_ROSET.keyLengthForBits(16); Moreover you can use the method keySupportsRBS(int Dynamic_Key_ID) to check if the length of a Dynamic Key support SAS-RBS. The methods returns true if supported and false if not. The byte[] provided for SAS-RBS encryption must have a strict length. This length can be determined using the method below, which returns the length as an integer. The strict length is necessary due to the data increase ratio. Providing an incorrect length may result in errors or cause data to be increased by more or less than the expected ratio. The strict length differs between encryption and decryption. For example, plain data for encryption may need to be provided as 10 bytes, but the encrypted output could be 25 bytes. Thus, when decrypting, you must provide 25 bytes. The output byte[] length can be directly calculated using the method below. Although byte[] data should generally follow strict lengths, the final segment of data (e.g., the last portion of a file) can be an exception. If the data length is smaller than the strict length, no error will occur when processing the entire data in one go. The method rbsNumberOfBytesToPass(int Dynamic_Key_ID, int maxByteBufferSize, boolean decryptMode) calculates the strict length for a given Dynamic Key. Be sure to set the decrypt mode properly. The maximum byte buffer size must also be provided, and after calculations, the closest possible strict length to the max buffer size will be returned. // Key Extraction int Dynamic_Key_ID = // extract Dynamic Key int maxByteBufferSize = 1024; // Plain byte[] data size int byteBufferSize_encrypting = SAS_ROSET.rbsNumberOfBytesToPass(Dynamic_Key_ID, maxByteBufferSize, false); // Encrypted byte[] data size int byteBufferSize_decrypting = SAS_ROSET.rbsNumberOfBytesToPass(Dynamic_Key_ID, maxByteBufferSize, true); Encrypt byte array To encrypt a byte[] using SAS-RBS, you can call the method rbsByteEncrypt(int Dynamic_Key_ID, int[] Static_Key_IDs_Array, byte[] data). This method will return the SAS-RBS encrypted data as byte[]. 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); // Plain byte[] buffer data size int maxByteBufferSize = 1024; int byteBufferSize_encrypting = SAS_ROSET.rbsNumberOfBytesToPass(Dynamic_Key_ID, maxByteBufferSize, false); // Encrypt the byte array byte[] byte_data = // a byte[] of length equal to 'byteBufferSize_encrypting'; byte[] encrypted_byte_data = SAS_ROSET.rbsByteEncrypt(Dynamic_Key_ID, Static_Key_IDs_Array, byte_data); Decrypt Encrypted byte array To decrypt a SAS-RBS encrypted byte[], use the method rbsByteDecrypt(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); // Encrypted byte[] buffer data size int maxByteBufferSize = 1024; int byteBufferSize_decrypting = SAS_ROSET.rbsNumberOfBytesToPass(Dynamic_Key_ID, maxByteBufferSize, true); // Decrypt the Encrypted byte array byte[] encrypted_byte_data = // a byte[] of length equal to 'byteBufferSize_decrypting'; byte[] decrypted_byte_data = SAS_ROSET.rbsByteDecrypt(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.