Moving bits to alter characters

Pages: 12
That's definitely C++ though.

No idea what the values in eTable and dTable mean, though.
Sorry about that... this is why I never "work" as a programmer because my documentation skills are nil. Here's a repost with comments:

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
namespace MyCrypt
{
  const char *DEFAULT_KEY = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+-=<>?,./\\|[]{}`~";
  const int eTable[] = {  85,170,-17, 68,153, 17,136, 34,-136, 51,-136, -51,-153, -51,-170 }; // split char value into 15 divisions, tables indicate value to offset
  const int dTable[] = { 136, 17,136,153,170,-85,-17,-68,  51,-34,  51,-170, -51,-153,-136 }; // same as above but reverses offset to original
  const float chapters = 17.0; // 15 * 17 = 255

  unsigned char cenc(const unsigned char cVal)
  {
    return (cVal + eTable[((int)ceil(cVal/chapters))-1]); // divide char val by 17, take ceiling, subtract 1 to get index in eTable
  }

  unsigned char cdec(const unsigned char cVal)
  {
    return (cVal + dTable[((int)ceil(cVal/chapters))-1]); // same as eTable, but for decryption
  }

  std::string encrypt(std::string s, std::string k = DEFAULT_KEY)
  {
    std::string sret;
    for(std::string::size_type i=0; i < s.size(); ++i)
    {
      unsigned char ci = s.at(i), ck = k.at(i % (k.size()-1)); // rotate key index for length of string
      sret += (cenc(ci)^ck); // get new value for s[i] from cenc(), XOR with key value from k[i]
    }
    return sret; // return encrypted string
  }

  std::string decrypt(std::string s, std::string k = DEFAULT_KEY)
  {
    std::string sret;
    for(std::string::size_type i=0; i < s.size(); ++i)
    {
      unsigned char ci = s.at(i), ck = k.at(i % (k.size()-1)); // rotate key index for length of string
      sret += cdec(ci^ck); // XOR s[i] with key value from k[i], get original value from cdec();
    }
    return sret; // return decrypted string
  }
};
Although I still don't quite understand the second still (are you changing the char in first half and then position in second half?) this is a lot more help with comments. :)

Also how did you find the dTable numbers to be opposite because they don't appear consistent. (I can tell that this looks like a fair encryption system that also shouldn't give unreliable results such as my current method of tripling the value)
STILL NOT ANSWERED
The problem with "encryption" is that you want to eventually reverse it. This particular thing is pretty rudimentary and probably not all that hard to "crack." It was just a pet project, though. The key selection is based off a modulo to keep the key rotating even if the string length is longer than the key length. I originally made this to encrypt whole files not strings.

Example encrypt:

For the ascii value of 84 ('T')
84 / 17 = 4.94
ceil(4.94) = 5
5-1 = 4 (This is probably redundant as I could have just taken the floor of the divison?)
eTable[4] = 153
84 + 153 = 237
237 XOR 65 ('A' from DEFAULT_KEY[0]) = 172.
So cenc('T') = '¼' (ascii 172)

Example decrypt:

Input value of 172 ('¼')
172 XOR 65 (once again 'A' from DEFAULT_KEY[0]) = 237
237 / 17 = 13.94
ceil(13.94) = 14
14 - 1 = 13
dTable[13] = -153
237 - 153 = 84
so denc('¼' XOR 'A') = 'T' (ascii 84, our original character!)

It's only a one-off from basic XOR but for my purposes it was "enough" lol
Last edited on
Topic archived. No new replies allowed.
Pages: 12