3v4l.org

run code in 300+ PHP versions simultaneously
<?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));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/nR0OU
function name:  (null)
number of ops:  11
compiled vars:  !0 = $textoCifrado, !1 = $cifrador
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   81     0  E >   ASSIGN                                                   !0, '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'
   82     1        NEW                                              $3      'RompeCesar'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $3
   88     4        INIT_FCALL                                               'print_r'
          5        INIT_METHOD_CALL                                         !1, 'descifrarSinSaberClave'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0  $6      
          8        SEND_VAR                                                 $6
          9        DO_ICALL                                                 
         10      > RETURN                                                   1

Class RompeCesar:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/nR0OU
function name:  __construct
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E > > RETURN                                                   null

End of function __construct

Function cifrardesplazamiento:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 6, Position 2 = 3
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 46
Branch analysis from position: 46
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 8
Branch analysis from position: 49
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 45
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 30
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 8
Branch analysis from position: 49
Branch analysis from position: 8
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 40
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
Branch analysis from position: 40
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 8
Branch analysis from position: 49
Branch analysis from position: 8
Branch analysis from position: 45
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 6, Position 2 = 3
Branch analysis from position: 6
Branch analysis from position: 3
filename:       /in/nR0OU
function name:  cifrarDesplazamiento
number of ops:  51
compiled vars:  !0 = $str, !1 = $shift, !2 = $i, !3 = $posDesplazada
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   14     2      > JMP                                                      ->4
   15     3    >   ASSIGN_OP                                     2          !1, 26
   14     4    >   IS_SMALLER                                               26, !1
          5      > JMPNZ                                                    ~5, ->3
   17     6    >   ASSIGN                                                   !2, 0
          7      > JMP                                                      ->46
   18     8    >   INIT_FCALL                                               'ord'
          9        FETCH_DIM_R                                      ~7      !0, !2
         10        SEND_VAL                                                 ~7
         11        DO_ICALL                                         $8      
         12        IS_NOT_EQUAL                                             $8, 32
         13      > JMPZ                                                     ~9, ->45
   19    14    >   INIT_FCALL                                               'ord'
         15        FETCH_DIM_R                                      ~10     !0, !2
         16        SEND_VAL                                                 ~10
         17        DO_ICALL                                         $11     
         18        ADD                                              ~12     $11, !1
         19        ASSIGN                                                   !3, ~12
   20    20        IS_SMALLER                                               122, !3
         21      > JMPZ                                                     ~14, ->30
   21    22    >   INIT_FCALL                                               'chr'
         23        MOD                                              ~16     !3, 122
         24        ADD                                              ~17     ~16, 96
         25        SEND_VAL                                                 ~17
         26        DO_ICALL                                         $18     
         27        ASSIGN_DIM                                               !0, !2
         28        OP_DATA                                                  $18
         29      > JMP                                                      ->45
   22    30    >   IS_SMALLER                                               !3, 97
         31      > JMPZ                                                     ~19, ->40
   23    32    >   INIT_FCALL                                               'chr'
         33        SUB                                              ~21     96, !3
         34        SUB                                              ~22     122, ~21
         35        SEND_VAL                                                 ~22
         36        DO_ICALL                                         $23     
         37        ASSIGN_DIM                                               !0, !2
         38        OP_DATA                                                  $23
         39      > JMP                                                      ->45
   25    40    >   INIT_FCALL                                               'chr'
         41        SEND_VAR                                                 !3
         42        DO_ICALL                                         $25     
         43        ASSIGN_DIM                                               !0, !2
         44        OP_DATA                                                  $25
   17    45    >   PRE_INC                                                  !2
         46    >   STRLEN                                           ~27     !0
         47        IS_SMALLER                                               !2, ~27
         48      > JMPNZ                                                    ~28, ->8
   29    49    > > RETURN                                                   !0
   30    50*     > RETURN                                                   null

End of function cifrardesplazamiento

Function descifrardesplazamiento:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 6, Position 2 = 3
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 6, Position 2 = 3
Branch analysis from position: 6
Branch analysis from position: 3
filename:       /in/nR0OU
function name:  descifrarDesplazamiento
number of ops:  13
compiled vars:  !0 = $str, !1 = $shift
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   32     2      > JMP                                                      ->4
   33     3    >   ASSIGN_OP                                     2          !1, 26
   32     4    >   IS_SMALLER                                               26, !1
          5      > JMPNZ                                                    ~3, ->3
   35     6    >   INIT_METHOD_CALL                                         'cifrarDesplazamiento'
          7        SEND_VAR_EX                                              !0
          8        MUL                                              ~4      !1, -1
          9        SEND_VAL_EX                                              ~4
         10        DO_FCALL                                      0  $5      
         11      > RETURN                                                   $5
   36    12*     > RETURN                                                   null

End of function descifrardesplazamiento

Function quitarespacios:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/nR0OU
function name:  quitarEspacios
number of ops:  8
compiled vars:  !0 = $str
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
   38     1        INIT_FCALL                                               'preg_replace'
          2        SEND_VAL                                                 '%2F%5Cs%2B%2F'
          3        SEND_VAL                                                 ''
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $1      
          6      > RETURN                                                   $1
   39     7*     > RETURN                                                   null

End of function quitarespacios

Function calcularfrecuenciasalfabeto:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 24, Position 2 = 11
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
2 jumps found. (Code = 44) Position 1 = 32, Position 2 = 26
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
2 jumps found. (Code = 44) Position 1 = 48, Position 2 = 34
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
2 jumps found. (Code = 44) Position 1 = 48, Position 2 = 34
Branch analysis from position: 48
Branch analysis from position: 34
Branch analysis from position: 26
2 jumps found. (Code = 44) Position 1 = 32, Position 2 = 26
Branch analysis from position: 32
Branch analysis from position: 26
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 24, Position 2 = 11
Branch analysis from position: 24
Branch analysis from position: 11
filename:       /in/nR0OU
function name:  calcularFrecuenciasAlfabeto
number of ops:  50
compiled vars:  !0 = $str, !1 = $largoSinEspacios, !2 = $apariciones, !3 = $frecuencia, !4 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
   41     1        INIT_METHOD_CALL                                         'quitarEspacios'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $5      
          4        ASSIGN                                                   !0, $5
   42     5        STRLEN                                           ~7      !0
          6        ASSIGN                                                   !1, ~7
   43     7        ASSIGN                                                   !2, <array>
   44     8        ASSIGN                                                   !3, <array>
   45     9        ASSIGN                                                   !4, 0
         10      > JMP                                                      ->20
   46    11    >   FETCH_OBJ_R                                      ~12     'letras'
         12        FETCH_DIM_R                                      ~13     ~12, !4
         13        ASSIGN_DIM                                               !2, ~13
         14        OP_DATA                                                  0
   47    15        FETCH_OBJ_R                                      ~15     'letras'
         16        FETCH_DIM_R                                      ~16     ~15, !4
         17        ASSIGN_DIM                                               !3, ~16
         18        OP_DATA                                                  0
   45    19        PRE_INC                                                  !4
         20    >   FETCH_OBJ_R                                      ~19     'letras'
         21        STRLEN                                           ~20     ~19
         22        IS_SMALLER                                               !4, ~20
         23      > JMPNZ                                                    ~21, ->11
   49    24    >   ASSIGN                                                   !4, 0
         25      > JMP                                                      ->30
   50    26    >   FETCH_DIM_R                                      ~23     !0, !4
         27        FETCH_DIM_RW                                     $24     !2, ~23
         28        PRE_INC                                                  $24
   49    29        PRE_INC                                                  !4
         30    >   IS_SMALLER                                               !4, !1
         31      > JMPNZ                                                    ~27, ->26
   52    32    >   ASSIGN                                                   !4, 0
         33      > JMP                                                      ->44
   53    34    >   FETCH_OBJ_R                                      ~29     'letras'
         35        FETCH_DIM_R                                      ~30     ~29, !4
         36        FETCH_OBJ_R                                      ~32     'letras'
         37        FETCH_DIM_R                                      ~33     ~32, !4
         38        FETCH_DIM_R                                      ~34     !2, ~33
         39        CAST                                          5  ~35     !1
         40        DIV                                              ~36     ~34, ~35
         41        ASSIGN_DIM                                               !3, ~30
         42        OP_DATA                                                  ~36
   52    43        PRE_INC                                                  !4
         44    >   FETCH_OBJ_R                                      ~38     'letras'
         45        STRLEN                                           ~39     ~38
         46        IS_SMALLER                                               !4, ~39
         47      > JMPNZ                                                    ~40, ->34
   55    48    > > RETURN                                                   !3
   56    49*     > RETURN                                                   null

End of function calcularfrecuenciasalfabeto

Function chicuadrado:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
2 jumps found. (Code = 44) Position 1 = 28, Position 2 = 5
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 22
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 28, Position 2 = 5
Branch analysis from position: 28
Branch analysis from position: 5
Branch analysis from position: 22
filename:       /in/nR0OU
function name:  chiCuadrado
number of ops:  30
compiled vars:  !0 = $frecs1, !1 = $frecs2, !2 = $chi, !3 = $i, !4 = $diferencia, !5 = $cuadradoDiferencia
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   61     2        ASSIGN                                                   !2, 0
   62     3        ASSIGN                                                   !3, 0
          4      > JMP                                                      ->24
   63     5    >   FETCH_DIM_R                                      ~8      !0, !3
          6        FETCH_OBJ_R                                      ~9      'letras'
          7        FETCH_DIM_R                                      ~10     ~9, !3
          8        FETCH_DIM_R                                      ~11     !1, ~10
          9        SUB                                              ~12     ~8, ~11
         10        ASSIGN                                                   !4, ~12
   64    11        MUL                                              ~14     !4, !4
         12        ASSIGN                                                   !5, ~14
   65    13        FETCH_OBJ_R                                      ~16     'letras'
         14        FETCH_DIM_R                                      ~17     ~16, !3
         15        FETCH_DIM_R                                      ~18     !1, ~17
         16        IS_NOT_EQUAL                                             ~18, 0
         17      > JMPZ                                                     ~19, ->22
   66    18    >   FETCH_OBJ_R                                      ~20     'letras'
         19        FETCH_DIM_R                                      ~21     ~20, !3
         20        FETCH_DIM_R                                      ~22     !1, ~21
         21        ASSIGN_OP                                     4          !5, ~22
   67    22    >   ASSIGN_OP                                     1          !2, !5
   62    23        PRE_INC                                                  !3
         24    >   FETCH_OBJ_R                                      ~26     'letras'
         25        STRLEN                                           ~27     ~26
         26        IS_SMALLER                                               !3, ~27
         27      > JMPNZ                                                    ~28, ->5
   69    28    > > RETURN                                                   !2
   70    29*     > RETURN                                                   null

End of function chicuadrado

Function descifrarsinsaberclave:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 22, Position 2 = 4
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 22, Position 2 = 4
Branch analysis from position: 22
Branch analysis from position: 4
filename:       /in/nR0OU
function name:  descifrarSinSaberClave
number of ops:  24
compiled vars:  !0 = $str, !1 = $arrayChis, !2 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   71     0  E >   RECV                                             !0      
   72     1        ASSIGN                                                   !1, <array>
   73     2        ASSIGN                                                   !2, 1
          3      > JMP                                                      ->20
   74     4    >   INIT_METHOD_CALL                                         'chiCuadrado'
          5        CHECK_FUNC_ARG                                           
          6        FETCH_OBJ_FUNC_ARG                               $6      'frecuenciasAlfabetoIngles'
          7        SEND_FUNC_ARG                                            $6
   75     8        INIT_METHOD_CALL                                         'calcularFrecuenciasAlfabeto'
          9        INIT_METHOD_CALL                                         'cifrarDesplazamiento'
         10        SEND_VAR_EX                                              !0
         11        SEND_VAR_EX                                              !2
         12        DO_FCALL                                      0  $7      
         13        SEND_VAR_NO_REF_EX                                       $7
         14        DO_FCALL                                      0  $8      
         15        SEND_VAR_NO_REF_EX                                       $8
         16        DO_FCALL                                      0  $9      
   74    17        ASSIGN_DIM                                               !1, !2
   75    18        OP_DATA                                                  $9
   73    19        PRE_INC                                                  !2
         20    >   IS_SMALLER                                               !2, 25
         21      > JMPNZ                                                    ~11, ->4
   77    22    > > RETURN                                                   !1
   78    23*     > RETURN                                                   null

End of function descifrarsinsaberclave

End of class RompeCesar.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
177.53 ms | 1416 KiB | 21 Q