Encryption ROT/Crypto

Hello, I've been wracking my brain and I cant find out where my encryption is going wrong. The file correctly inputs/outputs, but the encryption ROT/Crypto isn't working like planned. I didn't copy paste correctly, the return is in there...its that the encryption isn't working correctly. Hints are welcome, this is an assignment, and thank you.

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
  void Security::EncFileUsingRot(int rotNum)
{
  vector<string> s2;                           //Vector of class string of object s2

  getInputFile(s2);                            //invokes function and passes vector s2

  for(int i = 0; i < s2.size(); i++)
  {
    s2[i] = EncWordUsingRot(rotNum, s2[i]);    //calls function and stores the word in s2 element
  }
  cout<<endl;

  getOutputFile(s2);                           //invokes function to output file using vector s2
}

string Security::EncWordUsingRot(int rotNum, string word)
{
  rotNum = rotNum % 26;                        //ensures variable is not great than alphabet
  string encWord = word;

  for(int i = 0; i < word.size(); i++)
  {
    char c = word[i];
    c = tolower(c);                            //lowercases the letter for easier encryption
    if((c < 'a')||(c > 'z'))
    {
      encWord[i] = c;
    }
    else
    {
      c += rotNum;

      if(c > 'z')
      {
      c -= 26;
      }
      encWord[i] = c;

    }
  }
  return encWord;
}
Last edited on
Function EncWordUsingRot() is supposed to return a value of type string. But there is no return statement.
I had the return in there, just missed the copy/paste. For some reason the encrypted output file is off for ROT and Crypto.
The char type is probably treated as signed value in the range -128 to +127. A value greater than 127 will roll over to a negative value, which messes up the comparison c > 'z'.

Try declaring c as unsigned:
unsigned char c = word[i];
Last edited on
So that helped a lot, thank you! I still get some garbled results with using the crypto method and decryption. This is the rest of my code and I truly appreciate the help.
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
string Security::EncWordUsingCrypto(string key, string word)
{
  string encWord = word;

  for(int i = 0;i < word.size(); i++)
  {
    unsigned char c = word[i];
    c = tolower(c);
    if((c < 'a')||(c > 'z'))
    {
      encWord[i] = c;
    }
    else
    {
    int index = key.find(c, 0);
    encWord[i] = 'a' - index;
    }
  }
  return encWord;
}

void Security::DecFileUsingRot(int rotNum)
{
  vector<string> s2;

  getInputFile(s2);

  for(int i = 0; i < s2.size(); i++)
  {
    s2[i] = DecWordUsingRot(rotNum, s2[i]);
  }
  cout<<endl;

  getOutputFile(s2);
}

string Security::DecWordUsingRot(int rotNum, string word)
{
  rotNum = rotNum%26;
  string decWord = word;
  for(int i = 0;i < word.size(); i++)
  {
    unsigned char c = word[i];
    c = tolower(c);
    if((c < 'a')||(c > 'z'))
    {
      decWord[i] = c;
    }
    else
    {
      c -= rotNum;
      if(c >'z')
      {
        c += 26;
      }
      decWord[i] = c;
    }
  }
  return decWord;
}

void Security::DecFileUsingCrypto(string key)
{
  vector<string> s2;

  getInputFile(s2);

  for(int i = 0; i < s2.size(); i++)
  {
    s2[i] = DecWordUsingCrypto(key, s2[i]);
  }
  cout<<endl;

  getOutputFile(s2);
}

string Security::DecWordUsingCrypto(string key, string word)
{
  string decWord = word;

  for(int i = 0;i < word.size(); i++)
  {
    unsigned char c = word[i];
    c = tolower(c);
    if((c < 'a')||(c > 'z'))
      {
        decWord[i] = c;
      }
    else
    {
      int index = key.find(c, 0);
      decWord[i] = 'a' + index;
    }
  }
  return decWord;
}
Well, I wasn't quite sure what algorithm you were trying to implement, I tried an ordinary word such as "Bluegrass" as the key, but that didn't make sense. I figured it had to be a scrambled version of the alphabet.

This was my attempt. But if I'm on the wrong track, then I think you need to explain in more detail the expected input and output.
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>

    using namespace std;

string EncWordUsingCrypto(string key, string word);
string DecWordUsingCrypto(string key, string word);

int main()
{
    string original = "A good traveler has no fixed plans.";
    string key = "rsgtuvhixyzwdeabcjkopqflmn";

    string encoded = EncWordUsingCrypto(key, original);
    string decoded = DecWordUsingCrypto(key, encoded);

    cout <<  "original: " <<  original << endl;
    cout <<  "encoded:  " <<  encoded << endl;
    cout <<  "decoded:  " <<  decoded << endl;

    return 0;
}

string EncWordUsingCrypto(string key, string word)
{
    string encWord = word;

    for (unsigned int i=0; i<word.size(); i++)
    {
        unsigned char c = word[i];

        c = tolower(c);

        if ((c < 'a') || (c > 'z'))
        {
            encWord[i] = c;
        }
        else
        {
            int index = key.find(c, 0);
            encWord[i] = 'a' + index;
        }
    }
    return encWord;
}

string DecWordUsingCrypto(string key, string word)
{
    string decWord = word;

    for (unsigned int i = 0;i < word.size(); i++)
    {
        unsigned char c = word[i];
        c = tolower(c);
        if((c < 'a')||(c > 'z'))
        {
            decWord[i] = c;
        }
        else
        {
            int index = c - 'a';
            decWord[i] = key[index];
        }
    }
    return decWord;
}

Output:
original: A good traveler has no fixed plans.
encoded:  o cttm daofnxna gob zt whinm uxozb.
decoded:  a good traveler has no fixed plans.
Wow, that's great. You are correct in the assumption was an alphabet encryption/decryption, apologize for not mentioning that. I lowered all the cases to ensure easier use and just copy/pasted all char outside the alphabet. It was the decryption that was killing me. I tried so many methods, but inside decWord[i] = key[index], e.g. decWord[I] -= index and so on. Thank you very much. How coincidental that I just clicked this solved as you posted your solution!

cheers,

Russell
Glad you saw the response, sorry it took me a while to get back to this, I needed a clear space look at it without distractions.
Topic archived. No new replies allowed.