简体   繁体   English

简单的可逆字符串加密类

[英]Simple reversible encrypt class for strings

I use the following class in my CakePHP app to create reversible encrypted ids: 我在CakePHP应用程序中使用以下类创建可逆的加密ID:

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. 我想要一个完全独立并且不依赖于其他文件并且可以轻松集成到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); 它必须使用简单:例如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. 它依赖于openssl,因此如果您需要将其便携式,则可能不合适。

<?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). 如果您需要加密数字,则可以使用这些简单的函数(将_map_key函数中的随机字母更改为所需的任意字母,只需确保它们是唯一的即可)。 If you do not have numbers but string, you can STR2BIN them, then use this function: 如果没有数字而是字符串,则可以对它们进行STR2BIN,然后使用以下函数:

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 . 有标准的加密原语等的各种纯PHP实现AES ,你可以使用,像这样这样一个

The RC4 algorithm, in particular, is something you could implement in about a dozen lines of PHP if you wanted. 尤其是RC4算法,如果需要,您可以在大约12行PHP中实现。

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". 通常,在使用PHP之类的高级语言进行加密时,您可以选择“安全”,“快速”和“自包含”中的任意两个。


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: 好的,如果您想要的只是混淆,这是基于此示例的内容, 该示例基于php.net上的“ NOSPAM jamit dot com的彼得”:

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. 查看MCrypt函数。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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