Encrypter

Hello im trying to make a encrypter so far and im somewhat of a beginner i like to go for challenges that interest my needs and this is one of them...so my goal is to make the program identify the message a person puts in and turn it into a number sequence like say
a = 1
h = 2

person says ha it reads it and puts the output as 21 so im not sure how to go about this ive used google to a certain extent...also ive tried different methods and so on so can someone plz give me a basic example ty
I'm not sure if the example you gave would really count as a decent encryption. You could acheive this by simply getting the user to input a string, create a lot of if statements eahc on saying "if this letter is present, output this letter..." and then cycling through the string.

You want something more random than that and something that will change depending on letter combination. This is a common one I found before:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
int main()
{
  char string[11]="A nice cat";
  char key[11]="ABCDEFGHIJ";
  for(int x=0; x<10; x++)
  {
    string[x]=string[x]^key[x];
    cout<<string[x];
  }
  return 0;
}
Cryptography is imho one of the most demanding parts of programming. It is very difficult to create a good cipher without the necessary mathematical background.

If yuo want to play around a little for yourself you could maybe start with a simple, yet quite effective stream cipher called RC4. an example you can find here: http://www-staff.lboro.ac.uk/~lbjlc/Publications/Tutorials/rc4.txt
You find a list of the most known stream cyphers on wikipedia. Three examples of a little more advanced, but also recommendable stream ciphers are AES, twofish and serpent.
Sigh.

To do the kind of substitution cipher ( http://en.wikipedia.org/wiki/Substitution_cipher ) you want, you simply need a look-up table that translates a letter to a value.

For example:
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
#include <cctype>
#include <iostream>
#include <string>
using namespace std;

char crypt_lookup[ 26 + 1 ] = "qwertyuiopasdfghjklzxcvbnm";

char e( char c )
  {
  if (isalpha( c ))
    c = crypt_lookup[ toupper( c ) - 'A' ];
  return c;
  }

int main()
  {
  string s;
  cout << "Please enter a phrase to encrypt> " << flush;
  getline( cin, s );

  for (unsigned n = 0; n < s.length(); n++)
    s[ n ] = e( s[ n ] );

  cout << "The encrypted phrase is> " << s << endl;

  return 0;
  }

You can use the same lookup table to decrypt, except now you are first looking for the letter then returning the (index + 'a') when found.

Keep in mind that a substitution cipher is very easy to break. If you encrypt to more than one letter per source character, then you will have to make sure that there aren't any collisions in the output letter stream -- or just do something like put a space between letters...

Anyway, hope this helps.
I must say I am a cryptofreak. I've been working on various programs as a hobby for some time now. I was a little perplexed by the above sample however and when I gave it a shot I could not get it to decrypt at first, even when changing the -A to a +A. Its a bit counterintuitive I think, the code could be a lot simpler.

I think this code might be a little more easier to understand for a beginner:

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
#include <iostream>
#include <string>

using namespace std;

short option = 3;
int opt();
char key[27] = "qwertyuiopasdfghjklzxcvbnm";
char lookup[27] = "abcdefghijklmnopqrstuvwxyz";

char enc(char c);
char dec(char c);


int main()
{
    string s;
    opt();
    if (option == 0)
        {
            cout << "encrypt?  " << endl; getline(cin, s);
            for (int i = 0; i < s.length(); ++i)
                {
                    s[i] = enc(s[i]);
                }
            cout << "ENCRYPTED: " << s << endl;
        }
    if (option == 1)
        {
            cout << "decrypt?  "; getline(cin, s);
            for (int i = 0; i < s.length(); ++i)
                {
                    s[i] = dec(s[i]);
                }
            cout << "DECRYPTED: " << s << endl;
        }
}


int opt()
{
    cout << "Encrypt or decrypt, 0 or 1? " << endl;
    cin >> option; cin.ignore();
    return 0;
}


char enc(char c)
{
    int index = 0; char letter;
    if (isalpha(c))
        {
            c = tolower(c);
            while (c != lookup[index])
                {
                    ++index;
                }
            c = key[index];
        }
    letter = c;
    return letter;
}

char dec(char c)
{
    int index = 0; char letter;
    if (isalpha(c))
        {
            c = tolower(c);
            while (c != key[index])
                {
                    ++index;
                }
            c = lookup[index];
        }
    letter = c;
    return letter;

}


You should have at least a "lookup table" of 256 elements if you are working with characters. Also a dynamic S-Box is much more effective though likewise easy to understand. Additionally you avoid having this loads of iterations since you have to "look up" only one value.
Last edited on
Ah, brought back memories of the Enigma encrypter.

Here's an oversimplified version (1 wheel).

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
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <ctype.h>
#include <iostream>
#include <iterator>
#include <string>

static const char* cipher = "qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm";

char encrypt( char ch )
{
    static const char* encPtr = cipher;
    char res = isalpha( ch ) ? 
        ch - tolower( ch ) + encPtr[ tolower( ch ) - 'a' ] : ch;
    if( ++encPtr - 26 > cipher )
        encPtr = cipher;
    return res;
}

char decrypt( char ch )
{
    static const char* decPtr = cipher;
    char res = isalpha( ch ) ?
        std::distance( decPtr, std::find( decPtr, decPtr + 26, tolower( ch ) ) ) + 'a'
        + ( ch - tolower( ch ) ) : ch;
    if( ++decPtr - 26 > cipher )
        decPtr = cipher;    
    return res;
}

int main() {
    std::string msg = "The quick brown Fox jumped over the lazy Dog.";
    std::string enc;

    std::for_each( msg.begin(), msg.end(), boost::lambda::var( enc ) += 
        boost::lambda::bind( encrypt, boost::lambda::_1 ) );

    std::string dec;
    std::for_each( enc.begin(), enc.end(), boost::lambda::var( dec ) += 
        boost::lambda::bind( decrypt, boost::lambda::_1 ) );

    std::cout << "Msg = " << msg << std::endl
              << "Enc = " << enc << std::endl
              << "Dec = " << dec << std::endl;
}

Last edited on
Sigh.

1
2
3
4
5
6
7
#include <cstring>
char d( char c )
  {
  char* i = strchr( crypt_lookup, tolower( c ) );
  if (i) return (i - crypt_lookup + 'a');
  else return c;
  }



An S-box typically is a lookup table.
http://en.wikipedia.org/wiki/S-box

[edit] fixed a typo in the decryption function...
Last edited on
Thanks for the examples everyone and the time you've spent to help me get a grasp or idea of how i am going to go about of doing this thx again
Topic archived. No new replies allowed.