Jump to content

So...I made a cypher. [C/C++]

Nineshadow
#include <ctype.h>int strlen(char *str){    int count = 0;    while(*str != '\0')    {        count++;        str++;    }    return count;}int get_position_alpha(char c){    if(!isalpha©) return -1;    else    {        if(islower©) return c - 'a'+1;        if(isupper©) return c - 'A'+1;        return -1;    }}int abs(int n){    if(n >= 0) return n;    else return -1*n;}void cypher(char *str, int len, int offset){    for(int i = 0; i < len; i++)    {        if(isupper(*(str+i)))        {            *(str+i) = ((26 + ((*(str+i) - int('A')) + (offset % 26))) % 26) + int('A');        }        else if (islower(*(str+i)))        {            *(str+i) = ((26 + ((*(str+i) - int('a')) + (offset % 26))) % 26) + int('a');        }    }void cypher(char *str, int offset){    cypher(str, strlen(str) , offset);}void decypher(char *str, int offset){    cypher(str, -1*offset);}void cypher(char *str, int *key, int key_len){    char *begin = str;    for(int i = 0; i <key_len && *(begin) != '\0'; i++)    {        cypher(begin,abs(*(key+i)), *(key+i));        begin += abs(*(key+i));        if(i==key_len-1) i = 0;    }}void cypher(char *str, char *Key){    int len = strlen(Key);    int key[len];    for(int i = 0; i < len; i++)        key[i] = get_position_alpha(*(Key+i));    cypher(str, key, len);}void decypher(char *str, char *Key){    int len = strlen(Key);    int key[len];    for(int i = 0; i < len; i++)        key[i] = -1*get_position_alpha(*(Key+i));    cypher(str, key, len);}

It supports cstrings, arrays of characters.

Here is what it can do :

  • encode a string with a given offset
  • encode a string with a key, which results in multiple offsets. This key can be either an array of characters, or integers. In case you parse an array of characters, they are then transformed into an array of integers depending on their order in the alphabet. Case-sensitive.
  • decode a string with a given offset
  • decode a string with a given key , again, string of array of integers.

I'm currently working on making an executable which accepts command line arguments and on adding a pseudorandom key generator.

@Ciccioo

For example, this :

 

The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The languages only differ in their grammar, their pronunciation and their most common words. Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators. To achieve this, it would be necessary to have uniform grammar, pronunciation and more common words. If several languages coalesce, the grammar of the resulting language is more simple and regular than that of the individual languages. The new common language will be more simple and regular than the existing European languages. It will be as simple as Occidental; in fact, it will be Occidental. To an English person, it will seem like simplified English, as a skeptical Cambridge friend of mine told me what Occidental is.The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The languages only differ in their grammar, their pronunciation and their most common words. Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators.

with the key "sesquipedalian" , becomes :

 

Max Xnkhixtg etgzntljx fkx fxfuxkl hy max lrdv wrdzcp. Kyvzi nzkvmvoz zsdnozixz db j vhcq. Veh isyudsu, ckxnh, wtsvu, qfo, Qgdabn dbnb cqf goas jcqopizowd. Yax etgzntzxl hger wzwwvi ze kyvzi xivhhvm, oczdm kmjipixdjcrxw jwm jxuyh ceij seccts btvhw. Qhqdkazq dqmurinb fqh o bsk qcaacb zfslzfzx phnew ux wxlbktucv: fev tflcu ivwpnz oj kvt zskzindqz cajwbujcxhi. Je qsxyulu jmnx, mx apgxp nq zqoqebjah cx qbjs ibwtcfa uforrfw, ikhgngvbtmbhg tgw dfiv tfddfe nfiuj. Da nzqzmvg gvibpvbzb lxjunblu, jxu whqccqh ek ymj viwvxfuzs xmzsgmpn rb vxaf gwadzs obr fslzqfw matg matm hy max beuzmzulrc crexlrxzn. Ocz izr xjhhji gvwpdjpn frbb ru cehu iycfbj fsi vikvxmd ftmz ftq ngrbcrwp Sifcdsob zobuifljx. Bm pbee ux tl lbficv rj Fttzuvekrc; di avxo, do rdgg wz Xllrmnwcjb. Je qd Udwbyix ujwxsr, jf iuxx eqqy urtn brvymwtwsr Sbuzwgv, fx f ldximbvte Vtfukbwzv wizveu fw dzev ojgy hz rcvo Jxxdyzioju rb.Cqn Ukhefuqd bqdwkqljx fvi qfynqde ar ftq bjvn ojvjzm. Hvswf gsdowfyj xqblmxgvx bl t frma. Wfi jtzvetv, dlndx, nkjmo, zox, Zpmjyn dbnb cxu iqcu lesqrkbqwd. Yli pbzsgmsqe azxk mroona ro hvswf ufoaaof, ymjbk ikhgngvbtmbhg tgu kyvzi dfjk tfddji rjmyn. Zqzmtjiz mzjurinb fqo q dum secced bfslzfki apgxp nq pqeudjkun: xwn qcizr fstigs ht ufd xqixglbox mktgletmfij.

Pretty decent : b9nhfR6.png

Or, shifted by 3 letters :

 

Wkh Hxurshdq odqjxdjhv duh phpehuv ri wkh vdph idplob. Wkhlu vhsdudwh halvwhqfh lv d pbwk. Iru vflhqfh, pxvlf, vsruw, hwf, Hxursh xvhv wkh vdph yrfdexodub. Wkh odqjxdjhv rqob gliihu lq wkhlu judppdu, wkhlu surqxqfldwlrq dqg wkhlu prvw frpprq zrugv. Hyhubrqh uhdolchv zkb d qhz frpprq odqjxdjh zrxog eh ghvludeoh: rqh frxog uhixvh wr sdb hashqvlyh wudqvodwruv. Wr dfklhyh wklv, lw zrxog eh qhfhvvdub wr kdyh xqlirup judppdu, surqxqfldwlrq dqg pruh frpprq zrugv. Li vhyhudo odqjxdjhv frdohvfh, wkh judppdu ri wkh uhvxowlqj odqjxdjh lv pruh vlpsoh dqg uhjxodu wkdq wkdw ri wkh lqglylgxdo odqjxdjhv. Wkh qhz frpprq odqjxdjh zloo eh pruh vlpsoh dqg uhjxodu wkdq wkh halvwlqj Hxurshdq odqjxdjhv. Lw zloo eh dv vlpsoh dv Rfflghqwdo; lq idfw, lw zloo eh Rfflghqwdo. Wr dq Hqjolvk shuvrq, lw zloo vhhp olnh vlpsolilhg Hqjolvk, dv d vnhswlfdo Fdpeulgjh iulhqg ri plqh wrog ph zkdw Rfflghqwdo lv.Wkh Hxurshdq odqjxdjhv duh phpehuv ri wkh vdph idplob. Wkhlu vhsdudwh halvwhqfh lv d pbwk. Iru vflhqfh, pxvlf, vsruw, hwf, Hxursh xvhv wkh vdph yrfdexodub. Wkh odqjxdjhv rqob gliihu lq wkhlu judppdu, wkhlu surqxqfldwlrq dqg wkhlu prvw frpprq zrugv. Hyhubrqh uhdolchv zkb d qhz frpprq odqjxdjh zrxog eh ghvludeoh: rqh frxog uhixvh wr sdb hashqvlyh wudqvodwruv.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

As someone who doesn't code, answer to this:

 

4054c9dfdce4b53c094010582496c2bb8e7a8944

aka me

Link to comment
Share on other sites

Link to post
Share on other sites

Don't know what I've done, but Bitdefender now won't let me run it D:

 

God damn it Bitdefender! What the hell...

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Don't know what I've done, but Bitdefender now won't let me run it D:

 

God damn it Bitdefender! What the hell...

 

Antivirus is uesless these days.

 

For your program, there are 52 possible outputs. Just use modulo to effect the wrapping of the shift, then map each of the resultant 0-51 indices to the ascii codes. A character array of the alphabet in ascii order ought to do it.

Link to comment
Share on other sites

Link to post
Share on other sites

Try this:

 

void cypher(char *str, int len, int offset){	for(int i = 0; i < len; i++)	{		if(isupper(*(str+i)))		{			*(str+i) = ((26 + ((*(str+i) - int('A')) + (offset % 26))) % 26) + int('A');		}		else if (islower(*(str+i)))		{			*(str+i) = ((26 + ((*(str+i) - int('a')) + (offset % 26))) % 26) + int('a');		}	}}
I didn't test it but it "should" do the same thing without tripping up on large offsets, it might screw up if you overflow an int but the encode and decode should remain the same so should still work... I think...
Link to comment
Share on other sites

Link to post
Share on other sites

Try this:

 

void cypher(char *str, int len, int offset){	for(int i = 0; i < len; i++)	{		if(isupper(*(str+i)))		{			*(str+i) = ((26 + ((*(str+i) - int('A')) + (offset % 26))) % 26) + int('A');		}		else if (islower(*(str+i)))		{			*(str+i) = ((26 + ((*(str+i) - int('a')) + (offset % 26))) % 26) + int('a');		}	}}
I didn't test it but it "should" do the same thing without tripping up on large offsets, it might screw up if you overflow an int but the encode and decode should remain the same so should still work... I think...

 

Yup. that works.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

how about a feature that tells you the strength of the encryption key? the "strength" of the key would be based on how even is the character distribution of the encoded text

Link to comment
Share on other sites

Link to post
Share on other sites

how about a feature that tells you the strength of the encryption key? the "strength" of the key would be based on how even is the character distribution of the encoded text

It's something I had planned for it. Will implement that eventually.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

after a quick read (i might be wrong) it sims to be a Vingenère ciphe.

the strength from this ciphe comes only from the length of the key. if you have a long enough message you can still try to atack it using stadistical analysis of the letters trying different lenghts of key. It's also possible to try and look for common groupings of 3 or 4 letters and comparing them to the groups of letters in the lenguaje of the message.

 

however, since you are actually mantaining the spaces and every punctuation sing, it would be possible to try and get different short words and match them with the message. common words such as "a", "and" or "the" appear several times in the text. the distance between the letters is a good revelation and once you've got the groups, finding the common divisors would give the length of the key

 

 

 

p.s: sorry for the critics, it's just my obsesion to look for faults on ciphers

The best way to measure the quality of a piece of code is "Oh F*** "s per line

Link to comment
Share on other sites

Link to post
Share on other sites

after a quick read (i might be wrong) it sims to be a Vingenère ciphe.

the strength from this ciphe comes only from the length of the key. if you have a long enough message you can still try to atack it using stadistical analysis of the letters trying different lenghts of key. It's also possible to try and look for common groupings of 3 or 4 letters and comparing them to the groups of letters in the lenguaje of the message.

 

however, since you are actually mantaining the spaces and every punctuation sing, it would be possible to try and get different short words and match them with the message. common words such as "a", "and" or "the" appear several times in the text. the distance between the letters is a good revelation and once you've got the groups, finding the common divisors would give the length of the key

 

 

 

p.s: sorry for the critics, it's just my obsesion to look for faults on ciphers

I could always just make a version that doesn't care only about letters.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

@espurritado

Here are the results :

Original text, with the key "sesquipedalian " :

 

The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The languages only differ in their grammar, their pronunciation and their most common words. Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators. To achieve this, it would be necessary to have uniform grammar, pronunciation and more common words. If several languages coalesce, the grammar of the resulting language is more simple and regular than that of the individual languages. The new common language will be more simple and regular than the existing European languages. It will be as simple as Occidental; in fact, it will be Occidental. To an English person, it will seem like simplified English, as a skeptical Cambridge friend of mine told me what Occidental is.The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The languages only differ in their grammar, their pronunciation and their most common words. Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators.

gets encoded into :

 

g{x3Xˆ…‚ƒxt3tzˆtljx%f…x3€x€ux…†3‚y3‡{x3†r~v1wr~z}Š?1eyvzƒ5ˆz…v‡v‰z5z~ˆ‰zƒxz5~|)j)v‚}q70V‚0ƒsyu~su<0}…xnh1%wtsvu8,q€o8,Q~{|n)~|n|)}qf.o{s.„}qopƒzow~3%Y{x3tzˆtzx†3‚Œ3wzwwvƒ1z1…yvzƒ1xƒv‚‚v‡A5‰}z~‡5…‡„ƒŠƒx~j}rxw)jwm0„xuy‚0}ƒ„0s}}ts%|tvhw2!Q‚q~…{zq,~qmurƒn|)€q‚!o.|s….q}{{}|.zfslzfzx3Š‚ˆw3ux3wx†|…tu}vK1€v1t€†}u1ƒvwŠˆz5‰„5…vŽ5z…zƒˆ~‹z5}{jw|uj}x‚ƒ>0d0qsxyu†u0„mnx1%mx${pxp,nq,zqoq|j{‚)}x)qb„s.ƒ|wt}€{.u€orrfw13ƒ…‚ˆv|t‡|‚3tw3~€ƒv1t€~~€1ˆ€ƒu„C5^{5ˆz‹z‡v5vƒ|Šv|z|)lxjun|lu<0„xu0w‚q}}q‚0k%ymj$viwvx€uzs,xmzsmpn)r|)vx{f.w{~zs.o|r.€slzqfw3‡{t3‡{t‡3‚y3‡{x3|uz‡zu†r}1}rx†rxzˆC5i}z5ƒzŒ5x„‚‚„ƒ5vwp~jpn)€r||0ru0}‚u0ƒy}€|j%fsi$vikvxm~,€tmz,€tq)nr|}rwp!Sƒ€}~so|.zo|uƒfljx33\‡3Š|3ux3t†3†|€ƒ}v1r„1`ttzuv…r}L5~ƒ5{vx‰A5~‰5Œ~5wz5Xllrmnw}j|>0d0q~0U~w|yƒx%ujwxsr0$j€,ƒuxx,qqy,urtn)|rvymwtwsr.S|uzwv:%fx%f3†~xƒ‡|vt3Vt€u…|wzv1wƒzvu1€w1~zv1‰„y5‚z5Œ}v‰5dxx~yzƒ‰ju)r|7]qn0U…‚€uq~0|q~w…qljx%fvi$qfynq~,{r,€tq)|jvn)ojvjz‡<.bvsw€.s~owfyj%x‹|†‡xvx3|†3t3€Œ‡{?1W€ƒ1„tzvtv=1~†ˆ~xA5ˆ…„‡‰A5z‰xA5ZŠ‡„yn)~|n|)}xu0ƒq}u0†sqr…|qw~3%Yli$pbzsmsq,{zx…)mroon{)ro.‚vsw€.u€o{{o€1%ymj|…3ƒ…‚ˆv|t‡|‚3tu1…yvzƒ1~€„…1t€~~„ƒ5Œ„‡yˆC5Z‹z‡Ž„ƒz5‡zjurƒn|)€q‰0q0~u‡0s}}~0|fslzfki${pxp,nq,pqu~jkunC)xwn!q}ƒzr.€stƒs.‚t~3x‹ƒx†|‰x3‡…t†t‡€ƒ„?¥:

Now to a little trickery to mask the spaces.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

#include <ctype.h>int strlen(char *str){    int count = 0;    while(*str != '\0')    {        count++;        str++;    }    return count;}int get_position_alpha(char c){    if(!isalpha(c)) return -1;    else    {        if(islower(c)) return c - 'a'+1;        if(isupper(c)) return c - 'A'+1;        return -1;    }}int abs(int n){    if(n >= 0) return n;    else return -1*n;}void cypher(char *str, int len, int offset){    for(int i = 0; i < len; i++)    {        if(isupper(*(str+i)))        {            *(str+i) = ((26 + ((*(str+i) - int('A')) + (offset % 26))) % 26) + int('A');        }        else if (islower(*(str+i)))        {            *(str+i) = ((26 + ((*(str+i) - int('a')) + (offset % 26))) % 26) + int('a');        }    }void cypher(char *str, int offset){    cypher(str, strlen(str) , offset);}void decypher(char *str, int offset){    cypher(str, -1*offset);}void cypher(char *str, int *key, int key_len){    char *begin = str;    for(int i = 0; i <key_len && *(begin) != '\0'; i++)    {        cypher(begin,abs(*(key+i)), *(key+i));        begin += abs(*(key+i));        if(i==key_len-1) i = 0;    }}void cypher(char *str, char *Key){    int len = strlen(Key);    int key[len];    for(int i = 0; i < len; i++)        key[i] = get_position_alpha(*(Key+i));    cypher(str, key, len);}void decypher(char *str, char *Key){    int len = strlen(Key);    int key[len];    for(int i = 0; i < len; i++)        key[i] = -1*get_position_alpha(*(Key+i));    cypher(str, key, len);}

It supports cstrings, arrays of characters.

Here is what it can do :

  • encode a string with a given offset
  • encode a string with a key, which results in multiple offsets. This key can be either an array of characters, or integers. In case you parse an array of characters, they are then transformed into an array of integers depending on their order in the alphabet. Case-sensitive.
  • decode a string with a given offset
  • decode a string with a given key , again, string of array of integers.

I'm currently working on making an executable which accepts command line arguments and on adding a pseudorandom key generator.

@Ciccioo

For example, this :

with the key "sesquipedalian" , becomes :

Pretty decent : b9nhfR6.png

Or, shifted by 3 letters :

 

This is freaking awesome dude. C++ is my all time favorite language. I've been thinking about making something similar to this for a game. Neat though.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×