Implement the RC4 stream cipher. User should be able to enter any key that is 5
ID: 3916609 • Letter: I
Question
Implement the RC4 stream cipher. User should be able to enter any key that is 5 bytes to 32 bytes long. Be sure to discard the first 3072 bytes of the pseudo random numbers. THE KEY OR THE INPUT TEXT MUST NOT BE HARD CODED IN THE PROGRAM.
Test your program with the following plain text file:
In cryptography, RC4 (Rivest Cipher 4 also known as ARC4 or ARCFOUR meaning Alleged RC4) is a stream cipher. While remarkable for its simplicity and speed in software, multiple vulnerabilities have been discovered in RC4, rendering it insecure. It is especially vulnerable when the beginning of the output keystream is not discarded, or when nonrandom or related keys are used. Particularly problematic uses of RC4 have led to very insecure protocols such as WEP
Using the following code below. Write TWO separate programs: encryption and decryption. The encryption program should input the plaintext file and output a cipher text in hex. The decryption program should input the cipher text file in hex and output the plaintext.
#include<iostream>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
typedef uint8_t byte;
typedef struct
{
byte i, j;
byte S[256];
} Rc4State;
void swap(byte *a, byte *b)
{
byte temp = *a;
*a = *b;
*b = temp;
}
/*Initialization & initial permutation
also initialize i&j counters in state for stream generation*/
void initState(const byte K[256], int keylen, Rc4State *state)
{
byte T[256];
assert(keylen >= 1 && keylen <= 256);
int i;
for (i = 0; i < 256; i++) {
state->S[i] = i;
T[i] = K[i % keylen];
}
//Initial permutation of S
byte *S = state->S;
int j = 0;
for (i = 0; i < 256; i++) {
j = (j + S[i] + T[i]) % 256;
swap(&S[i], &S[j]);
}
//Initialize counters in state
state->i = state->j = 0;
}
/*Encrypt/Decrypt text by XORing with next byte of keystream*/
byte crypt(byte text, Rc4State *state)
{
byte t, k;
byte *i = &(state->i), *j = &(state->j);
byte *S = state->S;
*i = (*i + 1) % 256;
*j = (*j + S[*i]) % 256;
swap(&S[*i], &S[*j]);
t = (S[*i] + S[*j]) % 256;
k = S[t];
return text ^ k;
}
static byte rc4CryptByte(Rc4State *state, byte plainText)
{
byte *S = state->S;
byte i = ++(state->i);
byte j = (state->j += S[i]);
swap(&S[i], &S[j]);
byte t = S[i] + S[j];
byte k = S[t];
return plainText ^ k;
}
void rc4Crypt(Rc4State *state, byte text[], size_t len)
{
for (size_t i = 0; i < len; i++)
{
text[i] = rc4CryptByte(state, text[i]);
}
system("pause");
}
Explanation / Answer
#For Encryption
#For Decyption
import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.binary.Hex; public class RC4 { private int[] S = new int[256]; private final int keylen; public RC4(final byte[] key) { if (key.length < 1 || key.length > 256) { throw new IllegalArgumentException("key must be between 1 and 256 bytes"); } else { keylen = key.length; for (int i = 0; i < 256; i++) { S[i] = i; } int j = 0; for (int i = 0; i < 256; i++) { j = (j + S[i] + key[i % keylen]) % 256; int temp = S[i]; S[i] = S[j]; S[j] = temp; } } } public String encrypt(final String message) { byte[] plaintext = new byte[0]; try { plaintext = message.getBytes("ASCII"); } catch (UnsupportedEncodingException e) { } byte[] ciphertext = toBytes(transform(toInts(plaintext))); return Hex.encodeHexString(ciphertext); } private int[] transform(final int[] input) { final int[] output = new int[input.length]; final int[] S = new int[this.S.length]; System.arraycopy(this.S, 0, S, 0, S.length); int i = 0, j = 0, k, t; for (int counter = 0; counter < input.length; counter++) { i = (i + 1) & 0xFF; j = (j + S[i]) & 0xFF; S[i] ^= S[j]; S[j] ^= S[i]; S[i] ^= S[j]; t = (S[i] + S[j]) & 0xFF; k = S[t]; output[counter] = input[counter] ^ k; } return output; } private int[] toInts(byte[] bytes) { int[] output = new int[bytes.length]; for (int i = 0; i < bytes.length; i++) { output[i] = bytes[i]; } return output; } private byte[] toBytes(int[] ints) { byte[] output = new byte[ints.length]; for (int i = 0; i < ints.length; i++) { output[i] = (byte) ints[i]; } return output; } public static void main(String[] args) { byte[] key = "Key".getBytes(); RC4 rc4 = new RC4(key); String message = "pedia"; System.out.println("Message: " + message); String cipherText = rc4.encrypt(message); System.out.println("Encrypted: " + cipherText); } }