Приглашаем посетить
Сомов (somov.lit-info.ru)

Encrypts a string

############################################################################
#                                                                          #
# scramble()                        Version 1.0                            #
# Written by Craig Patchett         craig@patchett.com                     #
# Created 11/1/96                   Last Modified 3/28/97                  #
#                                                                          #
# Copyright 1997 Craig Patchett & Matthew Wright.  All Rights Reserved.    #
# This subroutine is part of The CGI/Perl Cookbook from John Wiley & Sons. #
# License to use this program or install it on a server (in original or    #
# modified form) is granted only to those who have purchased a copy of The #
# CGI/Perl Cookbook. (This notice must remain as part of the source code.) #
#                                                                          #
# Function:      Encrypts a string based on a key so that the encrypted    #
#                string can only be unencrypted using the key. This is     #
#                based on a relatively simple algorithm and should not be  #
#                depended on where a high degree of security is required.  #
#                                                                          #
# Usage:         &scramble($text, $key);                                   #
#                                                                          #
# Variables:     $text --  String containing text to be encrypted. May     #
#                          contain any characters in the printable ASCII   #
#                          range (ASCII 32 - 126).                         #
#                          Example 'password'                              #
#                $key --   String containing text to use as a key for      #
#                          encrypting and unencrypting $text. May          #
#                          contain any characters, printable or not.       #
#                          Example 'anything'                              #
#                                                                          #
# Returns:       The encrypted string                                      #
#                                                                          #
# Uses Globals:  None                                                      #
#                                                                          #
# Files Created: None                                                      #
#                                                                          #
############################################################################


sub scramble {
    local($text, $key) = @_;
    local($response) = '';
    local($i, $j, $num, $result);
    
    # Save the string lengths to speed things up slightly
    
    local($text_len) = length($text);
    local($key_len) = length($key);
    
    # Process each character in $string
    
    for ($i = 0; $i < $text_len; ++$i) {
    
        # Convert the character to ASCII, offset from space
        
        $num = ord(chop($text)) - 32;
        
        # If $key is longer than $string, stack characters
        
        for ($j = $i; $j < $key_len; $j += $text_len) {
            
            # Offset the original character by the key character and key length
            
            $num += ord(substr($key, $j, 1)) + $key_len;
        }
        
        # Bring it back into the printable ASCII range
        
        $num = $num % 95 + 32;
        
        # Convert ':' where appropriate
        
        if ($num == 58) { $num = 127 };
        
        # Convert it to an ASCII character
        
        $result .= pack("c", $num);
    }
    
    # Return the result
    
    return($result);
}


############################################################################
#                                                                          #
# unscramble()                      Version 1.0                            #
# Written by Craig Patchett         craig@patchett.com                     #
# Created 11/1/96                   Last Modified 3/28/97                  #
#                                                                          #
# Copyright 1997 Craig Patchett & Matthew Wright.  All Rights Reserved.    #
# This subroutine is part of The CGI/Perl Cookbook from John Wiley & Sons. #
# License to use this program or install it on a server (in original or    #
# modified form) is granted only to those who have purchased a copy of The #
# CGI/Perl Cookbook. (This notice must remain as part of the source code.) #
#                                                                          #
# Function:      Encrypts a string bases on a key so that the encrypted    #
#                string can only be unencrypted using the key. This is     #
#                based on a relatively simple algorithm and should not be  #
#                depended on where a high degree of security is required.  #
#                                                                          #
# Usage:         &unscramble($text, $key);                                 #
#                                                                          #
# Variables:     $text --  String containing encrypted text.               #
#                $key --   String containing text to use as a key for      #
#                          unencrypting $text. Must be the same            #
#                          string that was used for encrypting $text       #
#                          Example 'anything'                              #
#                                                                          #
# Returns:       The unencrypted string (will only equal the original      #
#                string if the keys match                                  #
#                                                                          #
# Uses Globals:  None                                                      #
#                                                                          #
# Files Created: None                                                      #
#                                                                          #
############################################################################


sub unscramble {
    local($text, $key) = @_;
    local($response) = '';
    local($i, $j, $num, $result);
    
    # Save the string lengths to speed things up slightly
    
    local($text_len) = length($text);
    local($key_len) = length($key);
    
    # Process each character in $string
    
    for ($i = $text_len - 1; $i >= 0; --$i) {
    
        # Convert the character to ASCII
        
        $num = ord(chop($text));
        
        # Change back to ':' if appropriate
        
        if ($num == 127) { $num = 58 }
        
        # Convert to 0-95 range
        
        $num -= 32;
        
        # If $key is longer than $string, stack characters
        
        for ($j = $i; $j < $key_len; $j += $text_len) {
            
            # Offset the original character by the key character and key length
            
            $num -= ord(substr($key, $j, 1)) + $key_len;
        }
        
        # Bring it back into the printable ASCII range
        
        $num = $num % 95 + 32;
        
        # Convert it to an ASCII character
        
        $result .= pack("c", $num);
    }
    
    # Return the result
    
    return($result);
}

1;