Open In App

CAST Algorithm in Cryptography

CAST is a general procedure for creating a family of block ciphers. Individual ciphers have names like CAST-128 and CAST-256. The article focuses on discussing the CAST algorithm in cryptography.

What is CAST Algorithm?

The CAST algorithm is generally used in IP security and follows the Fiestal structure, as the Fiestal structure divides the plain text into equal halves and does the encryption process.



CAST-128

CAST-128 also known as CAST5 is the most widely used CAST Cipher. It is a symmetric key block cipher that is used in a number of products as the default cipher in some versions of GPG and PGF.

CAST-256  

CAST-256 also known as CAST6 was published in June 1998. It was a candidate cipher in the AES competition and it did not make it to the finals.



Benefits of the CAST Algorithm

Limitations of the CAST Algorithm

Example:

The CAST (Carlisle Adams and Stafford Tavares) algorithm is a symmetric key block cipher that belongs to the family of Feistel ciphers. In this example, we will use the ‘pycryptodome' library, which provides the implementation of the CAST-128 algorithm.

First, make sure you have the ‘pycryptodome' library installed:

pip install pycryptodome

Now, let’s proceed with the example code:




from Crypto.Cipher import CAST
from Crypto.Random import get_random_bytes
 
def cast_encrypt(key, data):
    cipher = CAST.new(key, CAST.MODE_ECB)
    encrypted_data = cipher.encrypt(data)
    return encrypted_data
 
def cast_decrypt(key, encrypted_data):
    cipher = CAST.new(key, CAST.MODE_ECB)
    decrypted_data = cipher.decrypt(encrypted_data)
    return decrypted_data
 
# Example usage
if __name__ == "__main__":
    # Generate a 16-byte random key (128-bit key)
    key = get_random_bytes(16)
 
    # Data to be encrypted
    original_data = b'This is the original data to be encrypted using CAST-128.'
 
    # Encrypt the data using the CAST-128 algorithm
    encrypted_data = cast_encrypt(key, original_data)
 
    # Decrypt the data using the same key
    decrypted_data = cast_decrypt(key, encrypted_data)
 
    print("Original Data:", original_data)
    print("Encrypted Data:", encrypted_data)
    print("Decrypted Data:", decrypted_data)

Explanation:

  1. Import the required modules: We import the ‘CAST' cipher from ‘Crypto.Cipher' and ‘get_random_bytes' from ‘Crypto.Random'. The CAST cipher is the implementation of the CAST-128 algorithm, and ‘get_random_bytes' is used to generate a random key.
  2. Define encryption and decryption functions: We define two functions, ‘cast_encrypt()' and ‘cast_decrypt()', which take the key and data as input and return the encrypted and decrypted data, respectively.
  3. Example usage: In the example usage section, the following steps are performed:
    • A 128-bit random key is generated using ‘get_random_bytes()'.
    • A sample data ‘original_data' is created, which represents the data we want to encrypt.
    • The data is encrypted using the ‘cast_encrypt()' function, and the result is stored in ‘encrypted_data'.
    • The encrypted data is decrypted using the ‘cast_decrypt()' function, and the result is stored in ‘decrypted_data'.
    • Finally, the original data, encrypted data, and decrypted data are printed to demonstrate the process.

Output :

Original Data: b'This is the original data to be encrypted using CAST-128.'
Encrypted Data: b'\xe8\x18~j8\xed\xe0w\xfa\x92\xfe\x06h\x0f\xa8\x0fN\x0e\x17\xf1\x0b\xd8\x15\x9d\xf3\x86\xa3K\xc3)\x87\xb7'
Decrypted Data: b'This is the original data to be encrypted using CAST-128.'

The output demonstrates that the data was successfully encrypted and decrypted using the CAST-128 algorithm with the same key. The original data and the decrypted data match, confirming the correctness of the encryption and decryption process.


Article Tags :