How RC5 encryption algorithm works

RC5 is a symmetric key algorithm for block encryption designed by Ron Rivest. It is suitable for both hardware and software implementations due to the following characteristics:

  • Simplicity: The algorithm only uses primitive computer operations, such as addition, subtraction, bitwise XOR, and circular shifts. This makes it easy to implement and analyze.

  • Flexibility: It allows a variable number of rounds and bit size of the key.

  • Low memory utilization: It can be implemented on devices with limited memory.

Parameterization

RC5 is a parameterized, word-oriented algorithm. This means it is a block cipher with a two-word input (plaintext) and a two-word output (ciphertext) block size. The parameters are detailed as follows:

Word size: ww

This is the word size in bits. RC5 has two ww bit blocks, so the input and output blocks are each 2w2w bits long. For example, if ww is 3232 bit, then the input block will be 6464 bit long.

Number of rounds: rr

This is the number of rounds and determines the trade-off between speed and security, where greater rounds imply higher security but lower speed. rr also influences the size tt of the expanded key table SS that is derived from the secret key. The formula for tt is as follows:

Number of bytes in secret key: bb

This is the number of 88-bit bytes in the user-provided secret key, KK, and has the allowable range from 02550-255 bytes.

The table below summarizes the parameters:

Parameters of the RC5 encyrption algorithm

Parameter

Explanation

Accepted values

w

Word size in bits.

16, 32, 64

r

Number of rounds.

0–255

b

Number of octets (8-bit bytes) in the secret key K.

0–255

Given these parameters, the notation for the algorithm is as follows:

            RC5w/r/b RC5-w/r/b

The nominal version of this is RC532/12/16RC5-32/12/16, which implies there are two 3232 bit word inputs and outputs, 1212 rounds, and a 1616 byte (128128 bit) key.

Primitive operations

RC5 uses only three primitive operations and their inverses. These are:

  • Addition: This refers to the two's complement addition of words and is denoted by ++. The inverse of this is subtraction, denoted by -.

  • Bitwise XOR: This is the bitwise exclusive-OR of words and is denoted by \oplus.

  • Left rotation: This is the cyclic left rotation of words, denoted by x<<<y x <<< y, where xx is the word and yy is the number of bits to be shifted. The inverse is cyclic right rotation, represented by x>>>yx>>>y.

The algorithm

Now that we have defined the parameters and operations, we can begin taking a look at the three components of the RC5 encryption algorithm:

  • Key expansion algorithm

  • Encryption

  • Decryption

The diagram below illustrates the order of these components:

The flow of the RC5 algorithm

Key expansion algorithm

Recall that RC5 uses an expanded key table, S[0...t1]S[0...t-1], derived from the provided secret key, KK. The key expansion algorithm expands KK by filling the array SS with t=2(r+1)t=2(r+1) binary words. It does this over four steps, as outlined below:

Step 1: Initialization of magic constants

RC5 uses two word-sized constants, PP and QQ. They are defined as follows:

Where e e is the base of natural logarithms and ϕ\phi is the golden ratio.

For w=16,32,w= 16, 32,and 6464, the values of P and Q are given in the table:

Values of P and Q for different word sizes

Word size

P (in hexadecimal)

Q (in hexadecimal)

16

b7e1

9e37

32

b7e15163

9e3779b9

64

b7e151628aed2a6b

9e3779b97f4a7c15

Step two: Converting the secret key KK from bytes to words

The secret key array, K[0...b1]K[0...b-1], is used to initialize a new array, L[0...c1]L[0...c-1], where c=b/uc=\lceil b/u \rceil and u=w/8u=w/8 bytes per word. Once LL is declared, the pseudocode for copying KK to it is as follows:

for i = b-1 to 0:
L[i/u] = (L[i/u] <<< 8) + K[i]

Step three: Initializing the array SS

This step requires the initialization of the array SS of size t=2(r+1)t=2(r+1) and is dependent on the magic constants, PP and QQ. The pseudocode is given below:

S[0] = P
for i = 0 to t-1:
S[i] = S[i-1] + Q

Step four: Secret key mixing

The last step of the key expansion algorithm involves mixing the provided secret key over the arrays SS and LL. The larger of these arrays will be processed three times, and the smaller may be processed more. Moreover, AA and BB are registers that store input values of size ww bit each.

The pseudocode for mixing is as follows:

i = 0
j = 0
do 3 * max(t, c) times:
A = S[i] = (S[i] + A + B) <<< 3
B = L[j] = (L[j] + A + B) <<< (A + B)
i = (i + 1) % t
j = (j + 1) % c

Encryption

Now that we have the expanded key in the array S[0...t1]S[0...t-1], we can perform the encryption algorithm as specified below. Note that registers AAand BB hold the plaintext divided into two ww bit blocks.

A = A + S[0]
B = B + S[1]
for i = 1 to r:
A = ((A ^ B) <<< B) + S[2 * i]
B = ((B ^ A) <<< A) + S[2 * i + 1]

Once rr rounds are over, AA and BB together form the output ciphertext.

Decryption

The pseudocode for the decryption algorithm is the reverse of the encryption algorithm, as shown:

for i = r to 1:
B = ((B - S[2 * i + 1]) >>> A) ^ A
A = ((A - S[2 * i]) >>> B) ^ B
B = B - S[1]
A = A - S[0]

This concludes the overview of the different steps in the RC5 encryption algorithm.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved