آموزش های این وب سایت به صورت رایگان در دسترس است. اطلاعات بیشتر
بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]

این مطلب صدهزار بازدید کننده داشته! Two-Way-Encryption-With-PHP-Mcrypt

محمد  11 سال پیش  11 سال پیش
0 0

 

افرادی ک زبانشون خوبه ترجمه کنند لطفا من فقط میتونم بفهمم چی میگه به صورت تقریبی ;)

This class provides the functionality to encrypt and decrypt a text string. The class makes use of the PHP mcrypt extension which provides the ability to create two way encryption, or decoding of text messages.

<?php

// make it of break it
error_reporting(E_ALL);

/**
* Class to provide 2 way encryption of data
*
* @author    Kevin Waterson
* @copyright    2009    PHPRO.ORG
*
*/
class proCrypt 
{
    
/**
    *
    * This is called when we wish to set a variable
    *
    * @access    public
    * @param    string    $name
    * @param    string    $value
    *
    */
    
public function __set( $name, $value )
    {
        switch( $name)
        {
            case 
'key':
            case 
'ivs':
            case 
'iv':
            
$this->$name = $value;
            break;

            default:
            
throw new Exception( "$name cannot be set" );
        }
    }

    
/**
    *
    * Gettor - This is called when an non existant variable is called
    *
    * @access    public
    * @param    string    $name
    *
    */
   
 public function __get( $name )
    {
        switch( $name )
        {
            case 
'key':
            return 
'keee';

            case 
'ivs':
           
 return mcrypt_get_iv_size( MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB );

            case 
'iv':
            
return mcrypt_create_iv( $this->ivs );

            default:
            
throw new Exception( "$name cannot be called" );
        }
    }

    
/**
    *
    * Encrypt a string
    *
    * @access    public
    * @param    string    $text
    * @return    string    The encrypted string
    *
    */
   
 public function encrypt( $text )
    {
        
// add end of text delimiter
       
 $data = mcrypt_encrypt( MCRYPT_RIJNDAEL_128, $this->key, $text, MCRYPT_MODE_ECB, $this->iv );
        return base64_encode( $data );
    }
 
    
/**
    *
    * Decrypt a string
    *
    * @access    public
    * @param    string    $text
    * @return    string    The decrypted string
    *
    */
   
 public function decrypt( $text )
    {
        $text = base64_decode( $text );
        return mcrypt_decrypt( MCRYPT_RIJNDAEL_128, $this->key, $text, MCRYPT_MODE_ECB, $this->iv );
    }
// end of class

 

Example Usage

<?php

// a new proCrypt instance
$crypt = new proCrypt;

// encrypt the string
$encoded = $crypt->encrypt( 'my message');
echo $encoded."\n";

// decrypt the string
echo $crypt->decrypt( $encoded ) . "\n";

?>
.

 
 برای این سوال 1 پاسخ وجود دارد.
پاسخ به سوال 
محسن موحد  11 سال پیش
0 0

این اسکریپت ها غالبا با یک سرچ توی گوگل قابل دسترس هستن. بنابراین اگه آموزشی در موردش ندارین و صرف فقط معرفی کردن کار اضافیه. (مگر اینکه یک اسکریپت خاص باشه.)

این کلاسی هم که گذاشتید نکته ی خاصی نداره.


پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .