Encryption troubles

closed account (4ET0pfjN)
Here is the crypt.cpp file and the assignment says I need to write an encryption function, crypt with ths algorithm:

The cryptographic key K is a 16-bit integer (unsigned short)
and encrption E is:

E(M) = M XOR (K||K||...) where key K is repeated n/2 times,
M is message and n is message length

This is what I have for crypt function, is it correct?
Here is the crypt.cpp that has the crypt function that I needed to write, the header was defined already, just the body of function I needed to write:
============================================================================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using namespace std;

void crypt(const unsigned short key, const char *message, char *ciphertext, const int messageLength) {
   
   //for n/2 times, do:
   //	ciphertext = message[ith position]^key //XOR the plaintext
   for ( int i = 0; i < strlen(message); i++ )
   {
	ciphertext = message^key;
   }
   
}

int main(int argc,char **argv ) {

  const unsigned int bufferSize = 4096;
  
  if(argc == 1 || argc > 3) {
    cout << "USAGE: decrypt <KEY> [\"MESSAGE\"]" <<endl;
    cout << "WHERE: KEY is an 16-bit integer between 0 - 65535 <required>" << endl;
    cout << "       \"MESSAGE\" is an encrypted message [optional]" << endl;
    exit(1);
  }
  
  char message[bufferSize] = {};
  char ciphertext[bufferSize] = {};
  
  unsigned short key = atoi(argv[1]);//ANDY ADDED COMMENT: convert string to int
    
  if(argc == 3)
    strncpy(message,argv[2],bufferSize);
  else 
    cin.get(message,bufferSize,'\0');

  int messageLength = strnlen(message,bufferSize);
  
  //crypt(key,message,ciphertext,messageLength);
  
  for(int i=0; i<messageLength; i++)
    cout << ciphertext[i];
    
  if(isatty(fileno(stdout)))
    cout << endl;

  return 0;

}

Topic archived. No new replies allowed.