Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Interesting Cryptography program
Index -> Programming, C++ -> C++ Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
md




PostPosted: Tue Oct 18, 2005 10:11 pm   Post subject: Interesting Cryptography program

On the sugestion of wtd, here is an interesting crypto program I wrote; note that this is a cleaned up version that hasn't been tested...

c++:

// Crypt.cpp : Defines the entry point for the console application.
//

#include <string>
#include <iostream>
#include <fstream>

static char keys[52];         // 52 keys as there are 26 upper case and 26 lower case characters

void Encrypt(char *in)
{
        const int length = strlen(in);
        for(int i = 0; i < length; i++)
        {
                int key = -1;
                if( (in[i] >= 'A') && (in[i] <= 'Z') )
                        key = in[i] - 'A';                                          // first 26 keys
                else if( (in[i] >= 'a') && (in[i] <= 'z') )
                        key = in[i] - 'a' + 26;      // next 26 keys

                if(key >= 0)
                {
                        in[i] = keys[key];
                        keys[key]++;

                        if( (keys[key] > 'Z') && (keys[key] < 'a'))
                                keys[key] = 'a';
                        else if( keys[key] > 'z')
                                keys[key] = 'A';
                }
        }
}


void InitKeys(void)
{
        for(int i = 0; i < 26; i++)
                keys[i] = 'A' + i;

        for(int i = 26; i < 52; i++)
                keys[i] = 'a' + (i-26);
}

int main(int argc, char **argv)
{
        InitKeys();

        // the file name from the first argument
        char *in_file = argv[1];                // since 0 is the program name, 1 should be the filename (no error checking here :P)
        std::string out_file(in_file);
        out_file += ".encoded.txt";

        // open the input and output files
        std::ifstream input(in_file);
        std::ofstream output(out_file.c_str());

        // now just read the entire input file, encode it, and output it.
        while(!input.eof())
        {
                char buffer[258];
                input.read(buffer,256);
                buffer[257] = '\0';          // just to be sure
                Encrypt(buffer);
                output << buffer;


        }

        input.close();
        output.close();

        return 0;
}


Sorry there is no explanation... I'll post one as soon as I can figure out how to describe it without breaking people's minds... oh and if you can figure out how to decrypt it that'd be cool too Razz

I've updated it so that it actually compiles correctly, thanks wtd
It's been updated once more to the version that actually does what it's supposed to...
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Oct 18, 2005 10:20 pm   Post subject: (No subject)

Cleaned up a bit further.

c++:
#include <string>
#include <iostream>
#include <fstream>

static char keys[52];                        // 52 keys as there are 26 upper case and 26 lower case characters

void Encrypt(char *in)
{
        const int length = strlen(in);

        for (int i = 0; i < length; i++)
        {
                int key = -1;

                if (in[i] >= 'A' && in[i] <= 'Z')
                        key = in[i] - 'A';                                                        // first 26 keys
                else if (in[i] >= 'a' && in[i] <= 'z')
                        key = in[i] - 'a' + 26;                                                // next 26 keys

                if (key >= 0)
                {
                        in[i] = keys[key];
                        keys[key]++;

                        if (keys[key] > 'Z' && keys[key] < 'a')
                                keys[key] = 'a';
                        else if (keys[key] > 'z')
                                keys[key] = 'A';
                }
        }

}

void InitKeys(void)
{
        for (int i = 0; i < 26; i++)
                keys[i] = 'A' + i;

        for (int i = 26; i < 52; i++)
                keys[i] = 'a' + i;
}

int main(int argc, char **argv)
{
        InitKeys();

        // the file name from the first argument
        char *in_file = argv[1];                // since 0 is the program name, 1 should be the filename (no error checking here Razz)
        std::string out_file(in_file);
        out_file += ".encoded.txt";

        // open the input and output files
        std::ifstream input("test.txt");
        std::ofstream output;

        // now just read the entire input file, encode it, and output it.
        while(!input.eof())
        {
                char buffer[258];
                input >> buffer;
                buffer[257] = '\0';                // just to be sure
                Encrypt(buffer);
                output << buffer;
        }

        input.close();
        output.close();

        return 0;
}
md




PostPosted: Tue Oct 18, 2005 10:46 pm   Post subject: (No subject)

Ok, so here is how it works... try not to get confused since I'm not so good at explaining Razz

First we start with out string, it can be anything, however only A-Z,a-z will be encoded. We start off with each letter in the alphabet being associated with a key. This key is initially the same letter. As we move through the string we replace the character with the key associated with teh character, and then increment the key. The keys are A-Z,a-z.

So if we have a string AAA the first A is replaced with A, the second with B, the thrird with C. Each character has it's own key, so a message that has no repeating character will not change. This can be fixed by using a starting order for the keys that doesn't match, ei. the first A would have a value of say F, B could start as D, etc.

An example of the encoding:

Input wrote:
This is a test of teh encoding algorythm, it may seem like it's doing nothing
at first ut as you progress through the text it just get's more and more random


and the encrypted message:
Output wrote:

This jt a teuu of vfi gncpdkog blhqrywjm, lx ncz vhio mmkj ny'w eropi qszkprj
dA gqsxB uC ey Atv ptukukzA Dlvvwlm Enl FmxG rH jxBI mnJ'C pwwo fsf qxxp ygtgyr


Here is the actual code encrypted:
Encrypted code wrote:

// Crypt.cqr : Definfs uhg hovsz sojpw gpt xii dqqtrlj atumkebylsr.
//

#msfnudk <uzuntg>
#ougovel <ptvAvmcm>
#qvhpwfn <hwBwodn>

xCeDri jjfx kpAy[52]; // 52 lqBz gA Ekrys hzt 26 xvwuA kiBv jwg 26 quwwB lkCx mllCmnFyDD

vvsh ExoECxG(pmnF *ty)
{
qwzEH uAÃŒI rzBhJn = FKGsAC(vD);
ixH(wEL x = 0; y < tBFiMo; z++)
{
AGN mCD = -1;
Bj( (CH[D] >= 'A') && (EI[F] <= 'Z') )
nDE = GJ[H] - 'B'; // kIIGO 26 oEFH
FuIG Jl( (KK[L] >= 'o') && (ML[N] <= 'z') )
pHG = OM[P] - 'p' + 26; // NIxP 26 qJHJ

Ì Qm(rKI >= 0)
{
RO[S] = sLJK[tMK];
uNLL[vOM]++;

Tn( (wPNM[xQO] > 'a') && (yRPN[zSQ] < 'q'))
ATRO[BUS] = 'r';
VvPW Uo( CXTQ[DYU] > 'A')
EZVR[FaW] = 'C';
}
}
}


wyVi IPWQKbXS(xzXj)
{
pAJ(YQR Z = 0; a < 26; b++)
GcYT[c] = 'D' + ÃŒd;

qBK(eRS f = 26; g < 52; h++)
HdZU[i] = 's' + (j-26);
}

kST otlT(mUU uLjr, spvM **wNky)
{
JVnVLeaV();

// Wqf rowg Wxph sOCq Xri tpPWY yQlyrjXZ
tszR *qY_urxk = ASmz[1]; // XsZul 0 tY atm yTDnUBs aCtn, 1 ZuEzyk bo bvp vuzqbDur (cF sVWGX vwtwIvdo ÃŒxuYv Razz)
acl::bdZwep HAe_wxAw(yf_xzBx);
IBf_yACy += ".zgxJmAn.gyh";

// KzBh iyC BiACj Ejo LDkBEl zCDDc
dmp::DAenaEFv EkCFo(Fl_BGEF);
fpq::MCgqbGGw NGrDHs(OIt_DHFH.y_huc());

// mPx jJiv dIHr wzJ KnxIeL JoEKy EKGM, NpzQsO Lz, Iqt RLAFMB MC.
yANHP(!OÃŒrGND.QSF())
{
ABJf cOGHRg[258];
PsHPE.hSKu(dQIJTi,256);
eRKLUj[257] = '\0'; // kSjF GT fV kTkW
FtBlbIH(gUMNXm);
UVIJWJ << hXOPYn;


}

QuKYK.CIVlZ();
WZLLaM.DJXma();

obNbpv 0;
}



**note that hte actual program has some problems with text that isn't a power of 256... but only because it adds giberish to the end...
codemage




PostPosted: Wed Oct 19, 2005 8:45 am   Post subject: (No subject)

That's a bit ENIGMA-esque.
beard0




PostPosted: Wed Oct 19, 2005 2:15 pm   Post subject: Re: Interesting Cryptography program

Cornflake wrote:
oh and if you can figure out how to decrypt it that'd be cool too Razz


Impossible:

AAAAAAAAAAAAAAAAAAAAAAAAAA Encrypted = ABCDEFGHIJLKMNOPQRSTUVWXYZ

ABCDEFGHIJLKMNOPQRSTUVWXYZ Encrypted = ABCDEFGHIJLKMNOPQRSTUVWXYZ

So, what is ABCDEFGHIJLKMNOPQRSTUVWXYZ decrypted? Shocked
Naveg




PostPosted: Wed Oct 19, 2005 2:56 pm   Post subject: (No subject)

The decrypting program would have to recognize a set of rules.

First, similar to the encrypting program, we give each character an individual counter, starting at its own value, with A being 0.

For example, if the encrypted message begins with n consecutive characters, you know that the original message consisted of the first encrypted character n times. Once the characters are no longer consecutive you could check for what the next character is by keeping track of where the counter for each character currently is.

Take the first five characters of our alphabet example:

AAAAAB becomes ABCDEB

Now lets decode ABCDEB:

ABCDE are all consecutive, so we know the original message started with 5 A's. The next letter is a B. Since the 'A' counter is now at F (5), we know this cannot be an A. And since no other counters have been initialized (ie. no other characters have been used) we must start a new counter at 'B', Using this new counter,'B' becomes 'B'.

What if we added another A to the original message?

AAAAABA would become ABCDEBF.

Decode ABCDEBF

Using the same technique, the first five letters are again all A's, and the sixth is B. What about the F? Well we test the existing counters to see if any are currently in the 'F' position, and sure enough, the 'A' counter is. So the seventh letter in our coded message becomes yet another A.

A problem with this logic occurs when multiple counters are in the same position:

AAADE would encrypt to ABCDE, but our decoder would decode it as AAAAA.

Essentially, this is not a decryptable encryption method.
beard0




PostPosted: Wed Oct 19, 2005 3:13 pm   Post subject: (No subject)

Naveg: Did you read my post?

The one thing that could be done decryption-wise would be to assume that the text is written in a certain lanugage, and use a dictionary to find the most likely fit, though this would be far from foolproof, and would die a painful death when confronted with something such as the c++ code you encoded, and still more painful a death when confronted with the compiled encoded program.
Naveg




PostPosted: Wed Oct 19, 2005 3:18 pm   Post subject: (No subject)

beard0 wrote:
Naveg: Did you read my post?


Yes I did, I was just making it clear that the decoding is only flawed when two counters have the same value. More information has to be sent along with the encoded message that enables the decoder to choose the correct character when there are multiple choices.
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Wed Oct 19, 2005 3:51 pm   Post subject: Re: Interesting Cryptography program

beard0 wrote:
Cornflake wrote:
oh and if you can figure out how to decrypt it that'd be cool too Razz


Impossible:

AAAAAAAAAAAAAAAAAAAAAAAAAA Encrypted = ABCDEFGHIJLKMNOPQRSTUVWXYZ

ABCDEFGHIJLKMNOPQRSTUVWXYZ Encrypted = ABCDEFGHIJLKMNOPQRSTUVWXYZ

So, what is ABCDEFGHIJLKMNOPQRSTUVWXYZ decrypted? Shocked


well cornflake.. your algorithm is unfortunately quite.. ehm useless Embarassed it does not work as a oneway hash algorithm nor does it allow dercyption.. In other words: you did not come up with anything related to "cryptography" Razz

nice try though.
md




PostPosted: Wed Oct 19, 2005 5:29 pm   Post subject: (No subject)

Well... given that I thought it up and wrote it to see what it would do... I didn't expect anything to come of it Razz I'm going to fix it so that it's actually decryptable though, should make it interesting...


and by decryptable I mean you can figure out the original message from the decoded text, assuming you try... it won't decrypt strait...

[edit] I was going to make it semi workable by adding a number representing the number of times the key used had been changed... but that didn't really make it any easier... methinks I'll just leave it as an example of something completely useless, yet kinda interesting.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: