Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. write programs to implement the following: 1) one-time pad 2) the RC4 stream

ID: 3862577 • Letter: 1

Question

1. write programs to implement the following:
1) one-time pad
2) the RC4 stream cipher
3) construction for a deterministic polynomial-time algorithm G zs a variable output-length pseudorandom generator if the following hold:

1. Let s be a string and .e > 0 be an integer. Then G ( s, 11) outputs a string of length .e.

2. For all s, .e, f' with .e < .e, the string G( s, 1 E) is a prefix of G(s, 1 E') .

3. Define GE (s) def G(s, 1E(Isl)). Then for every polynomial C(·) it holds that G E is a pseudorandom generator with expansion factor .e.
4) stream-cipher mode of operation for encrypting a single variable-length message

Explanation / Answer

(1). program in c++ for one time pad

#include<iostream>

#include<stdlib.h>

#include<vector>

/* using namespace */

using namespace std;

/* upper case function */

void to_upper_case(vector<char>& text, int len)

{

for (int i = 0; i < len; i++)

{

if (text[i] >= 97 && text[i] <= 122)

text[i] -= 32;

}

}

/* print string function*/

void print_string(vector<char> text, int len)

{

for (int i = 0; i < len; i++)

{

cout << (char) (text[i] + 65);

}

cout << endl;

return;

}

size_t get_input(vector<char>& msg)

{

char a;

while (1)

{

a = getchar();

if (a == ' ')

break;

msg.push_back(a);

}

return msg.size();

}

int main()

{

vector<char> msg;

vector<char> enc_msg;

vector<char> dec_msg;

int *p;

int i;

size_t len;

cout << "Enter Message to be Encrypt:";

len = get_input(msg);

to_upper_case(msg, len);

p = (int*) malloc(msg.size() * sizeof(int));

for (i = 0; i < len; i++)

{

p[i] = rand() % 26;

if (msg[i] >= 65 && msg[i] <= 90)

enc_msg.push_back((char) ((msg[i] - 65 + p[i]) % 26));

else if (msg[i] >= 97 && msg[i] <= 122)

enc_msg.push_back((char) ((msg[i] - 97 + p[i]) % 26));

else

enc_msg.push_back((char) msg[i]);

}

cout << " Encoded Message:";

print_string(enc_msg, len);

cout << " Key for decryption: ";

for (i = 0; i < len; i++)

{

cout << (char) (p[i] + 65);

}

cout << endl;

cout << " Decrypted Message:";

for (i = 0; i < len; i++)

{

if ((enc_msg[i] - p[i]) < 0)

dec_msg.push_back((char) (enc_msg[i] - p[i] + 26));

else if ((enc_msg[i] - p[i]) >= 0)

dec_msg.push_back((char) (enc_msg[i] - p[i]));

else

dec_msg.push_back((char) enc_msg[i]);

}

/* prints op P/

print_string(dec_msg, len);

return 0;

}

(2). Program for implementing RC4 stream cipher.

#include "rc4.h"

#include <string>

#include <cstdlib>

// using namespace

using namespace std;

// doesn's take arguments.

RC4::RC4(){

// initial i,j to 0

i = 0;

j = 0;

}

// RC4 algorithm

void RC4::stream_setup(char* key){

int key_length = strlen(key);

int a = 0;

int b = 0;

int temp = 0;

for (a = 0; a < 255; a++){

S[a] = a;

K[a] = (int)key[b];

b++;

if (b > key_length){

b = 0;

}

}

for (a = 0; a < 255; a++){

b = (b + S[a] + (int)K[a]) % 256;

temp = S[a];

S[a] = S[b];

S[b] = temp;

}

}

char RC4::getStream(){

int temp = 0;

i = (i + 1) % 256;

j = (j + S[i]) % 256;

temp = S[i];

S[i] = S[j];

S[j] = temp;

int t = (S[i] + S[j]) % 256;

char* stream = (char *)(&S[t]);

return stream[0];

}

// char *encrypts it returning char *

char* RC4::rc4Crypt(char* data){

char crypto;

int data_length = strlen(data);

for (int index = 0; index <= data_length; index++){

crypto = getStream();

data[index] = data[index] ^ crypto;

}

return data;

}