Current Location: Home> Latest Articles> Why does str_rot13 fail when processing numbers? Common causes and solutions

Why does str_rot13 fail when processing numbers? Common causes and solutions

gitbox 2025-09-12

Sure! Below is the translated content you requested (in PHP language), with a horizontal line separating unrelated sections from the main body:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>  
</span><span><span class="hljs-comment">// This file explains the reasons why str_rot13 fails when processing numbers and the solutions.</span></span><span>  
</span><span><span class="hljs-comment">// Author: You can fill this in yourself</span></span><span>  
</span><span><span class="hljs-comment">// Date: 2025</span></span><span>  
<p></span>// ------------------------------- Beginning of the main body ----------------------------------<span></p>
<p><span class="hljs-comment">/**</p>
<ul data-is-last-node="" data-is-only-node="">
<li>
<p>Why does str_rot13 fail when processing numbers? Common causes and solutions</p>
</li>
<li></li>
<li>
<p>In PHP, <code>str_rot13
  • echo str_rot13("abc123"); // Output: nop123

  • From the result, we can see that only "abc" is replaced with "nop", while "123" remains unchanged.

  • II. Why was this design chosen?

  • This design is intentional. ROT13 originated from the ancient Roman Caesar cipher, but its modern implementation, particularly in the computer field,

  • typically only replaces ASCII letter characters. This is because it is mainly used for simple text obfuscation rather than strong encryption.

  • There is no direct "letter correspondence" for digits in ROT13, so str_rot13 naturally does not handle numbers.

  • III. The problem in common scenarios

  • When we want to "pseudo-encrypt" an entire string (including numbers), the behavior of str_rot13 may cause "information leakage".

  • For example, when hiding some sensitive content, although the letters are changed, the exposed numbers may still reveal too much.

  • For example:

  • $input = "Email: user123@example.com";

  • echo str_rot13($input);

  • // Output: Rznvy: hfre123@rknzcyr.pbz

  • In the result, 123 and the entire structure of the email can still be easily recognized.

  • IV. Solutions

  • If you need a "more comprehensive" encryption method (including numbers), you can use the following approaches:

  • 1. Using a custom ROT13 + ROT5 method

  • You can write your own function combining ROT13 and ROT5 (5-position shift for digits):

  • function rot13_5($str) {

  • $str = str_rot13($str);  
    
  • return preg_replace_callback(&#039;/\d/&#039;, function($matches) {  
    
  •     return (string)((($matches[0] + 5) % 10));  
    
  • }, $str);  
    
  • }

  • echo rot13_5("abc123"); // Output: nop678

  • This way, both letters and digits are obfuscated.

  • 2. Using base64_encode or other lightweight encryption methods

  • If you just want to obfuscate data and do not consider encryption strength, you can use base64_encode:

  • echo base64_encode("abc123"); // Output: YWJjMTIz

  • 3. Using openssl or sodium extensions for real encryption

  • For scenarios with high security requirements, you should use actual encryption methods, such as:

  • $ciphertext = openssl_encrypt("abc123", "AES-128-CTR", "secretkey", 0, "1234567891011121");

  • echo $ciphertext;

  • This way, not only letters and digits, but the entire structure is hidden.

  • V. Conclusion

  • str_rot13 is a quick method for letter obfuscation, but it does not handle numbers. If you want to "encrypt" numbers as well,

  • additional logic, such as ROT5, Base64, or more advanced encryption methods, is needed. Understanding the behavior of str_rot13

  • will help you choose the right tool for different application scenarios.
    */