Rivest Cipher 4

Rivest Cipher 4 (RC4)

A stream cipher designed by Ronald Rivest in 1987. Unlike block ciphers, which encrypt fixed-size blocks of data, RC4 encrypts data one byte at a time. This makes it well-suited for applications where data arrives in variable-length streams, such as network communications.RC4 generates a pseudorandom key stream based on an initial secret key. Each byte of plaintext is then combined with the key stream using the XOR operation to produce ciphertext. Decryption uses the same key to regenerate the key stream, and XOR is applied again to recover the original plaintext.

echo “test” > input.txt # Create a file named input.txt containing the text “test”

echo "test" > input.txt

openssl # an open-source cryptography toolkit
-e #encrypt
-rc4 # RC4 algorithm (deprecated / insecure)
-in input.txt # input plaintext file
-out encrypted.txt # output encrypted file
-K ‘000102030405060708090A0B0C0D0E0F’ # raw encryption key in hexadecimal (128-bit key)
-provider legacy # required because DES is deprecated in OpenSSL 3+

openssl enc -e -rc4 -in input.txt -out encrypted.txt -K '000102030405060708090A0B0C0D0E0F' -provider legacy

openssl # an open-source cryptography toolkit
-d # decrypt
-rc4 # same algorithm and mode used for encryption
-in encrypted.txt # input ciphertext file
-out decrypted.txt # output decrypted file
-K ‘000102030405060708090A0B0C0D0E0F’ # raw encryption key in hexadecimal (128-bit key)
-nosalt # disables salt (needed because encryption used raw key)
-provider legacy # enables deprecated DES support

openssl enc -d -rc4 -in encrypted.txt -out decrypted.txt -K '000102030405060708090A0B0C0D0E0F' -nosalt -provider legacy

cat decrypted.txt # Display the decrypted file contents

cat decrypted.txt