1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
icang [17]
3 years ago
7

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 discar

d the first 3072 bytes of the pseudo random numbers. THE KEY OR THE INPUT TEXT MUST NOT BE HARD CODED IN THE PROGRAM.
Computers and Technology
1 answer:
gladu [14]3 years ago
6 0

Answer:

Explanation:

#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;

}

You might be interested in
Write a basic notation for
Licemer1 [7]

Answer:

a = (b + c)/(2 * c)

z = x/(y + c)

c = (9 * c + 32)/5

Explanation:

Required

The expression in basic

To do this, we use () to group items, / as divide and * to combine factors

So, we have:

(a)\ a = \frac{(b + d)}{2c}

In basic, it is:

a = (b + c)/(2 * c)

(b)\ z = \frac{x}{y + c}

In basic, it is:

z = x/(y + c)

(c)\ c = \frac{9c + 32}{5}

c = (9 * c + 32)/5

4 0
3 years ago
10.13 lab 10c: output range with increment of 5 write a program whose input is two integers. output the first integer and subseq
Lynna [10]

he program gives an output in increment of 5, if the first inputted integer is less than the second. The program written in python 3 goes thus

3 0
2 years ago
Write an expression that computes the difference of the variables x and y on Python.
yulyashka [42]

Answer:

x times y

Explanation: because adding the fractions would ecvlate a different b     fraction

7 0
3 years ago
Laura is confused with the spelling of the word pronunciation. She types the word pronunciation. Which feature of the auto corre
larisa [96]
I think this answer is b
5 0
3 years ago
Read 2 more answers
Create a program called "Geometry" Prompt the user for a small decimal number. Prompt the user for a large decimal number. Using
skelet666 [1.2K]

Answer:

In Python:

import random

small = float(input("Small: "))

large = float(input("Large: "))

radius = round(random.uniform(small, large),2)

volume = round(4/3 * 22/7 * radius* radius* radius,2)

print("Radius: "+str(radius))

print("Volume: "+str(volume))

Explanation:

This imports the random module

import random

The next two lunes prompt the user for small and large decimal number

small = float(input("Small: "))

large = float(input("Large: "))

This generates the radius

radius = round(random.uniform(small, large),2)

This calculates the volume

volume = round(4/3 * 22/7 * radius* radius* radius,2)

This prints the generated radius

print("Radius: "+str(radius))

This prints the calculated volume

print("Volume: "+str(volume))

<em>Note that, the radius and the volume were approximated to 2 decimal places. Though, it wasn't stated as part of the program requirement; but it is a good practice.</em>

8 0
3 years ago
Other questions:
  • 5. What are the two main functions performed by UPS​
    12·1 answer
  • Word processing software, spreadsheet software, database software, and presentation software are examples of what category of co
    6·1 answer
  • When two or more links need to pass traffic as if they were one physical link, which of the following would be used to satisfy t
    12·1 answer
  • How would you compare and contrast the impact of the printing press with the impact of the internet?
    15·1 answer
  • Multiply the following two Tom's Tiny floating-point format numbers (8-bit: sign bit, 3-bit 2's complement exponent, 4-bit fract
    12·1 answer
  • a term to describe article that can be displayed in their entirety as opposed to abstract and reference only?​
    10·1 answer
  • 71 81 77 15 63 96 36 51 77 18 17
    14·1 answer
  • In this assignment, you will implement an online banking system. Users can sign-up with the system, log in to the system, change
    14·1 answer
  • Which programming language in order takes the most lines code​
    6·2 answers
  • 1. A cell is identified by its ........
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!