@ 2015-08-05T22:23:49Z <?php
/**
*
*/
class RompeCesar {
private $letras = "abcdefghijklmnopqrstuvwxyz";
private $frecuenciasAlfabetoIngles = array(0.0817, 0.0150, 0.0278, 0.0425, 0.1270, 0.0223, 0.0202, 0.0609, 0.0697, 0.0015, 0.0077, 0.0403, 0.0241, 0.0675, 0.0751, 0.0193, 0.0010, 0.0599, 0.0633, 0.0906, 0.0276, 0.0098, 0.0236, 0.0015, 0.0197, 0.0007);
function __construct() {
// code...
}
// A 65 Z 90
// a 97 z 122
public function cifrarDesplazamiento($str, $shift) {
while ($shift > 26) {
$shift -= 26;
}
for ($i = 0; $i < strlen($str); $i++) {
if (ord($str[$i]) != 32) {
$posDesplazada = ord($str[$i]) + $shift;
if ($posDesplazada > 122) {
$str[$i] = chr(($posDesplazada % 122) + 96);
} else if ($posDesplazada < 97) {
$str[$i] = chr(122 - (96 - $posDesplazada));
} else {
$str[$i] = chr($posDesplazada);
}
}
}
return $str;
}
public function descifrarDesplazamiento($str, $shift) {
while ($shift > 26) {
$shift -= 26;
}
return $this->cifrarDesplazamiento($str, -$shift);
}
public function quitarEspacios($str) {
return preg_replace('/\s+/', '', $str);
}
public function calcularFrecuenciasAlfabeto($str) {
$str = $this->quitarEspacios($str);
$largoSinEspacios = strlen($str);
$apariciones = array();
$frecuencia = array();
for ($i = 0 ; $i < strlen($this->letras) ; $i++) {
$apariciones[$this->letras[$i]] = 0;
$frecuencia[$this->letras[$i]] = 0;
}
for ($i = 0 ; $i < $largoSinEspacios ; $i++) {
$apariciones[$str[$i]]++;
}
for ($i = 0 ; $i < strlen($this->letras) ; $i++) {
$frecuencia[$this->letras[$i]] = $apariciones[$this->letras[$i]] / (float) $largoSinEspacios;
}
return $frecuencia;
}
// la comprobación del 0 es porque en textos pequeños puede que haya letras
// que no aparezcan
// cuanto menor sea la chi, más se parecen los arrays
public function chiCuadrado($frecs1, $frecs2) {
$chi = 0;
for ($i = 0 ; $i < strlen($this->letras) ; $i++) {
$diferencia = $frecs1[$i] - $frecs2[$this->letras[$i]];
$cuadradoDiferencia = $diferencia * $diferencia;
if ($frecs2[$this->letras[$i]] != 0)
$cuadradoDiferencia /= $frecs2[$this->letras[$i]];
$chi += $cuadradoDiferencia;
}
return $chi;
}
public function descifrarSinSaberClave($str) {
$arrayChis = array();
for ($i = 1 ; $i < 25 ; $i++) {
$arrayChis[$i] = $this->chiCuadrado($this->frecuenciasAlfabetoIngles,
$this->calcularFrecuenciasAlfabeto($this->cifrarDesplazamiento($str, $i)));
}
return $arrayChis;
}
}
// sabemos que está en inglés
$textoCifrado = "lrvmnir bpr sumvbwvr jx bpr lmiwv yjeryrkbi jx qmbm wi bpr xjvni mkd ymibrut jx irhx wi bpr riirkvr jx ymbinlmtmipw utn qmumbr dj w ipmhh but bj rhnvwdmbr bpr yjeryrkbi jx bpr qmbm mvvjudwko bj yt wkbrusurbmbwjk lmird jk xjubt trmui jx ibndt wb wi kjb mk rmit bmiq bj rashmwk rmvp yjeryrkb mkd wbi iwokwxwvmkvr mkd ijyr ynib urymwk nkrashmwkrd bj ower m vjyshrbr rashmkmbwjk jkr cjnhd pmer bj lr fnmhwxwrd mkd wkiswurd bj invp mk rabrkb bpmb pr vjnhd urmvp bpr ibmbr jx rkhwopbrkrd ywkd vmsmlhr jx urvjokwgwko ijnkdhrii ijnkd mkd ipmsrhrii ipmsr w dj kjb drry ytirhx bpr xwkmh mnbpjuwbt lnb yt rasruwrkvr cwbp qmbm pmi hrxb kj djnlb bpmb bpr xjhhjcwko wi bpr sujsru msshwvmbwjk mkd wkbrusurbmbwjk w jxxru yt bprjuwri wk bpr pjsr bpmb bpr riirkvr jx jqwkmcmk qmumbr cwhh urymwk wkbmvb";
$cifrador = new RompeCesar;
/*
echo $cifrador->cifrarDesplazamiento("attack", 17) . "\n";
echo $cifrador->cifrarDesplazamiento("attack", 225) . "\n";
echo $cifrador->descifrarDesplazamiento("rkkrtb", 225) . "\n";
//print_r($cifrador->calcularFrecuenciasAlfabeto($textoCifrado));*/
print_r($cifrador->descifrarSinSaberClave($textoCifrado));
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 5.0.0 - 5.0.5 , 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 , 5.3.0 - 5.3.29 , 5.4.0 - 5.4.45 , 5.5.24 - 5.5.35 , 5.6.7 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.20 , 7.2.0 - 7.2.33 , 7.3.16 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.4 , 8.3.6 Array
(
[1] => 3.8610192926996
[2] => 8.0940870034157
[3] => 5.1383381099849
[4] => 3.1305765933367
[5] => 2.4915963979181
[6] => 2.9996436353515
[7] => 4.692701093949
[8] => 7.9312572576079
[9] => 4.9210166191342
[10] => 2.0085306927504
[11] => 3.9309969459652
[12] => 6.6333817563885
[13] => 9.0755833185457
[14] => 8.385407152827
[15] => 3.1289385434874
[16] => 2.676404047001
[17] => 2.5120201597561
[18] => 1.4827336267976
[19] => 2.9789856411302
[20] => 5.2930992361982
[21] => 5.1487486322187
[22] => 2.1287578468551
[23] => 2.8390098573694
[24] => 13.038950629727
)
Output for 8.3.5 Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Array
(
[1] => 3.8610192926996
[2] => 8.0940870034157
[3] => 5.1383381099849
[4] => 3.1305765933367
[5] => 2.4915963979181
[6] => 2.9996436353515
[7] => 4.692701093949
[8] => 7.9312572576079
[9] => 4.9210166191342
[10] => 2.0085306927504
[11] => 3.9309969459652
[12] => 6.6333817563885
[13] => 9.0755833185457
[14] => 8.385407152827
[15] => 3.1289385434874
[16] => 2.676404047001
[17] => 2.5120201597561
[18] => 1.4827336267976
[19] => 2.9789856411302
[20] => 5.2930992361982
[21] => 5.1487486322187
[22] => 2.1287578468551
[23] => 2.8390098573694
[24] => 13.038950629727
)
Output for 4.4.2 - 4.4.9 Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/nR0OU on line 6
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/nR0OU on line 6
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/nR0OU on line 6
Process exited with code 255 . preferences:dark mode live preview
226.24 ms | 401 KiB | 312 Q