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

Implement the RC4 stream cipher in C++. User should be able to enter any key tha

ID: 3915182 • Letter: I

Question

Implement the RC4 stream cipher in C++. 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:

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.

You should 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.

May I get two screen shots showing the encryption then decryption with a 5 byte key, the cipher text from the encryption, saved in hexadecimal (you may want to use the “hex” output manipulator), the plaintext from the decryption of the cipher text (you need to convert hex text to number)? I would like to know how it suppose to look like so I can understand the source codes. Thank you.

Explanation / Answer

Ans :

class CRC4

{

public:

CRC4 ()

{

memset(sbox,0,256);

memset(key,0,256);

}

virtual ~CRC4 ()

{

memset(sbox,0,256);

memset(key,0,256);   

}

char *Encrypt(char *pszText,const char *pszKey)

{

i=0, j=0,n = 0;

ilen = (int)strlen(pszKey);

for (m = 0; m < 256; m++)

{

*(key + m)= *(pszKey + (m % ilen));

*(sbox + m) = m;

}

for (m=0; m < 256; m++)

{

n = (n + *(sbox+m) + *(key + m)) &0xff;

SWAP(*(sbox + m),*(sbox + n));

}

ilen = (int)strlen(pszText);

for (m = 0; m < ilen; m++)

{

i = (i + 1) &0xff;

j = (j + *(sbox + i)) &0xff;

SWAP(*(sbox+i),*(sbox + j));

k = *(sbox + ((*(sbox + i) + *(sbox + j)) &0xff ));

if(k == *(pszText + m))

encoded text;

k = 0;

*(pszText + m) ^= k;

}

return pszText;

}

char *Decrypt(char *pszText,const char *pszKey)

{

return Encrypt(pszText,pszKey) ;

}

private:

unsigned char sbox[256];

unsigned char key[256],k;   

int m, n, i, j, ilen;

};

class CBase32

{

public:

CBase32(){}

char *Encrypt(const char * srcp, int len, char * dstp)

{

register int i = 0;

char *dst = dstp;

for (i = 0; i < len - 2; i += 3)

{

*dstp++ = *(base32_map + ((*(srcp+i)>>2)&0x3f));

*dstp++ = *(base32_map + ((*(srcp+i)<< 4)&0x30 | (

*(srcp+i+1)>>4)&0x0f ));

*dstp++ = *(base32_map + ((*(srcp+i+1)<<2)&0x3C | (

*(srcp+i+2)>>6)&0x03));

*dstp++ = *(base32_map + (*(srcp+i+2)&0x3f));

}

srcp += i;

len -= i;

if(len & 0x02 )

{

*dstp++ = *(base32_map + ((*srcp>>2)&0x3f));

*dstp++ = *(base32_map + ((*srcp<< 4)&0x30 | (

*(srcp+1)>>4)&0x0f ));

*dstp++ = *(base32_map + ((*(srcp+1)<<2)&0x3C) );

*dstp++ = '=';

}

else if(len & 0x01 )

{

*dstp++ = *(base32_map + ((*srcp>>2)&0x3f));

*dstp++ = *(base32_map + ((*srcp<< 4)&0x30));

*dstp++ = '=';

*dstp++ = '=';

}

*dstp = '';

return dst;

}

void *Decrypt(const char * srcp, int len, char * dstp)

{

register int i = 0;

void *dst = dstp;

while(i < len)

{

*dstp++ = (B32_offset[*(srcp+i)] <<2 |

B32_offset[*(srcp+i+1)] >>4);

*dstp++ = (B32_offset[*(srcp+i+1)]<<4 |

B32_offset[*(srcp+i+2)]>>2);

*dstp++ = (B32_offset[*(srcp+i+2)]<<6 |

B32_offset[*(srcp+i+3)] );

i += 4;

}

srcp += i;

if(*(srcp-2) == '=')

{

*(dstp--) = '';

*(dstp--) = '';

}

else if(*(srcp-1) == '=')

*(dstp--) = '';

*dstp = '';

return dst;

};

size_t B32_length(size_t len)

{

size_t npad = len%3;

  

size_t size = (npad > 0)? (len +3-npad ) : len;

return (size*8)/6;

}

size_t Ascii_length(size_t len)

{

return (len*6)/8;

}

};

#include "Base32_RC4.h"

void main()

{

char str[32] = "This is a test for RC4 cypher";

CRC4 rc4;

printf("Demo for RC4 ");

printf("Plain text: %s ",str);

rc4.Encrypt(str,"Key");

printf("Encoded string: %s ",str);

rc4.Decrypt(str,"Key");

printf("Decoded string: %s ",str);

strcpy(str, "This is a test for Base32 cypher");

CBase32 base32;

char *dst;

dst = (char*)malloc( base32.B32_length(strlen(str))+1);

if(dst == NULL)

return;

printf(" Demo for Base32 ");

printf("Plain text: %s ",str);

base32.Encrypt(str,strlen(str),dst);

printf("Encoded string: %s ",dst);

memset(str,0,sizeof(str));

base32.Decrypt(dst,strlen(dst),str);

printf("Decoded string: %s ",str);

free(dst);

getchar();