Question

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

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.

program should be tested by a plain text

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

PLEASE COMPLETE IN C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <string>
#include<vector>
using namespace std;

vector<int> permute(vector<int>, vector<int>);
string encrypt(vector<int>s1 , vector<int> t1, string p);
string decrypt(vector<int>s1, vector<int> t1, string p);

int main() {

   string plaintext = "cryptology";
   string plaintext2 = "RC4";
   vector<int> S(256);
   vector<int> T(256);

   int key[] = { 1,2,3,6 };
   int key2[] = { 5,7,8,9 };

   int tmp = 0;
   for (int i = 0; i < 256;i++) {
       S[i] = i;
       T[i] = key[( i % (sizeof(key)/sizeof(*key)) )];
   }

   S = permute(S, T);

   for (int i = 0; i < 256 ;i++) {
       cout << S[i] << " ";
       if ((i + 1) % 16 == 0)
           cout << endl;
   }

   cout << endl;
   string p = encrypt(S, T, plaintext);
   cout << "Message: " << plaintext << endl;
   cout << "Encrypted Message: " << " " << p << endl;
   cout << "Decrypted Message: " << decrypt(S, T, p) << endl << endl;


   tmp = 0;
   for (int i = 0; i < 256;i++) {
       S[i] = i;
       T[i] = key2[(i % (sizeof(key) / sizeof(*key)))];
   }

   S = permute(S, T);

   for (int i = 0; i < 256;i++) {
       cout << S[i] << " ";
       if ((i + 1) % 16 == 0)
           cout << endl;
   }

   cout << endl;
   p = encrypt(S, T, plaintext2);
   cout << "Message: " << plaintext2 << endl;
   cout << "Encrypted Msg: " << p << endl;
   cout << "Decrypted Msg: "<<decrypt(S, T, p) << endl << endl;


   return 0;
}

string decrypt(vector<int>s1, vector<int> t1, string p) {
   int i = 0;
   int j = 0;
   int tmp = 0;
   int k = 0;

   int b;
   int c;

   int * plain = new int[p.length()];
   string plainT;

   for (int r = 0; r < p.length(); r++) {

       i = (i + 1) % 256;
       j = (j + s1[i]) % 256;


       b = s1[i];
       s1[i] = s1[j];
       s1[j] = b;

       tmp = (s1[i] + s1[j]) % 256;
       k = s1[tmp];

       c = ((int)p[r] ^ k);

       plain[r] = c;

       plainT += (char)plain[r];

   }
   return plainT;
}

string encrypt(vector<int>s1, vector<int> t1, string p) {

   int i = 0;
   int j = 0;
   int tmp = 0;
   int k = 0;


   int b;
   int c;

   int * cipher = new int [p.length()];
   string cipherT;
   cout << "Keys Generated for plaintext: ";

   for (int r = 0; r < p.length(); r++) {

       i = (i + 1) % 256;
       j = (j + s1[i]) % 256;

       b = s1[i];
       s1[i] = s1[j];
       s1[j] = b;

       tmp = (s1[i] + s1[j]) % 256;
       k = s1[tmp];

       cout << k << " ";
       c = ((int)p[r] ^ k);

       cipher[r] = c;

       cipherT += (char)cipher[r];
   }
   cout << endl;
   return cipherT;
}


vector<int> permute(vector<int> s1, vector<int> t1) {
   int j = 0;
   int tmp;
   for (int i = 0; i< 256; i++) {
       j = (j + s1[i] + t1[i]) % 256;

       tmp = s1[i];
       s1[i] = s1[j];
       s1[j] = tmp;
   }
   return s1;
}

Add a comment
Know the answer?
Add Answer to:
Implement the RC4 stream cipher in C++. User should be able to enter any key that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement the RC4 stream cipher in C++. User should be able to enter any key that is 5 bytes to 3...

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

  • The given plaintext is “Feistel cipher structure uses the same algorithm for both encryption and decryption”....

    The given plaintext is “Feistel cipher structure uses the same algorithm for both encryption and decryption”. Write Java code to implement Shift cipher (Encryption and Decryption) and test your code on given plaintext. Your code must meet following conditions. 1. User must enter the value of key from command prompt and print it at command prompt. 2. Print the cipher text and the plaintext at the command prompt after encryption and decryption. 3. Test your algorithm for 5 different key...

  • Write a program that implements an elementary bit stream cipher. An elementary level bit stream cipher...

    Write a program that implements an elementary bit stream cipher. An elementary level bit stream cipher is an encryption algorithm that encrypts 1 byte of plain text at a time. This one uses a given 4-bit bit pattern as the key. The size of the encrypted message that we want to be able to send has a maximum length of 200 characters. You must: 1. prompt the user to input the clear text to be encrypted. You must use printf()...

  • Final Project-1 A Modified XTS-AES Encryption and Decryption Input File: input. and key txt Time Limit:...

    Final Project-1 A Modified XTS-AES Encryption and Decryption Input File: input. and key txt Time Limit: No Problem Description Advanced Encryption Standard (AES) is a well-known symmetric block cipher in modern cryptography. It was published by NIST in 2001. Here, we design a modified XTS-AES as shown in Fig. 1. Please write two programs for encryption and decryption (e.g., encrypt.cpp and decrypt.cpp). To test the correctness of your encryption and decryption, two samples (one 256-bit plaintext and one 192-bit plaintext...

  • Decrypting the APCO cipher without the key. Decryption without the key is obviously a much more...

    Decrypting the APCO cipher without the key. Decryption without the key is obviously a much more difficult process. Indeed, the purpose of encryption is to make it as difficult as possible for anyone who does not know the key to read the plain text. We will be using an unsophisticated password cracking technique called a brute force attack. A brute force attack on the APCO cipher works by trying every possible four digit key (from 0000 to 9999) and keeping...

  • Write a Python program which implements the following two classical cryptosystem which we covered n class:...

    Write a Python program which implements the following two classical cryptosystem which we covered n class: a) Affine Cipher b) Vigenere Cipher Your program should consist of at least five functions: a) Two functions named encrypt, one for each of the two algorithms which accepts a lowercase alphabetical plaintext string and key as input and outputs a corresponding cipher text string. b) Two functions named decrypt, one for each of the two algorithms which accepts a lowercase alphabetical ciphertext string...

  • I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text an...

    I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text and key My Current Code is: void SubDecrypt(char *message, char *encryptKey) { int iteration; int iteration_Num_Two = 0; int letter; printf("Enter Encryption Key: \n");                                                           //Display the message to enter encryption key scanf("%s", encryptKey);                                                                   //Input the Encryption key for (iteration = 0; message[iteration] != '0'; iteration++)                               //loop will continue till message reaches to end { letter = message[iteration];                                                      ...

  • Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple...

    Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple encryption methods. It works by shifting all letters in the original message (plaintext) by a certain fixed amount (the amounts represents the encryption key). The resulting encoded text is called ciphertext. Example Key (Shift): 3 Plaintext: Abc Ciphertext: Def Task Your goal is to implement a Caesar cipher program that receives the key and an encrypted paragraph (with uppercase and lowercase letters, punctuations, and...

  • C program (Not C++, or C#) Viginere Cipher 1)Ask the user if we are encrypting or...

    C program (Not C++, or C#) Viginere Cipher 1)Ask the user if we are encrypting or decrypting. 2) Ask the user to enter a sentence to be transformed. 3) Ask the user to enter a sentence that will be used as the encryption or decryption key. 4) The sentences (array of characters) should end with a NULL terminator '\0'. 5) The range of values will be all printable characters on the ASCII chart starting with a SPACE - Value 32,...

  • Write a javascript program which implements the following two classical cryptosystem which we covered in class:...

    Write a javascript program which implements the following two classical cryptosystem which we covered in class: Affine Cipher Vigenere Cipher Your program should consist of at least five functions: Two functions named encrypt, one for each of the two algorithms which accepts a lowercase alphabetical plaintext string and key as input and outputs a corresponding cipher text string. Two functions named decrypt, one for each of the two algorithms which accepts a lowercase alphabetical ciphertext string and a key as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT