Encryption and Decryption of Rc4
Encryption and Decryption of Rc4
REPORT VERIFICATION
Date :
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
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.')
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