Creating and using a lookup array

P

Protoman

How would you use a lookup table of char*, I mean, how would you find
out where the intersection of D and E is? And how would you read two
strings, and parse each character. Here's the array:

const char* vigenereTable=new char[26][26];
*vigenereTable=,Q
{
{A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z},
{B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A},
{C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B},
{D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C},
{E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D},
{F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E},
{G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F},
{H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G},
{I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H},
{J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I},
{K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J},
{L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K},
{M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L},
{N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M},
{O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N},
{P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O},
{Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P},
{R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q},
{S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R},
{T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S},
{U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T},
{V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U},
{W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V},
{X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W},
{Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X},
{Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y},
};

Can you help me, I want to build a Vigenere cipher program. Thanks for
the help!!!
 
R

Ron Natalie

Protoman said:
How would you use a lookup table of char*, I mean, how would you find
out where the intersection of D and E is? And how would you read two
strings, and parse each character. Here's the array:

const char* vigenereTable=new char[26][26];
*vigenereTable=,Q

If you assume that all your letters are sequential integral
values (true for ASCII, but not all character sets, EBCDIC
for example has a couple of gaps (betwee I and J and R and
S for quaint historical reasons), there are better ways
to do this sort of cipher.

Second, if the table is always going to be 26x26, there's
no point in using dynamic allocation:

char vignereTable[26][26]
{
{A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z},
{B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A},
The above will work except A isn't defined. If you want
to use literal character values you need apostrophes around
it:
'A', 'B', ...
 
O

osmium

Protoman said:
How would you use a lookup table of char*, I mean, how would you find
out where the intersection of D and E is? And how would you read two
strings, and parse each character. Here's the array:

const char* vigenereTable=new char[26][26];
*vigenereTable=,Q

What's the comma for? Doesn't your compiler complain?
{
{A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z},

How do you expect to fit 51 characters into a 26 element array?
{B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A},
{C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B},
{D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C},
{E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D},
{F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E},
{G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F},
{H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G},
{I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H},
{J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I},
{K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J},
{L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K},
{M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L},
{N,O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M},
{O,P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N},
{P,Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O},
{Q,R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P},
{R,S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q},
{S,T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R},
{T,U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S},
{U,V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T},
{V,W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U},
{W,X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V},
{X,Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W},
{Y,Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X},
{Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y},
};

Can you help me, I want to build a Vigenere cipher program. Thanks for
the help!!!

If you can manage to get your intent into a program that compiles, D is the
4th letter of the alphabet and E is the 5th. Adjusting for the fact that
C++ arrays start at 0, *something like* table[3][4];

As has already been pointed out, you are doing this the hard way for most
practical purposes.
 
P

Protoman

Here's the new lookup table:

namespace
{
const char vigenereTable[26][26]=
{
{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'},
{'B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A'},
{'C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B'},
{'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C'},
{'E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D'},
{'F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E'},
{'G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F'},
{'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G'},
{'I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H'},
{'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I'},
{'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J'},
{'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K'},
{'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L'},
{'N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M'},
{'O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N'},
{'P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N',O'},
{'Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P'},
{'R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q'},
{'S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R'},
{'T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S'},
{'U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T'},
{'V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U'},
{'W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V'},
{'X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W'},
{'Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X'},
{'Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y'},
};
}

How do I find the intersection of, say, D and O? And how to I parse
induvidual characters from a string?
 
O

osmium

Protoman said:
OK the, what's the easy way?

The following was posted about a week ago and it was directed at you.
Please read it and try to get a little meaning out of the post. Please post
context in your messages if you expect any help from me. WTF means ::"what
the ****" in case it was too subtle for you.
____

Protoman said:
That's an ebook I have to purchase; I'd like a link to a place that's
free.

Do you have any idea what I see on my screen? I see:

"That's an ebook I have to purchase; I'd like a link to a place that's
free."

How in hell is anyone supposed to know WTF you are talking about?

In the off chance you are talking to me, I suggest this. Actually CLICK on
the link. Don't hypothesize what might happen if you click, actually CLICK.
Can you do that? Will you do that?

Jeez!

-- Osmium
 
S

Sherm Pendley

Protoman said:
OK the, what's the easy way?

The easy way for what? Please learn to quote enough of the message you're
replying to for your own message to make sense.

sherm--
 
S

Sherm Pendley

Protoman said:
For what Osminium said!!!

That's not even a sentence. What are you talking about? I have no idea what
"Osminium" said.

Please - learn to use Usenet correctly. When you reply to someone's post, you
need to quote enough of what they said, like I quoted your post above. That
way, if the post you're replying to hasn't arrived at my server yet, or has
expired and been deleted, your post will make sense.

sherm--
 
R

Ron Natalie

Protoman said:
Here's the new lookup table:
How do I find the intersection of, say, D and O? And how to I parse
induvidual characters from a string?

What do you mean intersection?

You access the characers in a string either with an iterator
or with the [ ] operator and some counter.
 
G

Greg

Ron said:
Protoman said:
Here's the new lookup table:
How do I find the intersection of, say, D and O? And how to I parse
induvidual characters from a string?

What do you mean intersection?

You access the characers in a string either with an iterator
or with the [ ] operator and some counter.

Given a secret key, "SECRET" and a message "MESSAGE" to encrypt, the
program would need to look up the following row & column intersections
in the Vigeneres table: {S, M}, {E, E}, {C, S}, {R, S}, {E, A}, {T,
G}, {S, E} to find how each letter should be encoded.

The program should first "normalize" the key and message inputs,
uppercasing lower case letters, rejecting any character that is not a
letter. Then it can call a function like the one below to find each
letter in the encoded message:

char EncodeLetter( char inMessageChar, char inSecretChar)
{
assert( inMessageChar >= 'A' and inMessageChar <= 'Z');
assert( inSecretChar >= 'A' and inSecretChar <= 'Z');

return vigenereTable[inSecretChar - 'A'][inMessageChar - 'A'];
}

For instance, the message in the above example would be encoded as:
EIUJEZW.

Greg
 
P

Protoman

Greg, here's the main funct of my new code:

int main()
{
string cleartext;
string key;
cout << "Enter the cleartext[10 char max]: " << endl;
getline(cin,cleartext,'\n');
cout << "Enter the key[10 char max]: " << endl;
getline(cin,key,'\n');
for(int i=0,int j=0,int k=0;
i<cleartext.length(),j<key.length;i++,j++,k++)
{
string encrypt[k]=vigenereTable[cleartext-'A'][key[j]-'A'];
}
system("PAUSE");
return 0;
}

It doesn't compile; could you help me, please? Thanks!!!
 
S

Sherm Pendley

Protoman said:
I'll try that.

You'll try what???

Please learn to post correctly. Your message makes no sense, because you
didn't bother quoting any of the message you're replying to.

sherm--
 
J

John Harrison

Protoman said:
Greg, here's the main funct of my new code:

int main()
{
string cleartext;
string key;
cout << "Enter the cleartext[10 char max]: " << endl;
getline(cin,cleartext,'\n');
cout << "Enter the key[10 char max]: " << endl;
getline(cin,key,'\n');
for(int i=0,int j=0,int k=0;

for (int i=0, j=0, k=0;
i<cleartext.length(),j<key.length;i++,j++,k++)

i<cleartext.length() && j<key.length(); i++, j++, k++)

(at a guess)
{
string encrypt[k]=vigenereTable[cleartext-'A'][key[j]-'A'];
}
system("PAUSE");
return 0;
}

It doesn't compile; could you help me, please? Thanks!!!


It might compile now, but I don't think it is going to do what you want.
The loop looks completely wrong.

john
 
G

Greg

Protoman said:
Greg, here's the main funct of my new code:

int main()
{
string cleartext;
string key;
cout << "Enter the cleartext[10 char max]: " << endl;
getline(cin,cleartext,'\n');
cout << "Enter the key[10 char max]: " << endl;
getline(cin,key,'\n');
for(int i=0,int j=0,int k=0;
i<cleartext.length(),j<key.length;i++,j++,k++)
{
string encrypt[k]=vigenereTable[cleartext-'A'][key[j]-'A'];
}
system("PAUSE");
return 0;
}

It doesn't compile; could you help me, please? Thanks!!!


At least this is getting somewhere. I would recommned first thinking
about what this program has to do, and then how it will do it. Writing
a lot of code without a clear idea how to the solve problem can waste a
lot of time.

The program needs to encode the cleartext by substituting all of its
letters with encoded ones. Once the program has reached the end of the
clear text, it is done. So the program needs to loop from the first to
the last character of the cleartext.

std::string encrypted;

for (int = 0; i < cleartext.length(); i++)
{
encrypted += [cleartext-'A'][key[ ?? ] - 'A'];

For output, the program will append each encoded character to
encrypted, a std::string. A more difficult problem is how to specify
the index for the key. If the key is as long or longer than the
message, then the index for the key and message would always be equal.
But if the key is shorter than the message, it has to "cycle": return
to the first character after the last one as been reached. Note that
there is still only one loop being executed, it's only the value of the
key's index that can vary from the cleartext's index i.

So how does the program calculate the index for key? One way is to
declare another index, j, increment j and when j >= key.length() set j
back to 0. That works, but a more streamlined approach could calculate
the key's index from i, and would not need another variable:

encrypted += [cleartext-'A'][key[ i % key.length()]-'A'];

The modulo operator, %, forces the key's index to "wrap around" should
the end of the key text be reached before the full message has been
encoded.

The program still needs more work, especially validating its input: if
the user types a lower case letter, make it upper case. For any other
character just add it to the encrypted message, as is. As the program
stands now, unless the user types only capital letters, it is likely to
crash.

Greg
 
P

Protoman

OK, I'm trying to write a funct to decipher the code, but it keeps
reencrypting it:

string decipher(const string& ciphertext,const string& key)
{
string decrypted;
for(int i=0; i< key.length();i++)
decrypted+=vTable[key-'A'][ciphertext[i%ciphertext.length()]-'A'];
return decrypted;
}

Can you help me? Here's the enciphering funct:

string encipher(const string& cleartext,const string& key)
{
string encrypted;
for(int i=0; i< cleartext.length();i++)
encrypted+=vTable[cleartext-'A'][key[i%key.length()]-'A'];
return encrypted;
}

Any ideas as to what's wrong? It encipher correctly, but it deciphers
wrong. Thanks!!!
 
G

Greg

Protoman said:
OK, I'm trying to write a funct to decipher the code, but it keeps
reencrypting it:

string decipher(const string& ciphertext,const string& key)
{
string decrypted;
for(int i=0; i< key.length();i++)
decrypted+=vTable[key-'A'][ciphertext[i%ciphertext.length()]-'A'];
return decrypted;
}

Can you help me? Here's the enciphering funct:

string encipher(const string& cleartext,const string& key)
{
string encrypted;
for(int i=0; i< cleartext.length();i++)
encrypted+=vTable[cleartext-'A'][key[i%key.length()]-'A'];
return encrypted;
}

Any ideas as to what's wrong? It encipher correctly, but it deciphers
wrong. Thanks!!!


To encrypt each character in the message originally, the program used
the clear text letter as an index into the table, and then used the key
as an index along the opposing axis to select the encrypted letter.

To decrypt an encrypted letter, the program performs the same two steps
but in reverse order: it first uses the key to select along the first
axis, and then uses the encrypted letter as an index along the second
axis to locate the plaintext letter.

In other words, if the program encodes a letter with this expression:

vTable[cleartext-'A'][key[i%key.length()]-'A'];

it should then decode it with this one:

vTable[key[i%key.length()]-'A'][encrypted-'A'];

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top