Decriptare un messaggio "segreto"

Stato
Discussione chiusa ad ulteriori risposte.

Robertof

Utente Silver
19 Giugno 2008
186
10
0
98
Non sapevo che titolo mettere, quindi scusatemi in anticipo.
L'esercizio che propongo è molto semplice, consiste nel fare una funzione che rimpiazzi la posizione di una lettera alfabetica con la lettera.
Vi faccio un esempio:
Codice:
a = 1 (posizione di a)
b = 2 (posizione di b, cioè a=1,b=2)
d = 4 (posizione di d, cioè a=1,b=2,c=3,d=4)
(Le lettere straniere sono incluse)
Lo script da voi fatto deve rimpiazzare gli spazi con questo carattere: "S", e rimuovere qualsiasi carattere speciale (compresi apici e accenti), inoltre la stringa dev'essere convertita in minuscolo.
L'esercizio non è finito qui, infatti dopo aver fatto la funzione che decripta è necessario fare anche quella che cripta :p
Una volta finito il codice decriptare questa frase ed inserirla nel tag off topic in una risposta in questo argomento:
Codice:
19 1 12 22 5 S 19 5 S 19 5 9 S 18 9 21 19 3 9 20 15 S 1 S 4 5 3 18 9 16 20 1 18 5 S 17 21 5 19 20 15 S 13 5 19 19 1 7 7 9 15 S 19 9 7 14 9 6 9 3 1 S 3 8 5 S 8 1 9 S 6 9 14 9 20 15 S 12 S 5 19 5 18 3 9 26 9 15 S 3 15 13 16 12 9 13 5 14 20 9 S 15 18 1 S 16 15 19 20 1 S 17 21 5 19 20 15 S 13 5 19 19 1 7 7 9 15 S 14 5 12 S 20 1 7 S 15 6 6 S 20 15 16 9 3 S 4 5 12 S 20 15 16 9 3 S 4 5 12 12 S 5 19 5 18 3 9 26 9 15
E criptare questa frase sempre inserendola in una risposta e nel tag off topic:
Codice:
Se questo codice viene criptato correttamente, significa che lo script funziona :-)

Soluzione (Non sbirciate finché non avete finito):
[ot]
PHP:
function decrypt_secret_key($key, $separator = ' ') {
	$alpha = str_split("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",1);
	$keys = array();
	for ($i = 0; $i < count($alpha); $i++)
		$keys[($i+1)] = $alpha[$i];
	
	$alpha['S'] = ' ';
	$exp = explode($separator, $key);
	$ret = '';
	foreach ($exp as $k) {
		if (is_numeric($k))
			$s = (int) $k-1;
		else
			$s = $k;
		
		$ret.=$alpha[$s];
	}
	return $ret;
}

function remove_special_keys($str) {
	$banned=explode(" ","! \" £ $ % & / ( ) ? ' ì ^ 0 1 2 3 4 5 6 7 8 9 @ ò ç # à ° ù § * + ] } è [ { é , ; . : - _ < > |");
	return str_replace($banned,'',$str);
}
function encrypt_secret_key($key,$sep=' ') {
	$alpha = str_split("abcdefghijklmnopqrstuvwxyz",1);
	$rt = array();
	$key=strtolower($key);
	$key=remove_special_keys($key);
	#$key=str_replace(' ','',$key);
	for ($i = 0; $i < count($alpha); $i++)
		$rt[$alpha[$i]] = ($i+1);
	
	$rt[' ']='S';
	$lettere=str_split($key,1);
	$ret=array();
	for ($i=0;$i<count($lettere);$i++) {
		$ret[] = $rt[$lettere[$i]];
	}
	
	return implode(' ',$ret);
}
[/ot]

Buon divertimento!
 
eheh...molto semplice in java:
Codice:
import java.util.*;
public class Prova{
    String s=" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    public void Decripta(String st){
        StringTokenizer stz=new StringTokenizer(st," ");
        String sfin="";
        while(stz.hasMoreTokens()){
            try{
                int v=Integer.parseInt(stz.nextToken());
                sfin+=""+s.charAt(v);
            }catch(Exception e){sfin+=" ";}
        }System.out.println(sfin.toLowerCase());
    }
    public void Cripta(String st){
        String sfin="";
        for(int i=0;i<st.length();i++){
            for(int k=0;k<s.length();k++){
                if((""+st.charAt(i)).equals(""+s.charAt(k))){
                    if(k==0){
                        sfin+="S ";
                        break;
                    }else{
                        sfin+=k+" ";
                        break;
                    }
                }
            }
        }System.out.println(sfin.trim());
    }
}
[ot]salve se sei riuscito a decriptare questo messaggio significa che hai finito l esercizio complimenti ora posta questo messaggio nel tag off topic del topic dell esercizio
[/ot]
 
Decrypt:
[OT]salve se sei riuscito a decriptare questo messaggio significa che hai finito l esercizio complimenti ora posta questo messaggio nel tag off topic del topic dell esercizio[/OT]
Crypt:
[OT]19 5 S 17 21 5 19 20 15 S 3 15 4 9 3 5 S 22 9 5 14 5 S 3 18 9 16 20 1 20 15 S 3 15 18 18 5 20 20 1 13 5 14 20 5 S 19 9 7 14 9 6 9 3 1 S 3 8 5 S 12 15 S 19 3 18 9 16 20 S 6 21 14 26 9 15 14 1 S[/OT]
Dodice:
PHP:
<?php
function sdecrypt($string)
{
	$sost = explode(' ', ' a b c d e f g h i j k l m n o p q r s t u v w x y z');
	$sost[S] = ' ';
	$string = explode(' ', $string);
	foreach ($string AS $str)
	{
		$stringa .= $sost[$str];
	}
	
	return $stringa;
}

function notexist($value,$vet)
{
    $flag=true;
    for($i=0;$i<count($vet);$i++)
    {
        if($value==$vet[$i])
        {
            $flag=false;
        }
    }
    return $flag;
}

function remove($vet, $accepted)
{
	for($i=0;$i<count($vet);$i++)
	{
		if(notexist($vet[$i], $accepted))
		{
			$vet[$i] ='';
		}
	}
	
	return $vet;
}

function scrypt($string)
{
	$dasost = explode(';', 'a;b;c;d;e;f;g;h;i;j;k;l;m;n;o;p;q;r;s;t;u;v;w;x;y;z; ');
	$sost = explode(' ', '1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 S');
	$string = strtolower($string);
	$string = str_split($string, 1);
	$string = remove($string, $dasost);
	$string = str_replace($dasost, $sost, $string);
	foreach($string AS $string)
	{
		$stringa .= $string.' ';
	}
	
	return $stringa;
}
?>

Oserei dire migliore del tuo tsè...XD, bella Roberto
 
Peccato che dichiari diecimila funzioni e usi str_replace, troppo semplice :asd:
EDIT: Ma lo fai il controllo sui caratteri speciali o no?
 
we vedi ke se mettono i caratteri cinesi a te escono a me no, perchè escludo tutti i caratteri tranne le lettere minuscole...vai a vedere...e poi le funzioni sn 4, una di decrypt, una di crypt, una per controllare se il carattere è fra i consentiti e uno per cancellarlo...se vuoi posso farti anche 3 funzioni, ma non vedo che cambi...
 
Codice:
def encrypt (string)
    enc = ""
    string.gsub(/[^A-z ]/, '').downcase.each_byte {|i|
        if (i == 32)
            enc.concat("S ")
        else
            enc.concat((i-96).to_s+" ")
        end
    }
    return enc
end

def decrypt (string)
    enc = ""
    string.split(/ /).each {|i|
        if (i == 'S')
            enc.concat(" ");
        else
           enc.concat((i.to_i+96).chr)
        end
    }
    return enc
end
[ot]
salve se sei riuscito a decriptare questo messaggio significa che hai finito l esercizio complimenti ora posta questo messaggio nel tag off topic del topic dell esercizio

19 5 S 17 21 5 19 20 15 S 3 15 4 9 3 5 S 22 9 5 14 5 S 3 18 9 16 20 1 20 15 S 3 15 18 18 5 20 20 1 13 5 14 20 5 S 19 9 7 14 9 6 9 3 1 S 3 8 5 S 12 15 S 19 3 18 9 16 20 S 6 21 14 26 9 15 14 1 S
[/ot]
 
Stato
Discussione chiusa ad ulteriori risposte.