0 ratings 0% found this document useful (0 votes) 16 views 8 pages Miniproject 01-20-38
This report details the implementation of the RC4 cryptography algorithm using Python, highlighting its symmetric stream cipher characteristics and usage in secure communications. It outlines the algorithm's key-scheduling and stream generation stages, as well as the encryption and decryption processes. Despite its simplicity and speed, the report warns against using RC4 for critical applications due to its vulnerabilities and recommends modern encryption methods instead.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Go to previous items Go to next items
Save Miniproject 01-20-38 For Later Mini Project Report Cover Sheet
SRM Institute of Science and Technology
College of Engineering and Technology
Department ese bh | Communication Engine
Experiment based Project
On
Implementation of RC4 Cryptography Algorithm
Rishabh Ghelani Nihaal Teenu C Abhinav Mishra
i RA211100401000 | RA211100401002 | RA211100401003
Topic | Mar i 6 8
k
Demonstratio | 15 ee \ 4 -
n
Document | 15 I 1s lon
Preparation
2 ie
Viva 10 ee 12
Total 40 ho 4e
e BC
REPORT VERIFICATION
Date aslelas
Staff Name Or. R-kumar / Dy. T- Ramu a
Signature11.1 AIM
Implementation of RC4 Encryption and Decryption Algorithms Using Python.
11.2 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 used
for secure communication in the Intemet Engineering Task Force (IETF). The
algorithm is known for its simplicity and speed and is widely used in various
applications
11.3 SOFTWARE
Python 3.8, Pycharm IDE
11.4RC4 ALGORITHM
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 a
stream 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). The
permutation 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 swapping
elements of the array and generating a byte from the array index, based on the
current state of the algorithm, This stream is XOR-ed with the plaintext toproduce the ciphertext, and vice versa for decryption.
Overall, RC4 is a simple, fast, and widely-used stream cipher that provides a
g00d 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.
RCABLOCK DIAGRAM
no
Encrypted
Tat
ed
11.5 IMPLEMENTATION METHODOLOGY
RC4 algorithm can be implemented using Python by following these stey
1. Define a function to perform the key-scheduling stage, which takes the
secret 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 to XOR with
the plaintext.
3. Define a function to perform the encryption, which takes the plaintext
and secret key as input, and generates the ciphertext by XOR-ing the plaintext
with the stream of bytes generated in step 2,
4, Define a function to perform the decryption, which takes the ciphertextand secret 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 and
plaintext, respectively.
11.6 RESULT AND DISCUSSION
This Python script implements the RC4 stream cipher, comprising functions for key
scheduling, key padding, stream generation, encryption, and decryption. The key scheduling
function initializes the state array using the provided key, while the padding key function
ensures the key is of the required length. The stream generation function produces a
keystream based on the state array. Encryption XORs plaintext characters with keystream
values, handling alphabetic and numeric characters differently. Decryption reverses the
encryption process. The user interface prompts the user to choose between encryption and.
decryption, then collects input text and keys accordingly, utilizing the appropriate functions
to perform the selected operation and outputting the result. However, RC4 is deprecated due
to its vulnerability to attacks, and it's advised to use modem encryption algorithms for secure
communication.
eT
‘ext= input("enter a plaintext: “)
Keyrinput("Enter the key: ")
cnerypeed ext = encrypt (texts Key)
printc'cipher text ist", encrypted text) |
euited =e 22 |
TExr =. input( “Enter cipher text: “) |
wer = input renter key! *) |
decrypted. text = decrypt (TEXT, KEY) |
princcbecrypted messages", decrypted text)
else!
print(‘Eeror in input ~ try again.")
Enter 1 for Enerypt, or 2 for Decrypt: 2
Enter a plaintext: Abhinav
Enter the key: COW
Cipher text {21 Heleubg,
Tabulation for Encryption
S.No_| Plain text Keyword _| Cipher Text
[1 ‘Abhinav ‘CCN Heloub
2 Nihaal ‘CCN. Ujluhm
3 Rishabh CCN Yjwbhes
4 Computer ‘CCN Ipaibupd
5 Networks ‘CCN UfxqvsveTabulation for Decryption
S.No Cipher Text Keyword Plain Text
[1 Heleubg CCN Abhinav
2___| Ujluhm CCN Nihaal
3 Yjwohes CCN Rishabh
4 Ipgibupd CCN Computer
[5 Ufxqvsve CCN Networks
11.7 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 explained
the theory behind the RC4 algorithm, its implementation using Python and the
software used, And the funetions that provides a convenient and easy-to-use
interface for implementing the RC4 algorithm in Python.
11.8 REFERENCES
1. Data communications and networking I Behrouz A Forouzan
2. https://www youtube.com/watch?v=Pl-ySfSabv8
APPENDIX:
Programme:
import random
def Key_Scheduling(key):
key length = len(key)
ifkey length > 256:
raise ValueBrror("Key too long (max length = 256)")
$= list(range(256))j=0
for iin range(256):
j= (+ S[i] + key[i % key_length]) % 256
S[i], SEi] = SG), Sti)
retum S
def pad_key(key):
padded_key = bytearray(256)
key_len=len(key)
if key_len> 256:
padded_keyfkey_len:
return padded_key
def stream_generation(S):
i=0
j=0
while True:
i=(1+1) % 256
j= G+ Sli) % 256
_ Sli], Si]= SU) Sielif c.isupper(:
©= chr((ord(c) + Next(Keystream) - 65) % 26 + 65)
elif c.isnumerie():
= str((int(c) + next(keystream)) % 10)
encrypted _text += ¢
return encrypted_text
def decrypt(ciphertext, key):
key =pad_key(key)
S=Key_Scheduling(key)
keystream = stream_generation(S)
decrypted_text ="
for c in ciphertext:
if c.isalpha():
if c.islower):
c= chr((ord(c) - next(keystream) - 97) % 26 + 97)
elif c.isupper():
c= chr((ord(c) - next(keystream) - 65) % 26 + 65)
elif c.isnumeric():
¢ = str((int(c) - next(keystream)) % 10)
decrypted_text +=
return decrypted_text
ed = input(‘Enter 1 for Encrypt, or 2 for Decrypt: ‘.upper()
ifed="1':
‘text= input("Enter a plaintext: ")
key=input("Enter the key: ")
encrypted _text = encrypt(text, key)
print("Cipher text is:", encrypted_text)
elif ed = "2:
TEXT = input("Enter cipher text: ")
KEY = input("Enter k
decrypted_text = decrypt(TEXT, KEY)
————_—print("Decrypted mMessage:", decrypted text)
else:
print(Error in input - ‘try again.)