Open In App

CAST Algorithm in Cryptography

Last Updated : 07 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

  • They use large S-boxes, 8*32 rather than the 6*4 of DES.
  • They are designed for software implementation.
  • The CAST S-boxes use bent functions as their columns. 
  • S-boxes meet the avalanche criterion, which means that every bit of input and every bit of round key affect every bit of round output and complement any input bit has exactly a 50% chance of changing any given output bit.

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.

  • It is a Feistel Cipher with 64-bit blocks and 16 rounds.
  • The key size varies from 40 bits to 128 bits.
  • There are eight 8*32 S-boxes. Out of these eight boxes, four are used in key scheduling and rest four boxes are used in actual encryption.
  • Round keys are 37 bits.
  • The F function XORs the input with 32 bits of round key, breaks the result into bytes, and runs each byte through a different S-box to get four 32-bit results.
  • Those are combined nonlinearly using different combining functions in different rounds.
  • The output is given a rotation that is controlled by other 5 round key bits.

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.

  • It uses 128-bit blocks and supports key sizes of 128, 192, or 256 bits.
  • It is a variant of the Fiestel cipher using four 32-bit sub-blocks.
  • Each round takes one 32-bit block as input and alters one block.
  • 48 rounds are used.
  • The round function and the S-boxes are identical to CAST-128.

Benefits of the CAST Algorithm

  • The CAST algorithm is two times faster than the reduced version of the Rijndael algorithm.
  • The CAST algorithm is generally used for security i.e, for IP security.
  • CAST-128 is available world wide on a royalty-free basis for commercial and non-commercial uses.

Limitations of the CAST Algorithm

  • The CAST algorithm is not strong as compared to AES.

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:

Python




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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads