The Wayback Machine - https://web.archive.org/web/20240909215114/https://www.geeksforgeeks.org/rc5-encryption-algorithm/
Open In App

RC5 Encryption Algorithm

Last Updated : 30 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:

ParameterPossible Value
block/word size (bits)16, 32, 64
Number of Rounds0 – 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:

SymbolOperation
x <<< yCyclic left shift of x by y bits
+Two’s complement addition of words where addition is modulo [Tex]2^w[/Tex]
^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)
16b7e19e37
32b7e151639e3779b9
64b7e151628aed2a6b9e3779b97f4a7c15

For any other word size, P and Q can be determined as:

P = Odd((e-2)[Tex]2^w[/Tex]) Q = Odd(([Tex]\phi[/Tex]-2)[Tex]2^w[/Tex])

Here, Odd(x) is the odd integer nearest to x, e is the base of natural logarithms and [Tex]\phi&nbsp;&nbsp;[/Tex]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:

  1. 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 mod[Tex]2^w&nbsp;&nbsp;[/Tex].
  2. XOR A and B. A=A^B
  3. Cyclic left shift new value of A by B bits.
  4. Add S[2*i] to the output of previous step. This is the new value of A.
  5. XOR B with new value of A and store in B.
  6. Cyclic left shift new value of B by A bits.
  7. Add S[2*i+1] to the output of previous step. This is the new value of B.
  8. 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


Output

Cipher 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

Difference between Software Encryption and Hardware Encryption
1. Software Encryption: Software Encryption, as the name suggests, is a process of keeping data safe using the software. In this, the software is generally installed in the host computer that encrypts and decrypts data. It is more cost-effective for smaller companies. In this, the password is the key that one needs to have access to data. It usuall
5 min read
RC4 Encryption Algorithm
RC4 is a stream cipher and variable-length key algorithm. This algorithm encrypts one byte at a time (or larger units at a time). A key input is a pseudorandom bit generator that produces a stream 8-bit number that is unpredictable without knowledge of input key, The output of the generator is called key-stream, is combined one byte at a time with
5 min read
Simplified International Data Encryption Algorithm (IDEA)
Introduction : The International Data Encryption Algorithm (IDEA) is a symmetric-key block cipher that was first introduced in 1991. It was designed to provide secure encryption for digital data and is used in a variety of applications, such as secure communications, financial transactions, and electronic voting systems. IDEA uses a block cipher wi
8 min read
Twofish Encryption Algorithm
When it comes to data protection, encryption methods act as our buffering agents. One example of an excellent block cipher is the Twofish encryption algorithm. Although it was a competitor of another block cipher in the Advanced Encryption Standard competition and was later succeeded by the latter, it can still be used as a safe approach to protect
6 min read
Knapsack Encryption Algorithm in Cryptography
Knapsack Encryption Algorithm is the first general public key cryptography algorithm. It was developed by Ralph Merkle and Mertin Hellman in 1978. As it is a Public key cryptography, it needs two different keys. One is the Public key which is used for the Encryption process and the other one is the Private key which is used for the Decryption proce
6 min read
ElGamal Encryption Algorithm
ElGamal Encryption is a public-key cryptosystem. It uses asymmetric key encryption for communicating between two parties and encrypting the message. This cryptosystem is based on the difficulty of finding discrete logarithms in a cyclic group that is even if we know ga and gk, it is extremely difficult to compute gak. In this article, we will get t
6 min read
Encryption vs Encoding vs Hashing
Pre-Requisite: Encryption, Encoding, Hashing. Encryption, Encoding, and Hahsing are similar kinds of things and have little difference between them. They all are used to change the format of the data or data transformation for different purposes. We will discuss them separately. Let us first discuss the definition of all these three processes and t
4 min read
Decrypt the encoded string with help of Matrix as per given encryption decryption technique
Given an encoded (or encrypted) string S of length N, an integer M. The task is to decrypt the encrypted string and print it. The encryption and decryption techniques are given as: Encryption: The original string is placed in a Matrix of M rows and N/M columns, such that the first character of the Original text or string is placed on the top-left c
6 min read
XOR Encryption by Shifting Plaintext
Here is a cipher algorithm, based on hexadecimal strings that is implemented by XORing the given plaintext, N number of times where N is its length. But, the catch is that every next XOR operation is done after shifting the consecutive plain text entry to the right. A sample operation is shown below : Suppose the password is 'abcd' then the hexadec
6 min read
Image encryption using cellular automata
To propose an image encryption system using one dimensional cellular automata for image encryption and decryption. Cellular automata can be corresponded with the essential cryptographic properties i.e. Balance, correlation-immune, nonlinearity, easy to implement in hardware. CA cryptosystems can give better performances compared to classic methods
4 min read
End to End Encryption (E2EE) in Computer Networks
What is Encryption? Have you ever wondered how our emails, texts, photos, videos are sent over the Internet? Whenever you send anything over the Internet, it passes through a lot of servers, routers, and other devices where anyone can intercept it and get access to your private information. Thus, to tackle such mishap, encryption was introduced. In
2 min read
Find the string among given strings represented using given encryption pattern
Given an array of strings arr[] of size N and an encrypted string str, the task is to find the correct string from the given array of strings whose encryption will give str where str is encrypted using the following rules: The starting characters form an integer representing the number of uppercase symbols In the decrypted string.The next 3 charact
8 min read
Strength of Data encryption standard (DES)
Data encryption standard (DES) is a symmetric key block cipher algorithm. The algorithm is based on Feistel network. The algorithm uses a 56-bit key to encrypt data in 64-bit blocks. There are mainly two categories of concerns about the strength of Data encryption standard. They are: Concerns about the particular algorithm used. Concerns about the
2 min read
Encryption and Decryption of String according to given technique
Given a string S, the task is to encrypt the string and decrypt the string again to the original form. Encryption Technique: If L is the length of the string, then take two values, one the ceil of ?L (say b), and the other floor of ?L (say a), and make a two-dimensional matrix having rows = a, and columns = b. If rows*columns &lt; L, then increase
10 min read
What is E2EE(End to End Encryption)?
E2EE or End to End Encryption refers to the process in which encryption of data are being done at the end host. It is an implementation of Asymmetric encryption and hence ensures a secure way of data communication. It is the most secure way to communicate privately and securely as data can be read-only by the sender and the receiver. No one else ca
3 min read
Conventional Encryption
Conventional encryption is a cryptographic system that uses the same key used by the sender to encrypt the message and by the receiver to decrypt the message. It was the only type of encryption in use prior to the development of public-key encryption. It is still much preferred of the two types of encryption systems due to its simplicity. It is a r
3 min read
Simplified Data Encryption Standard Key Generation
Simplified Data Encryption Standard (S-DES) is a simple version of the DES Algorithm. It is similar to the DES algorithm but is a smaller algorithm and has fewer parameters than DES. It was made for educational purposes so that understanding DES would become simpler. It is a block cipher that takes a block of plain text and converts it into ciphert
3 min read
Simplified Data Encryption Standard | Set 2
Prerequisite - Simplified Data Encryption Standard | Set 1 Simplified Data Encryption Standard is a simple version of Data Encryption Standard having a 10-bit key and 8-bit plain text. It is much smaller than the DES algorithm as it takes only 8-bit plain text whereas DES takes 64-bit plain text. It was developed for educational purpose so that und
13 min read
Difference Between Data Encryption and Data Compression
Data compression and data encryption are two important methods of protecting and managing data. While both of these processes can help protect your data, they work in different ways. In this article, we’ll discuss the key differences between data encryption and data compression, and explain how each can help you protect your data. What is Data Encr
4 min read
Rail Fence Cipher - Encryption and Decryption
Given a plain-text message and a numeric key, cipher/de-cipher the given text using Rail Fence algorithm. The rail fence cipher (also called a zigzag cipher) is a form of transposition cipher. It derives its name from the way in which it is encoded. Examples:  EncryptionInput : "GeeksforGeeks "Key = 3Output : GsGsekfrek eoeDecryptionInput : GsGsekf
15 min read
Encryption, Its Algorithms And Its Future
Encryption is very important in today's world, it is the process of transforming plaintext into ciphertext to maintain data security, and it is a crucial tool used in modern cyber security. It secures sensitive data by rendering it unreadable to unauthorized events, making sure of confidentiality, integrity, and authenticity. What is Encryption?Enc
6 min read
What is Data Encryption?
Data Encryption is an important part of preserving data integrity, and confidentiality, its importance cannot be overestimated. Almost the whole thing on the internet has been encrypted at some point. In this article, we will discuss data encryption and its importance. What is Data Encryption?Data Encryption is a method of preserving data confident
9 min read
Why is Encryption Important in Securing Data Transmission Over Networks?
Answer: Encryption is basically important for securing data transmission over Networks, because it makes shows that sensitive information remains private and inaccessible to unauthorized users.Below are the reasons specifying why encryption important in securing data transmission over networks: ConfidentialityEncryption process encodes the data int
2 min read
Encryption vs Hashing vs Salting
Data Integrity and Privacy form the building blocks of cybersecurity. These three core techniques- encryption, hashing, and salting, are considered to be the most important ones when it comes to the security of digital data. This is done to protect data during the process of storage and transmission, but they altogether play different roles and use
5 min read
How Does Wi-Fi Encryption Enhance Network Security?
Answer: Wireless security consists of encryption, authentication, access control, device security, access control, etc. It creates a layer of protection. The process starts by enabling the wireless network encryption method (such as WPA2 or WPA3) to block data transmission. Unauthorized people cannot read the data even if they are intercepted. Acce
2 min read
Difference Between Symmetric and Asymmetric Key Encryption
Symmetric Key Encryption: Encryption is a process to change the form of any message in order to protect it from reading by anyone. In Symmetric-key encryption the message is encrypted by using a key and the same key is used to decrypt the message which makes it easy to use but less secure. It also requires a safe method to transfer the key from one
2 min read
Public Key Encryption
Public key cryptography provides a secure way to exchange information and authenticate users by using pairs of keys. The public key is used for encryption and signature verification, while the private key is used for decryption and signing. When the two parties communicate with each other to transfer the intelligible or sensible message, referred t
8 min read
Encryption vs Digest in Cryptography
Encryption and Digest algorithms are used prominently in cryptography to protect the information which is always in high demand. Both are used as protection for data, however, their roles and capabilities of use are quite varied. Encryption replaces the normal or readable form of information (plaintext) with an unreadable form (ciphertext) to maint
6 min read
Advanced Encryption Standard (AES)
Advanced Encryption Standard (AES) is a specification for the encryption of electronic data established by the U.S. National Institute of Standards and Technology (NIST) in 2001. AES is widely used today as it is much stronger than DES and triple DES despite being harder to implement. In this article, we will cover the AES, the Working of Cipher an
8 min read
What is an Asymmetric Encryption?
Asymmetric encryption, also known as public-key cryptography, is a type of encryption that uses a pair of keys to encrypt and decrypt data. The pair of keys includes a public key, which can be shared with anyone, and a private key, which is kept secret by the owner. What is an Asymmetric Encryption?In asymmetric encryption, the sender uses the reci
8 min read
Practice Tags :