Report CCN

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

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)

Project based Experiment Report


On
Implementation of RC4 Encryption and Decryption Algorithms

Items Mark RA2011053 RA2011053 RA2010053


s 010038 010052 010060
Doddi Gorikapudi CH. Naga
Muralidhar Anne Sruthi
Proposal 05
Demonstration 10

Presentation 10
Report 10
Viva 05
Total 40

REPORT VERIFICATION

Date :

Staff Name :

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

Name of the members of the Group with Reg. Nos.:


Doddi Muralidhar RA2011053010038
Gorikapudi Anne RA2011053010052
CH Naga Sruthi RA2010053010060

Introduction: RC4 is a symmetric key stream cipher algorithm, which means it uses the
same key for both encryption and decryption, and operates on a stream of input data one byte
at a time. It was developed by Ron Rivest in 1987 and is widely used in various applications,
such as wireless security protocols (e.g., WEP and WPA), SSL/TLS, and file encryption.

RC4 works by initializing a state array (S) of 256 bytes, where each byte is a number from 0
to 255, and then performing a key-scheduling algorithm (KSA) that modifies the state array
based on the input key. After the KSA, the state array is used to generate a keystream of
pseudo-random bytes using a pseudo-random generation algorithm (PRGA). The keystream
is then XORed with the plaintext to produce the ciphertext, or XORed with the ciphertext to
recover the plaintext.

One of the strengths of RC4 is its simplicity and speed. It can be implemented with relatively
few lines of code and can process data quickly. However, RC4 has several weaknesses,
including biases in the initial bytes of the keystream, and a vulnerability to related key attacks
when the same key is used multiple times with different nonces. As a result, RC4 is generally
no longer considered secure for modern cryptography applications and is recommended to be
avoided in favor of newer algorithms such as AES

Software used: Jupyter Notebook

RC4 algorithm Theory/Explanation:


RC4 is a stream cipher, which means that it encrypts data one byte at a time using a stream of
pseudo-random bytes called the keystream. The algorithm consists of two main parts: the
key-scheduling algorithm (KSA) and the pseudo-random generation algorithm (PRGA).

The KSA takes the encryption key and uses it to initialize an array of 256 bytes called the
state (S). It does this by performing a series of swaps between elements of S based on the key
and the current position in the array.

The PRGA generates the keystream by repeatedly swapping elements of S and using them to
compute a pseudo-random byte K. This process continues indefinitely, generating a stream of
pseudo-random bytes that are XORed with the plaintext to produce the ciphertext.

To encrypt a message using RC4, we first generate the keystream by running the KSA and
PRGA with the encryption key. We then XOR each byte of the plaintext with the
corresponding byte of the keystream to produce the ciphertext.

To decrypt a message, we simply encrypt the ciphertext using the same key. This is possible
because XORing a value twice with the same key will cancel out the encryption and return
the original plaintext.

Implementation Methodology or steps:

Define the Key and Plaintext: To start with, we need to define the key and plaintext that we
want to encrypt using the RC4 algorithm.

Initialize the S-Box and Key Scheduling Algorithm (KSA): In the next step, we need to
initialize the S-box and perform the Key Scheduling Algorithm (KSA

Generate the Pseudo-Random Stream (PRGA): In the next step, we need to generate the
Pseudo-Random Stream using the S-box and the PRGA

Encrypt the Plaintext: In this step, we need to encrypt the plaintext using the Pseudo-Random
Stream.

To decrypt a message, we simply encrypt the ciphertext using the same key. This is possible
because XORing a value twice with the same key will cancel out the encryption and return
the original plaintext.

Programme:
def encryption():

global key, plain_text, n

# Given text and key


plain_text = "001010010010"
key = "101001000001"

# n is the no: of bits to


# be considered at a time
n = 3

print("Plain text : ", plain_text)


print("Key : ", key)
print("n : ", n)

print(" ")

# The initial state vector array


S = [i for i in range(0, 2**n)]
print("S : ", S)

key_list = [key[i:i + n] for i in range(0, len(key), n)]


# Convert to key_stream to decimal
for i in range(len(key_list)):
key_list[i] = int(key_list[i], 2)

# Convert to plain_text to decimal


global pt

pt = [plain_text[i:i + n] for i in range(0, len(plain_text), n)]

for i in range(len(pt)):
pt[i] = int(pt[i], 2)

print("Plain text ( in array form ): ", pt)

# Making key_stream equal


# to length of state vector
diff = int(len(S)-len(key_list))

if diff != 0:
for i in range(0, diff):
key_list.append(key_list[i])

print("Key list : ", key_list)


print(" ")

# Perform the KSA algorithm


def KSA():
j = 0
N = len(S)

# Iterate over the range [0, N]


for i in range(0, N):

# Find the key


j = (j + S[i]+key_list[i]) % N

# Update S[i] and S[j]


S[i], S[j] = S[j], S[i]
print(i, " ", end ="")

# Print S
print(S)

initial_permutation_array = S

print(" ")
print("The initial permutation array is : ",
initial_permutation_array)

print("KSA iterations : ")


print(" ")
KSA()
print(" ")

# Perform PGRA algorithm


def PGRA():

N = len(S)
i = j = 0
global key_stream
key_stream = []

# Iterate over [0, length of pt]


for k in range(0, len(pt)):
i = (i + 1) % N
j = (j + S[i]) % N

# Update S[i] and S[j]


S[i], S[j] = S[j], S[i]
print(k, " ", end ="")
print(S)
t = (S[i]+S[j]) % N
key_stream.append(S[t])

# Print the key stream


print("Key stream : ", key_stream)
print(" ")

print("PGRA iterations : ")


print(" ")
PGRA()

# Performing XOR between generated


# key stream and plain text
def XOR():
global cipher_text
cipher_text = []
for i in range(len(pt)):
c = key_stream[i] ^ pt[i]
cipher_text.append(c)

XOR()

# Convert the encrypted text to


# bits form
encrypted_to_bits = ""
for i in cipher_text:
encrypted_to_bits += '0'*(n-len(bin(i)[2:]))+bin(i)[2:]

print(" ")
print("Cipher text : ", encrypted_to_bits)
encryption()

print("---------------------------------------------------------")

# Function for decryption of data


def decryption():

# The initial state vector array


S = [i for i in range(0, 2**n)]

key_list = [key[i:i + n] for i in range(0, len(key), n)]

# Convert to key_stream to decimal


for i in range(len(key_list)):
key_list[i] = int(key_list[i], 2)

# Convert to plain_text to decimal


global pt

pt = [plain_text[i:i + n] for i in range(0, len(plain_text), n)]

for i in range(len(pt)):
pt[i] = int(pt[i], 2)

# making key_stream equal


# to length of state vector
diff = int(len(S)-len(key_list))

if diff != 0:
for i in range(0, diff):
key_list.append(key_list[i])

print(" ")

# KSA algorithm
def KSA():
j = 0
N = len(S)

# Iterate over the range [0, N]


for i in range(0, N):
j = (j + S[i]+key_list[i]) % N

# Update S[i] and S[j]


S[i], S[j] = S[j], S[i]
print(i, " ", end ="")
print(S)

initial_permutation_array = S
print(" ")
print("The initial permutation array is : ",
initial_permutation_array)
print("KSA iterations : ")
print(" ")
KSA()
print(" ")

# Perform PRGA algorithm


def do_PGRA():

N = len(S)
i = j = 0
global key_stream
key_stream = []

# Iterate over the range


for k in range(0, len(pt)):
i = (i + 1) % N
j = (j + S[i]) % N

# Update S[i] and S[j]


S[i], S[j] = S[j], S[i]
print(k, " ", end ="")
print(S)
t = (S[i]+S[j]) % N
key_stream.append(S[t])

print("Key stream : ", key_stream)


print(" ")

print("PGRA iterations : ")


print(" ")
do_PGRA()

# Perform XOR between generated


# key stream and cipher text
def do_XOR():
global original_text
original_text = []
for i in range(len(cipher_text)):
p = key_stream[i] ^ cipher_text[i]
original_text.append(p)

do_XOR()

# convert the decrypted text to


# the bits form
decrypted_to_bits = ""
for i in original_text:
decrypted_to_bits += '0'*(n-len(bin(i)[2:]))+bin(i)[2:]

print(" ")
print("Decrypted text : ",
decrypted_to_bits)

# Driver Code
decryption()

Demonstration Result and Screenshots:


We were able to encrypt and decrypt data using the RC4 algorithm in your Python programs.
The implementation produced ciphertext that is generated by XORing the plaintext with a
keystream generated by the RC4 algorithm, using the provided key. This ciphertext can be
sent over a network or stored in a file, and can only be decrypted by someone who has the
same key used for encryption.

To decrypt the ciphertext, you can use the same key to generate the keystream and then
XOR it with the ciphertext to recover the original plaintext. The implementation of RC4
encryption and decryption in Python is relatively simple and can be done with just a few lines
of code
Tabulation: Key: 101001000001
S.No Plain text Cipher Text

1 101001000001 110011100011
2 101001000001 011011100011
3 101001000001 010011100011
4 111010010010 000011100011
5 001110001110 110111111111

Conclusion:
In conclusion, RC4 is a widely used symmetric key stream cipher algorithm for encryption
and decryption of data. Although RC4 is simple and fast, it has several weaknesses that make
it insecure for modern cryptography applications. These include biases in the initial bytes of
the keystream and vulnerability to related key attacks. As a result, RC4 is no longer
recommended for use in new applications and is generally considered to be insecure. It is
recommended to use newer, more secure encrpyption algorithms such as AES for
cryptography applications.

Reference List: https://www.geeksforgeeks.org/implementation-of-rc4-algorithm/


https://www.tutorialspoint.com/what-is-rc4-encryption-working-usage-
advantages-and-disadvantages

You might also like