简体   繁体   中英

Simple reversible encrypt class for strings

I use the following class in my CakePHP app to create reversible encrypted ids:

class Tiny
{
    public static $set = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

    public static function toTiny($id)
    {
        $set = self::$set;

        $HexN="";
        $id = floor(abs(intval($id)));
        $radix = strlen($set);
        while (true)
        {
            $R=$id%$radix;
            $HexN = $set{$R}.$HexN;
            $id=($id-$R)/$radix;
            if ($id==0) break;
        }
        return $HexN;
    }

    public static function reverseTiny($str)
    {
        $set = self::$set;
        $radix = strlen($set);
        $strlen = strlen($str);
        $N = 0;
        for($i=0;$i<$strlen;$i++)
        {
            $N += strpos($set,$str{$i})*pow($radix,($strlen-$i-1));
        }
        return "{$N}";
    }
}

I'm looking for something just as simple for strings. Where it has a simple set variable and just two functions, one for encrypting and one for decrypting. All of the ones I have seen so far have been over-complicated and too framework-based. I want a class that is completely stand-alone and not reliant on other files and easily integrated into CakePHP.

Can anyone help? Does anyone know of one or could help me modify this script to work with strings instead so I can create another class. It needs to be simple to use: eg Class::encrypt(string);

Also security is not a major concern here as it's not being used for protecting anything rather just hashing a string in a way that can be reversed.

This is what I want:

class Encrypt
{
    public static $set = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

    public static function Encrypt($string)
    {
        $set = self::$set;
        return;
    }
    public static function Decrypt($string)
    {
        $set = self::$set;
        return;
    }
}

Here is a modified version of the code that I use. It relies on openssl, so it may not be suitable if you need it to be portable.

<?php

class Cipher {                                                                          
   public static function strongRand($num_bytes, $raw = false) {                        
      $rand = openssl_random_pseudo_bytes($num_bytes);                                  
      if (!$raw) {                                                                      
         $rand = base64_encode($rand);                                                  
      }
      return $rand;
   }

   public static function encrypt($data, $password = "default", $type = "aes128") {     
      $iv = Cipher::strongRand(12);                                                     
      $encrypted = openssl_encrypt($data, $type, $password, false, $iv);                
      return $iv . $encrypted;                                                          
   }      

   public static function decrypt($data, $password = "default", $type = "aes128") {     
      $iv = substr($data, 0, 16);
      $data = substr($data, 16);                                                        
      $decrypted = openssl_decrypt($data, $type, $password, false, $iv);                
      return $decrypted;
   }   
}

?>

An example of how to use the code:

<?php

include('cipher.php');

$enc = Cipher::encrypt('kachow');
echo $enc . "\n";

$dec = Cipher::decrypt($enc);
echo $dec . "\n";

?>

Outputs:

0fykYiBPJAL/C3fFuX+jApFRdRw7NY8uYmGaaQ==
kachow

If you need to crypt numbers, I use those simple functions (change the random letters in function _map_key to whatever you want, just be sure that they are unique). If you do not have numbers but string, you can STR2BIN them, then use this function:

function _map_kid($kid, $umap=false){
    $map = array('M','Y','S','I','M','P','L','E','K');
    $ret = '';
    for($i = 0; $i<strlen($kid); $i++){
        if($umap){
            $ret .= array_search(substr($kid,$i,1),$map);
        } else {
            $ret .= $map[substr($kid,$i,1)];
        }
    }
    return $ret;
}
function cript_customer_id($customer_id, $key=0){
    if($key==0){
        $key = trim(microtime(true));
    }
    $key = intval($key);
    if(strlen($key)<=3)     $key +=999;
    if(substr($key,-1)==0)  $key +=3;
    $key = substr($key,-3);
    $kid = ((($customer_id.substr($key,0,1))+substr($key,1,1))*substr($key,-1)).$key;
    return _map_kid($kid);
}

function _decript_customer_id($kid){
    if(trim($kid)=='') return false;
    $kid = strtoupper($kid);
    $kid = _qsc2_map_kid($kid, true);
    $key = substr($kid, -3);
    $customer_id = substr($kid, 0, -3);
    if(substr($key,-1)>0){
        $customer_id = substr((($customer_id/substr($key,-1))-substr($key,1,1)),0,-1);
    } else {
        $customer_id = 0;
    }
    return $customer_id;
}

There are various pure PHP implementations of standard crypto primitives like AES that you could use, like this or this one .

The RC4 algorithm, in particular, is something you could implement in about a dozen lines of PHP if you wanted.

However, those implementations are not likely to be very efficient; in general, when it comes to crypto in high-level languages like PHP, you can have any two out of "secure", "fast" and "self-contained".


OK, if all you want is obfuscation, here's something based on this example by "peter at NOSPAM jamit dot com" over at php.net:

class Obfuscate
{
    public static function obfuscate($string)
    {
        return str_rot13( base64_encode( $string ) );
    }
    public static function deobfuscate($string)
    {
        return base64_decode( str_rot13( $string ) );
    }
}

This should be fast and self-contained, but it provides very little security. At best it makes a nice little puzzle for amateur cryptographers, of a level perhaps comparable with a newspaper crossword puzzle.

Look into MCrypt functions. It'll be even shorter than the above code. And secure too!

我更喜欢使用mcrypt,但如果不想,请尝试在phpclasses.org上找到有用的东西,例如http://www.phpclasses.org/package/2995-PHP-Encode-and-decode-text -strings与-随机keys.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM