Selasa, Oktober 29, 2013

Rail Fence Cipher (HTML + Javascript)

<html>   
<script type="text/javascript">
function Encrypt() {
    plaintext = document.getElementById("p").value.toLowerCase().replace(/[^a-z]/g, "");  
    if(plaintext.length < 1){ alert("please enter some plaintext"); return; }    
    var key = parseInt(document.getElementById("key").value);
    if(key > Math.floor(2*(plaintext.length-1))){ alert("key is too large for the plaintext length."); return; }  
    ciphertext = "";
    for(line=0; line<key-1; line++){
       skip=2*(key-line-1);   j=0;
        for(i=line; i<plaintext.length;){
            ciphertext += plaintext.charAt(i);
            if((line==0) || (j%2 == 0)) i+=skip;
           else i+=2*(key-1) - skip;  
           j++;          
        }
    }
    for(i=line; i<plaintext.length; i+=2*(key-1)) ciphertext += plaintext.charAt(i);
    document.getElementById("c").value = ciphertext;
}

function Decrypt(f) {
    ciphertext = document.getElementById("c").value.toLowerCase().replace(/[^a-z]/g, "");  
    if(ciphertext.length < 1){ alert("please enter some ciphertext (letters only)"); return; }    
    var key = parseInt(document.getElementById("key").value);
    if(key > Math.floor(2*(ciphertext.length-1))){ alert("please enter 1 - 22."); return; }      
    pt = new Array(ciphertext.length);   k=0;
    for(line=0; line<key-1; line++){
       skip=2*(key-line-1);  j=0;
        for(i=line; i<ciphertext.length;){
            pt[i] = ciphertext.charAt(k++);
            if((line==0) || (j%2 == 0)) i+=skip;
           else i+=2*(key-1) - skip;  
           j++;        
        }
    }
    for(i=line; i<ciphertext.length; i+=2*(key-1)) pt[i] = ciphertext.charAt(k++);
    document.getElementById("p").value = pt.join("");
}
</script>

<h2>Rail Fence Cipher </h2>
<p>Enter the word that you want to ENCRYPT, then put "NUMBER" in a key form to make how many rail you need</p>

Plaintext<BR>
<TEXTAREA id="p" name="p" rows="4" cols="50">attack at dawn</TEXTAREA>
<P>line   = <INPUT id="key" name="key" size="5" value=" " type="text"></P>
<P><INPUT name="btnEn" value="v Encrypt v" onclick="Encrypt()" type="button"> 
<INPUT name="btnDe" value="^ Decrypt ^" onclick="Decrypt()" type="button"></P>
<P>Ciphertext<BR><TEXTAREA id="c" name="c" rows="4" cols="50"></TEXTAREA> </P>

    </body>
</html>

1 ulasan:

Unknown berkata...

N r s e t i F - N 0
e e i t k o i n f r a t o L G C W - 7 1 v
m a m o o c n A S 6

Catat Ulasan

Pesanan daripada penulis :
Selamat datang ke 0x2013LΣΣT. Sekiranya anda mempunyai persoalan, pandangan, permintaan, bantuan, cadangan dan sebagainya. Tinggalkan pesanan anda ke dalam kotak komen. Terima kasih !
- http://0x2013.blogspot.com -