473,698 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't figure out what the error is here

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...som ething subtle. Here's the code:

Enigma.hpp
-------------------------------
#ifndef ENIGMA_HPP
#define ENIGMA_HPP
#include <iostream>
#include <string>
using namespace std;
class Error{};
class Rotor
{
public:
Rotor(char pos=0):CurPos(0 ),steps(0)
{
memcpy(Alphabet ,"ABCDEFGHIJKLM NOPQRSTUVWXYZ01 23456789",36);
SetRotorPositio n(CharacterMap( pos));
}
int GetSteps()const {return steps;}
static int CharacterMap(ch ar the_char)
{
static const char *alphabet =
"ABCDEFGHIJKLMN OPQRSTUVWXYZ012 3456789";
const char* p;
if (p=strchr(alpha bet, the_char))
return (p-alphabet);
else
throw Error();
}
void SetRotorPositio n(int NewPos)
{
while(NewPos < 0)
{
NewPos+=36;
}
CurPos=NewPos%3 6;
steps=0;
}
void AdvanceRotor(in t Steps)
{
CurPos += Steps;
while(CurPos < 0)
{
CurPos +=36;
}
CurPos %=36;
steps+=Steps%36 ;
}
void ReverseRotor(in t Steps)
{
AdvanceRotor(-Steps);
}
char GetCurrentChara cter()const
{
return Alphabet[CurPos];
}
char GetCharacterInd ex(int Index)const
{
return Alphabet[(CurPos+Index)% 36];
}
char GetCharacterInv erse(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),R 3(r3),R4(r4),R5 (r5),R6(r6),R7( r7),R8(r8),R9(r 9),R10(r10)
{
memcpy(Reflecto r,"COAYIWV7E380 9TBUZ26NPGF4DQL 5RJX1SHKM",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 <string>
using namespace std;

char Enigma::plugboa rd(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.resi ze(cleartext.si ze());
unsigned int i=0;
for(;i<cleartex t.length();i++)
{
int val=Rotor::Char acterMap(plugbo ard(cleartext[i]));
if (val<36)
{
char val1 = R1.GetCharacter Index(val);
int val2 = Rotor::Characte rMap(val1);
char val3 = R2.GetCharacter Index(val2);
int val4 = Rotor::Characte rMap(val3);
char val5 = R3.GetCharacter Index(val4);
int val6 = Rotor::Characte rMap(val5);
char val7 = R4.GetCharacter Index(val6);
int val8 = Rotor::Characte rMap(val7);
char val9=R5.GetChar acterIndex(val8 );
int val10 = Rotor::Characte rMap(val9);
char val11=R6.GetCha racterIndex(val 10);
int val12=Rotor::Ch aracterMap(val1 1);
char val13=R7.GetCha racterIndex(val 12);
int val14=Rotor::Ch aracterMap(val1 3);
char val15=R8.GetCha racterIndex(val 14);
int val16=Rotor::Ch aracterMap(val1 5);
char val17=R9.GetCha racterIndex(val 16);
int val18=Rotor::Ch aracterMap(val1 7);
char val19=R10.GetCh aracterIndex(va l18);
int val20=Rotor::Ch aracterMap(val1 9);
char val21 = Enigma::Reflect or[val20];
int val22 = Rotor::Characte rMap(val21);
char val23 = R10.GetCharacte rInverse(val22) ;
int val24 = Rotor::Characte rMap(val23);
char val25 = R9.GetCharacter Inverse(val24);
int val26 = Rotor::Characte rMap(val25);
char val27 = R8.GetCharacter Inverse(val26);
int val28 = Rotor::Characte rMap(val27);
char val29 = R7.GetCharacter Inverse(val28);
int val30 = Rotor::Characte rMap(val29);
char val31 = R6.GetCharacter Inverse(val30);
int val32=Rotor::Ch aracterMap(val3 1);
char val33=R5.GetCha racterIndex(val 32);
int val34=Rotor::Ch aracterMap(val3 3);
char val35=R4.GetCha racterIndex(val 34);
int val36=Rotor::Ch aracterMap(val3 5);
char val37=R3.GetCha racterIndex(val 36);
int val38=Rotor::Ch aracterMap(val3 7);
char val39=R2.GetCha racterIndex(val 38);
int val40=Rotor::Ch aracterMap(val3 9);
char val41=R1.GetCha racterIndex(val 40);
ciphertext[i] = plugboard(val41 );
R1.AdvanceRotor (1);
if((R1.GetSteps ()%36)==0)
{
R2.AdvanceRotor (1);
if((R2.GetSteps ()%36)==0)
{
R3.AdvanceRotor (1);
if((R3.GetSteps ()%36)==0)
{
R4.AdvanceRotor (1);
if((R4.GetSteps ()%36)==0)
R5.AdvanceRotor (1);
{
if((R5.GetSteps ()%36)==0)
R6.AdvanceRotor (1);
{
if((R6.GetSteps ()%36)==0)
R7.AdvanceRotor (1);
{
if((R7.GetSteps ()%36)==0)
R8.AdvanceRotor (1);
{
if((R8.GetSteps ()%36)==0)
R9.AdvanceRotor (1);
{
if((R9.GetSteps ()%36)==0)
R10.AdvanceRoto r(1);
}
}
}
}
}
}
}
}
}
else ciphertext[i]=cleartext[i];
}
return ciphertext;
}
---------------------------------------

EnigmaMain.cpp
----------------------------------
#include "Enigma.hpp "
#include <iostream>
#include <string>
using namespace std;
int main()
{
start:
bool mode;
cout << "Encrypt or decrypt?[Encrypt=1|Decry pt=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,cle artext);
string ciphertext(encr yptor.Encrypt(c leartext));
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,cip hertext);
string cleartext(decry ptor.Encrypt(ci phertext));
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 2287
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...som ething 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|Decry pt=0]:
1
Enter the rotor settings:
1
fkuu
2
3
4
^C

bisqwit@chii:~/en$ ./a.out
Encrypt or decrypt?[Encrypt=1|Decry pt=0]:
1
Enter the rotor settings:
1 2 3 4 5 6 7 8 9 10
Enter cleartext:
Ciphertext:
Encrypt or decrypt?[Encrypt=1|Decry pt=0]:
^C

bisqwit@chii:~/en$
bisqwit@chii:~/en$ ./a.out
Encrypt or decrypt?[Encrypt=1|Decry pt=0]:
1
Enter the rotor settings:
aiueoaiueo
Error.
Encrypt or decrypt?[Encrypt=1|Decry pt=0]:
^C

So you need to explain.

--
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision)
Oct 9 '07 #2
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 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...so mething 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.programmin g.

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
[Re-crossposted to clc++ for what I hope are (or will become) obvious
reasons, but followups set to comp.programmin g 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 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...s omething 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.programmin g.
The trouble is that comp.programmin g will now ask "er, find a bug in *what*
program?"

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 9 '07 #4
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2952
by: Professor Frink | last post by:
First off, I apologize if this gets long. I'm simply trying to give you all enough information to help me out. I'm writing (almost finished, actually), my first VB.Net application. It's a forms application that is going to be used to extract data from a legacy system (VSAM based mainframe file structure), and output data in pipe-delimited record layouts, multiple record types per file, one file per chosen client. I have been working on...
102
5672
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler can't tell where a bracket is missing.... a few weeks have past, I requested a feature for the delphi ide/editor "automatic identation of code in begin/end statements etc" and today when I woke up I suddenly released a very simple solution for this...
0
1624
by: William Stacey [MVP] | last post by:
This code worked on fx 1.1 and now I get a "Keyset does not exist" error when trying to SignData with RSA under FX2.0. Smells like some security error, but can't debug it as I think error is thrown in win32. If you create a new RSA, it works. But creating RSA from an SNK does not work. string privSnk = @"v:\wsesimpletcpdll\wsesimpletcpdll.snk"; // Use any snk file. System.Security.Cryptography.RSACryptoServiceProvider rsa =...
3
1355
by: Brian Blais | last post by:
Hello, I have an odd kind of Heisenbug in what looks like a pretty simple program. The program is a progress bar code I got at the Python Cookbook: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/168639 (including the code below)
2
1719
by: kkleung89 | last post by:
Basically, here's what's happening with the program. I have a table of Customers and a table of Pets, with the latter containing a field linking it to its customer of ownership. I have a form with all of the personal information of the customers, and within it is a list box with all of the pets in it. In the current design, it takes that information from a query, but since every customer is different, the query needs to change each...
9
2122
by: Tony Girgenti | last post by:
Hello. I'm developing and testing a web application using VS.NET 2003, VB, .NET Framework 1.1.4322, ASP.NET 1.1.4322 and IIS5.1 on a WIN XP Pro, SP2 computer. I'm using a web form. For a datagrid control, i used the Caption property. It displays fine on the datagrid and allows me to run the program. But, when i view the HTML for the web form, it shows this error at the
2
1452
by: ayman723 | last post by:
hi; I have this code in my book and when I copied it into my compiler into two files, one is header and the other is .cpp. it gives me an error, I'm sure I copied it right but can't figure out the error. here is my code: Time.h // declaration of class time. // membar functions defined in Time.cpp
4
3764
by: d3vkit | last post by:
Okay so I am at a loss here. I have a website that I've previously had no trouble connecting to the mysql DB on. I have an include to a connect file with the relevant connection info, and it was working fine until today. I am trying to implement some ajax with the javascript framework mootools (although I don't see how this is causing the problem it started happening right around this time sooo...) I am sending info from my login form to the...
8
5362
by: =?Utf-8?B?R3JlZyBMYXJzZW4=?= | last post by:
I'm trying to figure out how to modify a panel (panel1) from a backgroundworker thread. But can't get the panel to show the new controls added by the backgroundwork task. Here is my code. In this code there is a panel panel1, that I populate with a lable in the foreground. Then when I click on "button1" a backgroundworker thread in async mode is started. When the backgoundworker thread completes the thread returns a panel to populate...
0
8671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9152
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8856
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4360
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2321
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.