Crypt messages with the Vigen�re method

[deleted user]
11 years ago | edited 10 years ago

1

Hi everybody!

I made a program to crypt messages with the Vigene method.

For those who don’t know what it is:
-You have an original message and a key to crypt the message.
-You assign each letter to a number: a will be 0, b will be 1, c will be 2 … and z will be 25.
-You add the first number of the message with the first number of the key, the second number of the message with the second number of the key…
-You get a crypted message assigning each number to each letter.

If you want to decrypt a message, you just have to subtract instead of add.

Here is an example:
If your message is: “Hack This”
And if your key is: “key”
Hack This
keyk eyke
The crypted message will be: “Reau Xfsw”

If you still don’t understand what I’m saying ^^ go here: http://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher

And here is my program written in javascript:

var alphab="abcdefghijklmnopqrstuvwxyz";  
var lbiz="";  
var lrec="aaaaceeeeiiioooouuuy";  
var crypt=prompt("Do you want to crypt (enter 0) or to decrypt (enter 1) a message?");  
if (crypt==="1"){  
var mc=prompt("What is the crypted message?");  
var clprompt("What is the key?");  
mc=mc.toLowerCase();  
clcltoLowerCase();  
var phrase2=[];  
for (k=0;k<mc.length;k++){  
phrase2[k]=" ";  
for (n=0;n<lbiz.length;n++){  
if (mc[k]===lbiz[n]){phrase2[k]=lrec[n]}  
if (n===lbiz.length-1&&phrase2[k]===" "){phrase2[k]=mc[k]}  
}  
}  
var phrase3="";  
for (k=0;k<phrase2.length;k++){phrase3=phrase3+phrase2[k]}  
mc=phrase3;  
var phrase2=[];  
for (k=0;k<cllength;k++){  
phrase2[k]=" ";  
for (n=0;n<lbiz.length;n++){  
if (clk]===lbiz[n]){phrase2[k]=lrec[n]}  
if (n===lbiz.length-1&&phrase2[k]===" "){phrase2[k]=clk]}  
}  
}  
var phrase3="";  
for (k=0;k<phrase2.length;k++){phrase3=phrase3+phrase2[k]}  
clphrase3;  
var nc=[];  
for (k=0;k<mc.length;k++){  
nc[k]=" ";  
for (n=0;n<alphab.length;n++){  
if (mc[k]===alphab[n]){nc[k]=n}  
if (n===alphab.length-1&&nc[k]===" "){nc[k]=mc[k]}  
}  
}  
var nbcl[];  
for (k=0;k<cllength;k++){  
nbclk]=" ";  
for (n=0;n<alphab.length;n++){  
if (clk]===alphab[n]){nbclk]=n}  
}  
}  
var fkpuzat=0;  
if (nbclnbcllength-1]===" "){  
nbclnbcllength-1]="a";  
fkpuzat++;  
}  
for (k=0;k<nbcllength;k++){  
if (nbclk]===" "){  
fkpuzat++;  
for (n=k;n<nbcllength-1;n++){nbcln]=nbcln+1]}  
k--;  
}  
}  
var tabr=[];  
for (k=0;k<nbcllength-fkpuzat;k++){tabr[k]=nbclk]}  
nbcltabr;  
var ss=[];  
var ssprov=[];  
var mcl=mc.length;  
var cl=nbcllength;  
if (mcl>cl){  
var nbclupl=[];  
var r=mcl%cl;  
var q=(mcl-r)/cl;  
for (k=0;k<q;k++){  
for (n=0;n<cl;n++){nbclupl[k*cl+n]=nbcln]}  
}  
for (k=0;k<r;k++){nbclupl[cl*q+k]=nbclk]}  
for (k=0;k<mcl;k++){  
if (isNaN(nc[k])||nc[k]===" "){  
for (n=nbclupl.length;n>k;n--){nbclupl[n]=nbclupl[n-1]}  
nbclupl[k]=" ";  
}  
}  
for (k=0;k<mcl;k++){  
if (!isNaN(nc[k])&&nc[k]!==" "){ssprov[k]=(nc[k]-nbclupl[k])%26;  
if (ssprov[k]<0){ss[k]=ssprov[k]+26}  
else {ss[k]=ssprov[k]}  
}  
else {ss[k]=nc[k]}  
}  
}  
else {  
for (k=0;k<mcl;k++){  
if (isNaN(nc[k])||nc[k]===" "){  
for (n=nbcllength;n>k;n--){nbcln]=nbcln-1]}  
nbclk]=" ";  
}  
}  
for (k=0;k<mcl;k++){  
if (!isNaN(nc[k])&&nc[k]!==" "){ssprov[k]=(nc[k]-nbclk])%26;  
if (ssprov[k]<0){ss[k]=ssprov[k]+26}  
else {ss[k]=ssprov[k]}  
}  
else {ss[k]=nc[k]}  
}  
}  
var md="";  
for (k=0;k<ss.length;k++){  
if (!isNaN(ss[k])&&ss[k]!==" "){md=md+alphab[ss[k]]}  
else {md=md+ss[k]}  
}  
var message=md;  
}  
else if (crypt==="0"){  
var mc=prompt("What message do you want to crypt?");  
var clprompt("What is the key?");  
mc=mc.toLowerCase();  
clcltoLowerCase();  
var phrase2=[];  
for (k=0;k<mc.length;k++){  
phrase2[k]=" ";  
for (n=0;n<lbiz.length;n++){  
if (mc[k]===lbiz[n]){phrase2[k]=lrec[n]}  
if (n===lbiz.length-1&&phrase2[k]===" "){phrase2[k]=mc[k]}  
}  
}  
var phrase3="";  
for (k=0;k<phrase2.length;k++){phrase3=phrase3+phrase2[k]}  
m=phrase3;  
var phrase2=[];  
for (k=0;k<cllength;k++){  
phrase2[k]=" ";  
for (n=0;n<lbiz.length;n++){  
if (clk]===lbiz[n]){phrase2[k]=lrec[n]}  
if (n===lbiz.length-1&&phrase2[k]===" "){phrase2[k]=clk]}  
}  
}  
var phrase3="";  
for (k=0;k<phrase2.length;k++){phrase3=phrase3+phrase2[k]}  
clphrase3;  
var n=[];  
for (k=0;k<m.length;k++){  
n[k]=" ";  
for (n=0;n<alphab.length;n++){  
if (m[k]===alphab[n]){n[k]=n}  
if (n===alphab.length-1&&n[k]===" "){n[k]=m[k]}  
}  
}  
var nbcl[];  
for (k=0;k<cllength;k++){  
nbclk]=" ";  
for (n=0;n<alphab.length;n++){  
if (clk]===alphab[n]){nbclk]=n}  
}  
}  
var fkpuzat=0;  
if (nbclnbcllength-1]===" "){  
nbclnbcllength-1]="a";  
fkpuzat++;  
}  
for (k=0;k<nbcllength;k++){  
if (nbclk]===" "){  
fkpuzat++;  
for (n=k;n<nbcllength-1;n++){nbcln]=nbcln+1]}  
k--;  
}  
}  
var tabr=[];  
for (k=0;k<nbcllength-fkpuzat;k++){tabr[k]=nbclk]}  
nbcltabr;  
var ad=[];  
var ml=m.length;  
var cl=nbcllength;  
if (ml>cl){  
var nbclupl=[];  
var r=ml%cl;  
var q=(ml-r)/cl;  
for (k=0;k<q;k++){  
for (n=0;n<cl;n++){nbclupl[k*cl+n]=nbcln]}  
}  
for (k=0;k<r;k++){nbclupl[cl*q+k]=nbclk]}  
for (k=0;k<ml;k++){  
if (isNaN(n[k])||n[k]===" "){  
for (n=nbclupl.length;n>k;n--){nbclupl[n]=nbclupl[n-1]}  
nbclupl[k]=" ";  
}  
}  
for (k=0;k<ml;k++){  
if (!isNaN(n[k])&&n[k]!==" "){ad[k]=(n[k]+nbclupl[k])%26}  
else {ad[k]=n[k]}  
}  
}  
else {  
for (k=0;k<ml;k++){  
if (isNaN(n[k])||n[k]===" "){  
for (n=nbcllength;n>k;n--){nbcln]=nbcln-1]}  
nbclk]=" ";  
}  
}  
for (k=0;k<ml;k++){  
if (!isNaN(n[k])&&n[k]!==" "){ad[k]=(n[k]+nbclk])%26}  
else {ad[k]=n[k]}  
}  
}  
var mc="";  
for (k=0;k<ad.length;k++){  
if (!isNaN(ad[k])&&ad[k]!==" "){mc=mc+alphab[ad[k]]}  
else {mc=mc+ad[k]}  
}  
var message=mc;  
}  
else {message="You have to enter 0 or 1!"}  
alert(message);  

I have some questions:
-What do you think about my program? :p
-Can you see a way to improve it? Maybe with functions?
-Can I make a program to decrypt a crypted message without knowing the key?

10replies
4voices
227views
Pinkponyprincess
11 years ago

0

Didn’t really bother to go thru your code, being that it is not really re useable ,Vigene cypher has been cracked for 100 years i think ????? (Didnt even google it just now) but Vigene is useless as far as i know. But kudos if it is for learning purposes.

Pinkponyprincess
11 years ago

0

Didnt mean to sound like a bitch lol

[deleted user]
11 years ago

0

Yes my goal was to learn javascrpit first, this program is an excuse ^^

Pinkponyprincess
11 years ago

0

Cool :) , well , maybe put your code in functions , like a “create_key” function, and a encrypting function, maybe ?

[deleted user]
11 years ago

0

Thanks I will try :)

[deleted user]
11 years ago

0

Admin of Ponies lol XD. What they said ^

[deleted user]
11 years ago | edited 10 years ago

0

Ok I made an update with functions! :)
Now my program accept accents, symbols and keep numbers as they are.

With a little html it looks great :)

<!DOCTYPE html>  
<html>  
   <head>  
      <title>Vigene</title>  
      <style>  
         body{  
            background-color:black;  
            text-align:center;  
         }  
         h1{  
            color:#35d699;  
         }  
         h2{  
            color:#35d699;  
         }  
         p{  
            color:#35d699;  
         }  
         form{  
            color:#35d699;  
         }  
         a{  
            color:#35d699;  
         }  
      </style>  
      <script>  
         var alphab = 'abcdefghijklmnopqrstuvwxyz';  
         var rectify = function (p) {  
//replace strange letters by normal letters rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr  
             var lbiz = '';  
             var lrec = 'aaaaceeeeiiioooouuuy';  
             var p2 = '';  
             for (k = 0; k < p.length; k++) {  
                 var j = 0;  
                 for (n = 0; n < lbiz.length; n++) {  
                     if (p[k] === lbiz[n]) {  
                         p2 = p2 + lrec[n];  
                         j++;  
                     }  
                     if (n === lbiz.length - 1 && j === 0) {  
                         p2 = p2 + p[k];  
                     }  
                 }  
             }  
             return p2;  
         }  
         var saveNumbers = function (mess) {  
//save message's numbers and replace other things by spaces sssssssssssssssssssssssssssssssssssssssssssss  
             var numbers = [];  
             for (k = 0; k < mess.length; k++) {  
                 if (!isNaN(mess[k]) && mess[k] !== ' ') {  
                     numbers[k] = mess[k];  
                 }   
                 else {  
                     numbers[k] = ' ';  
                 }  
             }  
             return numbers;  
         }  
         var delNumbers = function (mess) {  
//replace message's numbers by spaces ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd  
             var newMess = [];  
             for (k = 0; k < mess.length; k++) {  
                 if (!isNaN(mess[k]) && mess[k] !== ' ') {  
                     newMess[k] = ' ';  
                 }   
                 else {  
                     newMess[k] = mess[k];  
                 }  
             }  
             return newMess;  
         }  
         var replaceLettersByNumbers = function (mess) {  
//replace message's letters by numbers rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr  
             var nbMess = [];  
             for (k = 0; k < mess.length; k++) {  
                 nbMess[k] = ' ';  
                 for (n = 0; n < alphab.length; n++) {  
                     if (mess[k] === alphab[n]) {  
                         nbMess[k] = n;  
                     }  
                     if (n === alphab.length - 1 && nbMess[k] === ' ') {  
                         nbMess[k] = mess[k];  
                     }  
                 }  
             }  
             return nbMess;  
         }  
         var keepNumbers = function (originalArray) {  
//keep only numbers in an array kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk  
             var newArray = [];  
             var fkpuzat = 0;  
             for (k = 0; k < originalArray.length; k++) {  
                 if (originalArray[k] !== ' ' && !isNaN(originalArray[k])) {  
                     newArray[fkpuzat] = originalArray[k];  
                     fkpuzat++;  
                 }  
             }  
             return newArray;  
         }  
         var duplicateKey = function (message, key) {  
//duplicate or cut the key to get the same length as the message and place it ddddddddddddddddddddddddddd  
             var mcl = message.length;  
             var keyl = key.length;  
             var keydupl = [];  
             if (mcl > keyl) {  
                 var r = mcl % keyl;  
                 var q = (mcl - r) / keyl;  
                 for (k = 0; k < q; k++) {  
                     for (n = 0; n < keyl; n++) {  
                         keydupl[k * keyl + n] = key[n];  
                     }  
                 }  
                 for (k = 0; k < r; k++) {  
                     keydupl[keyl * q + k] = key[k];  
                 }  
             }   
             else {  
                 for (k = 0; k < mcl; k++) {  
                     keydupl[k] = key[k];  
                 }  
             }  
             for (k = 0; k < mcl; k++) {  
                 if (isNaN(message[k]) || message[k] === ' ') {  
                     for (n = keydupl.length; n > k; n--) {  
                         keydupl[n] = keydupl[n - 1];  
                     }  
                     keydupl[k] = ' ';  
                 }  
             }  
             return keydupl;  
         }  
         var encrMess=function (){  
// Encrypt the message eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee  
            var fr=document.getElementById("encrypt");  
            var message0=fr.elements[0].value;  
            var key0=fr.elements[1].value;  
            if (message0.length === 0||key0.length === 0) {  
               message0 = 'You have to write a message and a key !';  
               key0 = 'a';  
            }  
            var key1 = key0.toLowerCase();  
            var key2 = rectify(key1);  
            var key3 = delNumbers(key2);  
            var verif = 0;  
            for (k = 0; k < key3.length; k++) {  
                for (n = 0; n < alphab.length; n++) {  
                    if (key3[k] === alphab[n]) {  
                        verif++; // Check if there is at least a letter in the key  
                    }  
                }  
            }  
            if (verif === 0) {  
                message0='You have to write at least a letter in the key.';  
                key3 = ["a"];  
            }  
            var key4 = replaceLettersByNumbers(key3);  
            var key5 = keepNumbers(key4);  
            var message1 = message0.toLowerCase();  
            var message2 = rectify(message1);  
            var message3 = delNumbers(message2);  
            var message4 = replaceLettersByNumbers(message3);  
            var key6 = duplicateKey(message4, key5);  
            var total = [];  
                for (k = 0; k < message4.length; k++) {  
                    if (!isNaN(message4[k]) && message4[k] !== ' ') {  
                        total[k] = (message4[k] + key6[k]) % 26;  
                    }   
                    else {  
                        total[k] = message4[k];  
                    }  
                }  
            var message5 = '';  
            for (k = 0; k < total.length; k++) {  
                if (!isNaN(total[k]) && total[k] !== ' ') {  
                    message5 = message5 + alphab[total[k]];  
                }  
                else {  
                    message5 = message5 + total[k];  
                }  
            }  
            var numbers = saveNumbers(message2);  
            var message6 = '';  
            for (k = 0; k < message0.length; k++) {  
                if (numbers[k] !== ' ') {  
                    message6 = message6 + numbers[k];  
                }  
                else {  
                    message6 = message6 + message5[k];  
                }  
            }  
            document.getElementById("encryptedMessage").innerHTML=message6;  
         }  
         var decrMess=function (){  
// Decrypt the message dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd  
            var fr=document.getElementById("decrypt");  
            var message0=fr.elements[0].value;  
            var key0=fr.elements[1].value;  
            if (message0.length === 0||key0.length === 0) {  
               message0 = 'You have to write a message and a key !';  
               key0 = 'a';  
            }  
            var key1 = key0.toLowerCase();  
            var key2 = rectify(key1);  
            var key3 = delNumbers(key2);  
            var verif = 0;  
            for (k = 0; k < key3.length; k++) {  
                for (n = 0; n < alphab.length; n++) {  
                    if (key3[k] === alphab[n]) {  
                        verif++; // Check if there is at least a letter in the key  
                    }  
                }  
            }  
            if (verif === 0) {  
                message0='You have to write at least a letter in the key.';  
                key3 = ["a"];  
            }  
            var key4 = replaceLettersByNumbers(key3);  
            var key5 = keepNumbers(key4);  
            var message1 = message0.toLowerCase();  
            var message2 = rectify(message1);  
            var message3 = delNumbers(message2);  
            var message4 = replaceLettersByNumbers(message3);  
            var key6 = duplicateKey(message4, key5);  
            var total = [];  
                for (k = 0; k < message4.length; k++) {  
                    if (!isNaN(message4[k]) && message4[k] !== ' ') {  
                        total[k] = (message4[k] - key6[k]) % 26;  
                        if (total[k] < 0) {  
                            total[k] = total[k] + 26;  
                        }  
                    }  
                    else {  
                        total[k] = message4[k];  
                    }  
                }  
            var message5 = '';  
            for (k = 0; k < total.length; k++) {  
                if (!isNaN(total[k]) && total[k] !== ' ') {  
                    message5 = message5 + alphab[total[k]];  
                }  
                else {  
                    message5 = message5 + total[k];  
                }  
            }  
            var numbers = saveNumbers(message2);  
            var message6 = '';  
            for (k = 0; k < message0.length; k++) {  
                if (numbers[k] !== ' ') {  
                    message6 = message6 + numbers[k];  
                }  
                else {  
                    message6 = message6 + message5[k];  
                }  
            }  
            document.getElementById("decryptedMessage").innerHTML=message6;  
         }  
      </script>  
   </head>  
   <body>  
      <br />  
      <h1>Encrypt messages with the Vigene method !</h1>  
      <br />  
      <h2>Encrypt a message</h2>  
      <form id="encrypt" onSubmit="encrMess();return false">  
         Decrypted message :<br />  
         <textarea rows="4" cols="50" autofocus></textarea><br />  
         Key :<br />  
         <input type="password" /><br /><br />  
         <input type="submit" value="Encrypt !" />  
      </form>  
      <p id="encryptedMessage">Your encrypted message will appear here.</p>  
      <a href="">Click here to refresh the page.</a>  
      <br />  
      <br />  
      <hr />  
      <h2>Decrypt a message</h2>  
      <form id="decrypt" onSubmit="decrMess();return false">  
         Encrypted message :<br>  
         <textarea rows="4" cols="50"></textarea><br />  
         Key :<br />  
         <input type="password" /><br /><br />  
         <input type="submit" value="Decrypt !" />  
      </form>  
      <p id="decryptedMessage">Your decrypted message will appear here.</p>  
      <a href="">Click here to refresh the page.</a>  
   </body>  
</html>  

[/spoiler]

Now come the hard part, I would create a program to crack an encrypted message. I made some searches:
http://en.wikipedia.org/wiki/Frequency_analysis
http://en.wikipedia.org/wiki/Kasiski_examination

And an existing website to test how it can work:
http://www.mygeocachingprofile.com/codebreaker.vigenerecipher.aspx

But it seems this method doesn’t always work when the key length is unknown. Here is an example:
jbmk fmhuex agksg vv nlwkm wf q lihxbwgyir ag bvr uhgjrxhrt nipm jig mbel aidcuh mx mpseu cwf’m?

If I don’t give you the length of the key I used, the website above won’t help. But if I tell you that the length of my key is [spoiler]8 you will find the original message (and the key).

Do you have any advice to make this program?

I know it’s not steganography but @Keeper may be interested in the subject? :)

Reply has been removed
Fireshard
11 years ago

0

I am pretty sure you cannot fully automatize the cracking of such and encryption, since you need to somehow validate your findings as actual words.

Also, you would frequency analysis is highly dependent upon language of the text. So if I use French in my plaintext and you try to crack it using frequency analysis of the English alphabet, you are very likely to fail.

Also, having short plaintexts is likely to cause your algorithm to fail, due to the plaintext’s deviation from the statistical mean.

If you do manage to get past these problems, finding the length of the key could simply be bruteforcing different lengths (although time-expensive, it is still do-able within a fair amount of time)

Keeper
11 years ago

1

Cracking polyalphabetical ciphers is way easier than other means of encryption. Generally speaking, if you were asked to crack your first encryption algorithm as a homework or whatever beginner related, that would be Vigenere. Forget about estimating key length and finding the key itself and so on and so forth. Look into linear fault analysis and you will get round this part.

Pinkponyprincess
11 years ago

0

:) Keeper makes sense, but my first thought was maybe to break the encrypted code into columns of say 1, then 2 , then 3 , then 4 and so forth , and in each state(amount of columns ) you use regex to search for patterns , when you find a pattern you probably have found the key length , just so everyone knows , this is just a theory i thought of, and i have no idea if it would work…

You must be logged in to reply to this discussion. Login
1 of 11

This site only uses cookies that are essential for the functionality of this website. Cookies are not used for tracking or marketing purposes.

By using our site, you acknowledge that you have read and understand our Privacy Policy, and Terms of Service.

Dismiss