RC5 is a symmetric key block encryption algorithm designed by Ron Rivest in 1994. It is notable for being simple, fast (on account of using only primitive computer operations like XOR, shift, etc.) and consumes less memory. Example:
Key : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Plain Text : 00000000 00000000
Cipher Text : EEDBA521 6D8F4B15
RC5 is a block cipher and addresses two word blocks at a time. Depending on input plain text block size, number of rounds and key size, various instances of RC5 can be defined and each instance is denoted as RC5-w/r/b where w=word size in bits, r=number of rounds and b=key size in bytes. Allowed values are:
Parameter | Possible Value |
---|
block/word size (bits) | 16, 32, 64 |
Number of Rounds | 0 - 255 |
Key Size (bytes) | 0 - 255 |
Note - Since at a time, RC5 uses 2 word blocks, the plain text block size can be 32, 64 or 128 bits. Notation used in the algorithm:
Symbol | Operation |
---|
x <<< y | Cyclic left shift of x by y bits |
+ | Two's complement addition of words where addition is modulo 2^w |
^ | Bit wise Exclusive-OR |
Step-1: Initialization of constants P and Q. RC5 makes use of 2 magic constants P and Q whose value is defined by the word size w.
Word Size (bits) | P (Hexadecimal) | Q (Hexadecimal) |
---|
16 | b7e1 | 9e37 |
32 | b7e15163 | 9e3779b9 |
64 | b7e151628aed2a6b | 9e3779b97f4a7c15 |
For any other word size, P and Q can be determined as:
P = Odd((e-2)2^w) Q = Odd((\phi-2)2^w)
Here, Odd(x) is the odd integer nearest to x, e is the base of natural logarithms and \phi is the golden ratio. Step-2: Converting secret key K from bytes to words. Secret key K of size b bytes is used to initialize array L consisting of c words where c = b/u, u = w/8 and w = word size used for that particular instance of RC5. For example, if we choose w=32 bits and Key k is of size 96 bytes then, u=32/8=4, c=b/u=96/4=24. L is pre initialized to 0 value before adding secret key K to it.
for i=b-1 to 0
L[i/u] = (L[u/i] <<< 8) + K[i]
Step-3: Initializing sub-key S. Sub-key S of size t=2(r+1) is initialized using magic constants P and Q.
S[0] = P
for i = 1 to 2(r+1)-1
S[i] = S[i-1] + Q)
Step-4: Sub-key mixing. The RC5 encryption algorithm uses Sub key S. L is merely, a temporary array formed on the basis of user entered secret key. Mix in user's secret key with S and L.
i = j = 0
A = B = 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
Step-5: Encryption. We divide the input plain text block into two registers A and B each of size w bits. After undergoing the encryption process the result of A and B together forms the cipher text block. RC5 Encryption Algorithm:
- One time initialization of plain text blocks A and B by adding S[0] and S[1] to A and B respectively. These operations are mod2^w .
- XOR A and B. A=A^B
- Cyclic left shift new value of A by B bits.
- Add S[2*i] to the output of previous step. This is the new value of A.
- XOR B with new value of A and store in B.
- Cyclic left shift new value of B by A bits.
- Add S[2*i+1] to the output of previous step. This is the new value of B.
- Repeat entire procedure (except one time initialization) r times.
A = A + S[0]
B = B + S[1]
for i = 1 to r do:
A = ((A ^ B) <<< B) + S[2 * i]
B = ((B ^ A) <<< A) + S[2 * i + 1]
return A, B
Alternatively, RC5 Decryption can be defined as:
for i = r down to 1 do:
B = ((B - S[2 * i + 1]) >>> A) ^ A
A = ((A - S[2 * i]) >>> B) ^ B
B = B - S[1]
A = A - S[0]
return A, B
Below is the implementation of above approach:
C++
#include <cstdint>
#include <iostream>
#include <vector>
// Constants for RC5-32/12/16
const int W = 32; // Word size in bits
const int R = 12; // Number of rounds
const int B = 16; // Key size in bytes
const int C = 4; // Number of words in key
// Magic constants
const uint32_t P = 0xB7E15163;
const uint32_t Q = 0x9E3779B9;
// Rotate left function
uint32_t rotl(uint32_t x, int y)
{
return (x << y) | (x >> (W - y));
}
// RC5 key expansion
void rc5_key_setup(const std::vector<uint8_t>& key,
std::vector<uint32_t>& S)
{
std::vector<uint32_t> L(C, 0);
for (int i = B - 1; i >= 0; --i) {
L[i / 4] = (L[i / 4] << 8) + key[i];
}
S[0] = P;
for (int i = 1; i < 2 * (R + 1); ++i) {
S[i] = S[i - 1] + Q;
}
uint32_t A = 0, B = 0;
int i = 0, j = 0;
for (int k = 0; k < 3 * std::max(2 * (R + 1), C); ++k) {
A = S[i] = rotl(S[i] + A + B, 3);
B = L[j] = rotl(L[j] + A + B, A + B);
i = (i + 1) % (2 * (R + 1));
j = (j + 1) % C;
}
}
// RC5 encryption
void rc5_encrypt(const std::vector<uint32_t>& S,
uint32_t& A, uint32_t& B)
{
A = (A + S[0])
& ((1 << W) - 1); // Adjusted modulo operation
B = (B + S[1])
& ((1 << W) - 1); // Adjusted modulo operation
for (int i = 1; i <= R; ++i) {
A = (rotl(A ^ B, B) + S[2 * i])
& ((1 << W) - 1); // Adjusted modulo operation
B = (rotl(B ^ A, A) + S[2 * i + 1])
& ((1 << W) - 1); // Adjusted modulo operation
}
}
int main()
{
std::vector<uint8_t> key = { 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00 };
std::vector<uint32_t> S(2 * (R + 1), 0);
rc5_key_setup(key, S);
uint32_t A = 0x00000000;
uint32_t B = 0x00000000;
rc5_encrypt(S, A, B);
std::cout << "Cipher Text: " << std::hex << A << " "
<< B << std::endl;
return 0;
}
Java
import java.util.ArrayList;
import java.util.List;
public class RC5 {
// Constants for RC5-32/12/16
private static final int W = 32; // Word size in bits
private static final int R = 12; // Number of rounds
private static final int B = 16; // Key size in bytes
private static final int C
= 4; // Number of words in key
// Magic constants
private static final int P = 0xB7E15163;
private static final int Q = 0x9E3779B9;
// Rotate left function
private static int rotl(int x, int y)
{
return (x << y) | (x >>> (W - y));
}
// RC5 key expansion
private static void rc5KeySetup(byte[] key, int[] S)
{
int[] L = new int[C];
for (int i = B - 1; i >= 0; --i) {
L[i / 4] = (L[i / 4] << 8) + (key[i] & 0xFF);
}
S[0] = P;
for (int i = 1; i < 2 * (R + 1); ++i) {
S[i] = S[i - 1] + Q;
}
int A = 0, B = 0;
int i = 0, j = 0;
for (int k = 0; k < 3 * Math.max(2 * (R + 1), C);
++k) {
A = S[i] = rotl(S[i] + A + B, 3);
B = L[j] = rotl(L[j] + A + B, (A + B) % W);
i = (i + 1) % (2 * (R + 1));
j = (j + 1) % C;
}
}
// RC5 encryption
private static void rc5Encrypt(int[] S, int[] data)
{
int A = data[0];
int B = data[1];
A = (A + S[0]);
B = (B + S[1]);
for (int i = 1; i <= R; ++i) {
A = rotl(A ^ B, B) + S[2 * i];
B = rotl(B ^ A, A) + S[2 * i + 1];
}
data[0] = A;
data[1] = B;
}
public static void main(String[] args)
{
byte[] key = new byte[B];
int[] S = new int[2 * (R + 1)];
rc5KeySetup(key, S);
int[] data = { 0x00000000, 0x00000000 };
rc5Encrypt(S, data);
// Convert the integer values to lowercase hex
// strings and format the output
System.out.printf("Cipher Text: %08x %08x%n",
data[0], data[1]);
}
}
// This code is contributed by Shivam Gupta
OutputCipher Text: eedba521 6d8f4b15
Advantages:
High level of security: RC5 is designed to provide a high level of security against attacks, including brute-force attacks and differential cryptanalysis. It uses a variable-length key and can operate on block sizes of up to 256 bits, making it difficult for attackers to break the encryption.
Fast encryption and decryption: RC5 is known for its fast encryption and decryption speeds. It uses simple mathematical operations such as modular arithmetic and bit shifting, which can be efficiently implemented on modern CPUs and hardware.
Flexible key length: RC5 allows for a variable-length key, which can range from 0 to 2040 bits. This flexibility allows users to choose a key length that suits their security needs and resources.
Disadvantages:
Vulnerable to side-channel attacks: RC5 is vulnerable to side-channel attacks, such as timing attacks and power analysis attacks. These attacks exploit information leaked through the implementation of the algorithm, rather than attacking the algorithm itself.
Limited adoption: RC5 is not widely adopted in practice compared to other encryption algorithms, such as AES. This means that there may be fewer resources and tools available to support RC5 in various applications and systems.
Patent issues: RC5 was subject to a patent held by RSA Security, which limited its adoption and use in commercial applications. Although the patent has since expired, it may have contributed to the limited adoption of RC5 compared to other encryption algorithms
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
What is OSI Model? - Layers of OSI Model The OSI (Open Systems Interconnection) Model is a set of rules that explains how different computer systems communicate over a network. OSI Model was developed by the International Organization for Standardization (ISO). The OSI Model consists of 7 layers and each layer has specific functions and re
13 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example
12 min read