Visualizzazione dei risultati da 1 a 2 su 2

Discussione: Js: Crypt/Decrypt

  1. #1
    Utente di HTML.it L'avatar di zinzi
    Registrato dal
    Jan 2002
    Messaggi
    27

    Js: Crypt/Decrypt

    Ciao a tutti,
    in uno script di crypt/decrypt trovato in rete vorrei aggiungere un'ulteriore variabile per renderlo più complesso.

    Praticamente da un form in html dove immetto il testo da cryptare + la password, il codice genera un testo cryptato che poi con l'apposita funzione decrypt (e lo stesso form ma con un campo text nuovo) decrypta il testo.

    Il codice infatti è così. La modifica che vorrei implementare è un ciclo di criptaggio a piacere. Praticamente, per simulare quello che vorrei ottenere, basta copiare il testo cryptato e ricriptarlo n volte... e viceversa.

    Spero di essermi spiegato bene.
    Grazie a tutti e ciao.
    Zinzi.

  2. #2
    Utente di HTML.it L'avatar di zinzi
    Registrato dal
    Jan 2002
    Messaggi
    27

    codice (per i prigroni..)

    <!-- TWO STEPS TO INSTALL XOR ENCRYPTION4:

    1. Copy the coding into the HEAD of your HTML document
    2. Add the last code into the BODY of your HTML document -->



    <HEAD>
    <SCRIPT LANGUAGE="JavaScript">

    <!-- Begin
    /************************************************** **********************
    * To implement this script onto your page, copy and paste the Javascript
    * on this page and place it in the page that you want the encryption
    * routine available on. Then use the function "encrypt()" to encrypt
    * data. This function takes two parameters. The first parameter is the
    * plain text string and the second parameter is the key. The returned
    * string is the encrypted string. To decrypt the string, use the
    * function "decrypt()" with the encrypted string as the first parameter
    * and key as the second parameter. It returns the decrypted string.
    *
    * Examples:
    * var secret = encrypt("My surprise will consist of ....", "password");
    * document.writeln(secret);
    *
    * document.form[0].elements[1].value = decrypt(document.form[0].elements[0].value, "password");
    *
    ************************************************** ***********************/

    function encrypt(str, pwd) {
    if(pwd == null || pwd.length <= 0) {
    alert("Please enter a password with which to encrypt the message.");
    return null;
    }
    var prand = "";
    for(var i=0; i<pwd.length; i++) {
    prand += pwd.charCodeAt(i).toString();
    }
    var sPos = Math.floor(prand.length / 5);
    var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
    var incr = Math.ceil(pwd.length / 2);
    var modu = Math.pow(2, 31) - 1;
    if(mult < 2) {
    alert("Algorithm cannot find a suitable hash. Please choose a different password. \nPossible considerations are to choose a more complex or longer password.");
    return null;
    }
    var salt = Math.round(Math.random() * 1000000000) % 100000000;
    prand += salt;
    while(prand.length > 10) {
    prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
    }
    prand = (mult * prand + incr) % modu;
    var enc_chr = "";
    var enc_str = "";
    for(var i=0; i<str.length; i++) {
    enc_chr = parseInt(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255));
    if(enc_chr < 16) {
    enc_str += "0" + enc_chr.toString(16);
    } else enc_str += enc_chr.toString(16);
    prand = (mult * prand + incr) % modu;
    }
    salt = salt.toString(16);
    while(salt.length < 8)salt = "0" + salt;
    enc_str += salt;
    return enc_str;
    }

    function decrypt(str, pwd) {
    if(str == null || str.length < 8) {
    alert("A salt value could not be extracted from the encrypted message because it's length is too short. The message cannot be decrypted.");
    return;
    }
    if(pwd == null || pwd.length <= 0) {
    alert("Please enter a password with which to decrypt the message.");
    return;
    }
    var prand = "";
    for(var i=0; i<pwd.length; i++) {
    prand += pwd.charCodeAt(i).toString();
    }
    var sPos = Math.floor(prand.length / 5);
    var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
    var incr = Math.round(pwd.length / 2);
    var modu = Math.pow(2, 31) - 1;
    var salt = parseInt(str.substring(str.length - 8, str.length), 16);
    str = str.substring(0, str.length - 8);
    prand += salt;
    while(prand.length > 10) {
    prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
    }
    prand = (mult * prand + incr) % modu;
    var enc_chr = "";
    var enc_str = "";
    for(var i=0; i<str.length; i+=2) {
    enc_chr = parseInt(parseInt(str.substring(i, i+2), 16) ^ Math.floor((prand / modu) * 255));
    enc_str += String.fromCharCode(enc_chr);
    prand = (mult * prand + incr) % modu;
    }
    return enc_str;
    }
    // End -->
    </script>

    </HEAD>



    <BODY>

    <form name="box"><center>
    <table cellpadding=0 cellspacing=0 border=0><tr><td colspan=3>
    <textarea cols=40 rows=5 wrap=virtual name=ipt>The quick brown fox jumped over the lazy dogs.</textarea>
    </td></tr>
    <tr height=50><td valign="top">
    <input type="button" onclick="document.box.opt.value= encrypt(document.box.ipt.value, document.box.pwd.value);" value="Encrypt">
    </td><td align="center" valign="center">
    <input type="text" name="pwd" value="password">
    </td><td align="right" valign="bottom">
    <input type="button" onclick="document.box.ipt.value= decrypt(document.box.opt.value, document.box.pwd.value);" value="Decrypt">
    </td></tr>
    <tr><td colspan=3>
    <textarea cols=40 rows=5 wrap=virtual name=opt></textarea>
    </td></tr></table>
    </center>
    </form>


Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.