0% found this document useful (0 votes)
186 views8 pages

Encryption and Decryption of Rc4

The document describes an implementation of the RC4 encryption and decryption algorithms using Python, including defining functions for key scheduling, stream generation, encryption, and decryption. It also provides sample encryptions and decryptions to demonstrate the implementation and discusses weaknesses in the RC4 algorithm that make it no longer considered secure. The project was completed by three students and involved explaining the RC4 theory, code implementation, and results from testing the encryption and decryption functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
186 views8 pages

Encryption and Decryption of Rc4

The document describes an implementation of the RC4 encryption and decryption algorithms using Python, including defining functions for key scheduling, stream generation, encryption, and decryption. It also provides sample encryptions and decryptions to demonstrate the implementation and discusses weaknesses in the RC4 algorithm that make it no longer considered secure. The project was completed by three students and involved explaining the RC4 theory, code implementation, and results from testing the encryption and decryption functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Laboratory Report Cover Sheet

SRM Institute of Science and Technology


College of Engineering and Technology
Department of Electronics and Communication Engineering
18ECC303J Computer Communication and Networks
Sixth Semester, 2022-23 (Even semester)

Experiment based Project


On
Development and Implementation of RC4 cryptography Algorithm

Topic Mark RA2011004010073 RA2011004010076 RA2011004010084


RECHARLA PERALA KASA SRI
SHREYA MANOJ DURGA SATYA VIJAY
VENKTA
Proposal 5
Submission
Demonstration 10
Presentation 10
Document 10
Preparation
Viva 5
Total 40

REPORT VERIFICATION

Date :

Staff Name : Dr. T. Rama Rao

Signature :
Title of the project:
Implementation of RC4 Encryption and Decryption Algorithms Using Python.

Introduction:
The RC4 algorithm is a symmetric stream cipher used for encryption and decryption of data. It was
developed by Ron Rivest in 1987 and was initially usedfor secure communication in the Internet
Engineering Task Force (IETF). The algorithm is known for its simplicity and speed and is widely
used in various applications.

Software used:
Python 3.8, Pycharm IDE

RC4 algorithm Theory/Explanation:


RC4 (Rivest Cipher 4) is a symmetric stream cipher algorithm that is widely used in various
applications, such as secure socket layer (SSL), wireless network encryption, and Bluetooth. It operates
on bytes of data and generates astream of pseudo-random bytes, which are XOR-ed with the plaintext
to produce the ciphertext. RC4 encryption and decryption is achieved by using a secret key of arbitrary
length between 40 and 2048 bits.
The RC4 algorithm consists of two main stages: key-scheduling and stream generation. In the
key-scheduling stage, the algorithm creates a permutation of the 256-byte array, based on the secret key
and an initialization vector (IV). Thepermutation is achieved by performing a series of swaps between
elements of the array, depending on the key and IV. In the stream generation stage, the algorithm
generates a pseudo-random stream of bytes by repeatedly swappingelements of the array and generating
a byte from the array index, based on thecurrent state of the algorithm. This stream is XOR-ed with the
plaintext to produce the ciphertext, and vice versa for decryption.
Overall, RC4 is a simple, fast, and widely-used stream cipher that provides a good level of
security for various applications. However, it is vulnerable to certain attacks, such as key recovery
attacks and related key attacks, and therefore should not be used as the sole security mechanism for
critical applications.
To encrypt a plaintext message using RC4, the pseudorandom stream is XORed with the
plaintext to produce the ciphertext. To decrypt the ciphertext, the same pseudorandom stream is XORed
with the ciphertext to recover the original plaintext.
Implementation Methodology or steps:

RC4 algorithm can be implemented using Python by following these steps:


1. Define a function to perform the key-scheduling stage, which takes thesecret key as input
and generates the permutation of the 256-byte array.

2. Define a function to generate the pseudo-random stream of bytes, which takes the
permutation array as input and generates a stream of bytes toXOR with the plaintext.
3. Define a function to perform the encryption, which takes the plaintext andsecret key as input,
and generates the ciphertext by XOR-ing the plaintext with the stream of bytes generated in
step2
4. Define a function to perform the decryption, which takes the ciphertext andsecret key as input,
and generates the plaintext by XOR-ing the ciphertext with the same stream of bytes generated in
step 2.
5. Test the encryption and decryption functions with sample plaintext and secret key, and
verify that the output matches the expected ciphertext andplaintext, respectively.

Programme:
def key_scheduling(key):
sched = [i for i in range(0, 256)]
i = 0
for j in range(0, 256):
i = (i + sched[j] + key[j % len(key)]) % 256
tmp = sched[j]
sched[j] = sched[i]
sched[i] = tmp
return sched
def stream_generation(sched):
stream = []
i = 0
j = 0
while True:
i = (1 + i) % 256
j = (sched[i] + j) % 256
tmp = sched[j]
sched[j] = sched[i]
sched[i] = tmp
yield sched[(sched[i] + sched[j]) % 256]
def encrypt(text, key):
text = [ord(char) for char in text]
key = [ord(char) for char in key]
sched = key_scheduling(key)
key_stream = stream_generation(sched)
ciphertext = ''
for char in text:
enc = str(hex(char ^ next(key_stream))).upper()
ciphertext += (enc)
return ciphertext
def decrypt(ciphertext, key):
ciphertext = ciphertext.split('0X')[1:]
ciphertext = [int('0x' + c.lower(), 0) for c in ciphertext]
key = [ord(char) for char in key]
sched = key_scheduling(key)
key_stream = stream_generation(sched)
plaintext = ''
for char in ciphertext:
dec = str(chr(char ^ next(key_stream)))
plaintext += dec
return plaintext
if __name__ == '__main__':
ed = input('Enter 1 for Encrypt, or 2 for Decrypt: ').upper()
if ed == '1':
plaintext = input('Enter your plaintext: ')
key = input('Enter your secret key: ')
result = encrypt(plaintext, key)
print('Cipher Text: ')
print(result)
elif ed == '2':
ciphertext = input('Enter your ciphertext: ')
key = input('Enter your secret key: ')
result = decrypt(ciphertext, key)
print('Cipher Text: ')
print(result)
else:
print('Error in input - try again.')

Demonstration Result and Screenshots:


Tabulation for Encryption:
S.No Plain text Cipher Text Keyword
1 SRMECE 0X9D0X760X130XD60XC0XEB ABC
2 Jersey 0X840X410X2C0XE00X2A0XD7 ABC
3 abc 0XAF0X460X3D ABC
4 1234 0X1A0XAF0X3D0XC9 CCN
5 A1B2C3 0X6A0XAC0X4C0XCF0X270X5A CCN

Tabulation for Decryption:


S.No Cipher text Plain Text Keyword
1 0X9D0X760X130XD60XC0XEB SRMECE ABC
2 0X840X410X2C0XE00X2A0XD7 Jersey ABC
3 0XAF0X460X3D Abc ABC
4 0X1A0XAF0X3D0XC9 1234 CCN
5 0X6A0XAC0X4C0XCF0X270X5A A1B2C3 CCN

Discussion of Results:
RC4 is a stream cipher that uses a variable-length key to generate a pseudorandom stream of bits,
which is then XORed with the plaintext to produce the ciphertext. RC4 has been widely used in many
applications, including wireless networks, SSL/TLS, and WEP/WPA encryption.
In recent years, several weaknesses have been discovered in RC4, which make it vulnerable to attacks.
One of the most significant weaknesses is the RC4 bias, which allows an attacker to predict a significant
number of the keystream bytes generated by RC4 with a high probability. This bias can be exploited to
recover plaintext from ciphertext or to mount a variety of attacks, including the well-known BEAST attack
on SSL/TLS.
Furthermore, RC4 is also vulnerable to key recovery attacks, which allow an attacker to recover the
secret key used to encrypt the data. These attacks are based on analyzing the RC4 output and exploiting
statistical weaknesses in the key scheduling algorithm.
Due to these weaknesses, it is no longer recommended to use RC4 for encryption or decryption.
Instead, modern ciphers such as AES (Advanced Encryption Standard) should be used. AES is a block cipher
that has been extensively studied and is considered secure for most applications.
In conclusion, RC4 is no longer considered a secure encryption algorithm due to its vulnerabilities to
attacks. It is recommended to use modern ciphers such as AES instead.

Conclusion:
The RC4 algorithm is a widely used stream cipher for encryption and decryption of data. It is known for its simplicity
and speed. In this report, we have explainedthe theory behind the RC4 algorithm, its implementation using Python
and the software used. And the functions that provides a convenient and easy-to-use interface for implementing the
RC4 algorithm in Python.
Name of the members of the Group:
RECHARLA SHREYA - RA2011004010073
PERALA MANOJ DURGA VENKAT – RA2011004010076
KASA SRI SATYA VIJAY – RA2011004010084

Reference List:
1. Data communications and networking I Behrouz A Forouzan
2. https://www.youtube.com/watch?v=Pl-ySf5abv8

You might also like