444,120 Members | 1,799 Online
Need help? Post your question and get tips & solutions from a community of 444,120 IT Pros & Developers. It's quick & easy.

Can't figure out what the error is here

 P: n/a Can you please help me figure out what the error is here, in this rotor cipher simulator, which I wrote to amuse myself? The runtime error is that it isn't symmetrical --decrypting a piece of ciphertext w/ the encrypting key doesn't result in the original message--, which I'm assuming is linked to a coding error. I've spent hours pouring over this code, and I can't find anything. I must be missing something...something subtle. Here's the code: Enigma.hpp ------------------------------- #ifndef ENIGMA_HPP #define ENIGMA_HPP #include #include using namespace std; class Error{}; class Rotor { public: Rotor(char pos=0):CurPos(0),steps(0) { memcpy(Alphabet,"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456 789",36); SetRotorPosition(CharacterMap(pos)); } int GetSteps()const{return steps;} static int CharacterMap(char the_char) { static const char *alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; const char* p; if (p=strchr(alphabet, the_char)) return (p-alphabet); else throw Error(); } void SetRotorPosition(int NewPos) { while(NewPos < 0) { NewPos+=36; } CurPos=NewPos%36; steps=0; } void AdvanceRotor(int Steps) { CurPos += Steps; while(CurPos < 0) { CurPos +=36; } CurPos %=36; steps+=Steps%36; } void ReverseRotor(int Steps) { AdvanceRotor(-Steps); } char GetCurrentCharacter()const { return Alphabet[CurPos]; } char GetCharacterIndex(int Index)const { return Alphabet[(CurPos+Index)%36]; } char GetCharacterInverse(int i)const { if (i>=CurPos) return Alphabet[i - CurPos]; else return Alphabet[36 + i - CurPos]; } private: char Alphabet[36]; int CurPos; int steps; }; class Enigma { public: Enigma(char r1,char r2,char r3,char r4,char r5,char r6,char r7,char r8,char r9,char r10): R1(r1),R2(r2),R3(r3),R4(r4),R5(r5),R6(r6),R7(r7),R 8(r8),R9(r9),R10(r10) { memcpy(Reflector,"COAYIWV7E3809TBUZ26NPGF4DQL5RJX1 SHKM",36); } ~Enigma(){} string Encrypt(const string& cleartext); string Decrypt(const string& ciphertext); private: char plugboard(char input); Rotor R1; Rotor R2; Rotor R3; Rotor R4; Rotor R5; Rotor R6; Rotor R7; Rotor R8; Rotor R9; Rotor R10; char Reflector[36]; }; #endif ------------------------- Enigma.cpp ------------------------- #include "Enigma.hpp" #include using namespace std; char Enigma::plugboard(char Char) { if(Char=='A') return '0'; else if(Char=='B') return 'Q'; else if(Char=='C') return 'W'; else if(Char=='D') return 'E'; else if(Char=='E') return 'D'; else if(Char=='F') return 'T'; else if(Char=='G') return 'Y'; else if(Char=='H') return 'U'; else if(Char=='I') return 'I'; else if(Char=='J') return 'O'; else if(Char=='K') return 'P'; else if(Char=='L') return 'S'; else if(Char=='M') return 'N'; else if(Char=='N') return 'M'; else if(Char=='O') return 'J'; else if(Char=='P') return 'K'; else if(Char=='Q') return 'B'; else if(Char=='R') return 'Z'; else if(Char=='S') return 'L'; else if(Char=='T') return 'F'; else if(Char=='U') return 'H'; else if(Char=='V') return 'X'; else if(Char=='W') return 'C'; else if(Char=='X') return 'V'; else if(Char=='Y') return 'G'; else if(Char=='Z') return 'R'; else if(Char=='0') return 'A'; else if(Char=='1') return '9'; else if(Char=='2') return '8'; else if(Char=='3') return '7'; else if(Char=='4') return '6'; else if(Char=='5') return '5'; else if(Char=='6') return '4'; else if(Char=='7') return '3'; else if(Char=='8') return '2'; else if(Char=='9') return '1'; } string Enigma::Encrypt(const string& cleartext) { string ciphertext; ciphertext.resize(cleartext.size()); unsigned int i=0; for(;i #include using namespace std; int main() { start: bool mode; cout << "Encrypt or decrypt?[Encrypt=1|Decrypt=0]: " << endl; cin >mode; try { if(mode==1) { cout << "Enter the rotor settings: " << endl; char R1,R2,R3,R4,R5,R6,R7,R8,R9,R10; cin >R1 >R2 >R3 >R4 >R5 >R6 >R7 >R8 >R9 >R10; Enigma encryptor(R1,R2,R3,R4,R5,R6,R7,R8,R9,R10); cin.ignore(1); string cleartext; cout << "Enter cleartext: " << endl; getline(cin,cleartext); string ciphertext(encryptor.Encrypt(cleartext)); cout << "Ciphertext: " << ciphertext << endl; } if(mode==0) { cout << "Enter the rotor settings: " << endl; char R1,R2,R3,R4,R5,R6,R7,R8,R9,R10; cin >R1 >R2 >R3 >R4 >R5 >R6 >R7 >R8 >R9 >R10; Enigma decryptor(R1,R2,R3,R4,R5,R6,R7,R8,R9,R10); cin.ignore(1); string ciphertext; cout << "Enter ciphertext: " << endl; getline(cin,ciphertext); string cleartext(decryptor.Encrypt(ciphertext)); cout << "Cleartext: " << cleartext << endl; } goto start; } catch(Error& obj) { cout << "Error. \a" << endl; goto start; } return 0; } ------------------------------------------- BTW, how secure do you believe this cipher to be, bitwise? Thank you ever so much! Oct 9 '07 #1
4 Replies

 P: n/a On Tue, 09 Oct 2007 04:37:05 -0000, Protoman wrote: Can you please help me figure out what the error is here, in this rotor cipher simulator, which I wrote to amuse myself? The runtime error is that it isn't symmetrical --decrypting a piece of ciphertext w/ the encrypting key doesn't result in the original message--, which I'm assuming is linked to a coding error. I've spent hours pouring over this code, and I can't find anything. I must be missing something...something subtle. Here's the code: Compiles fine for me. I couldn't figure out how to run it, though: bisqwit@chii:~/en\$ ./a.out Encrypt or decrypt?[Encrypt=1|Decrypt=0]: 1 Enter the rotor settings: 1 fkuu 2 3 4 ^C bisqwit@chii:~/en\$ ./a.out Encrypt or decrypt?[Encrypt=1|Decrypt=0]: 1 Enter the rotor settings: 1 2 3 4 5 6 7 8 9 10 Enter cleartext: Ciphertext: Encrypt or decrypt?[Encrypt=1|Decrypt=0]: ^C bisqwit@chii:~/en\$ bisqwit@chii:~/en\$ ./a.out Encrypt or decrypt?[Encrypt=1|Decrypt=0]: 1 Enter the rotor settings: aiueoaiueo Error. Encrypt or decrypt?[Encrypt=1|Decrypt=0]: ^C So you need to explain. -- Joel Yliluoma - http://bisqwit.iki.fi/ : comprehension = 1 / (2 ^ precision) Oct 9 '07 #2

 P: n/a On 09 Oct 2007 07:34:04 GMT, Joel Yliluoma wrote: On Tue, 09 Oct 2007 04:37:05 -0000, Protoman wrote: >Can you please help me figure out what the error is here, in thisrotor cipher simulator, which I wrote to amuse myself? The runtimeerror is that it isn't symmetrical --decrypting a piece of ciphertextw/ the encrypting key doesn't result in the original message--, whichI'm assuming is linked to a coding error. I've spent hours pouringover this code, and I can't find anything. I must be missingsomething...something subtle. Here's the code: Compiles fine for me. I couldn't figure out how to run it, though: In any case, I think your question has nothing to do with C++ language per se. It's a "find a bug in my program" type question, which may be better suited for a group such as comp.programming. However, I find a lot of numbered variables in your code. Are you sure that an array and a loop won't do? It could greatly simplify your code and improve your chances of finding the actual error. -- Joel Yliluoma - http://bisqwit.iki.fi/ : comprehension = 1 / (2 ^ precision) Oct 9 '07 #3

 P: n/a [Re-crossposted to clc++ for what I hope are (or will become) obvious reasons, but followups set to comp.programming again] Joel Yliluoma said: On 09 Oct 2007 07:34:04 GMT, Joel Yliluoma wrote: >On Tue, 09 Oct 2007 04:37:05 -0000, Protoman wrote: >>Can you please help me figure out what the error is here, in thisrotor cipher simulator, which I wrote to amuse myself? The runtimeerror is that it isn't symmetrical --decrypting a piece of ciphertextw/ the encrypting key doesn't result in the original message--, whichI'm assuming is linked to a coding error. I've spent hours pouringover this code, and I can't find anything. I must be missingsomething...something subtle. Here's the code: Compiles fine for me. I couldn't figure out how to run it, though: In any case, I think your question has nothing to do with C++ language per se. It's a "find a bug in my program" type question, which may be better suited for a group such as comp.programming. The trouble is that comp.programming will now ask "er, find a bug in *what* program?" -- Richard Heathfield Email: -http://www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Oct 9 '07 #4

 P: n/a On 2007-10-09 06:37, Protoman wrote: Can you please help me figure out what the error is here, in this rotor cipher simulator, which I wrote to amuse myself? The runtime error is that it isn't symmetrical --decrypting a piece of ciphertext w/ the encrypting key doesn't result in the original message--, which I'm assuming is linked to a coding error. Most likely it is a logical error, and not a programming error. In other words you have not created an Enigma cipher, but rather something that at a first glance looks like it but it does not work like Enigma. Go through the logic of the program step by step and make sure that it works like Enigma. BTW, how secure do you believe this cipher to be, bitwise? No idea, probably not very secure by today's standards. For a better answer ask in a cryptography group, such as sci.crypt. -- Erik WikstrĂ¶m Oct 9 '07 #5