Thread: [INC] Crypt
View Single Post
Author Message
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-11-2011 , 21:33   [INC] Crypt
Reply With Quote #1

Crypt
This is a simple xor encrypt\decrypt method. It is available here in the thread to copy\paste or as an include file. I wrote it for someone by request but figured others may be able to use it. Also included below is an equivalent in VB, php, and C.

Commands
  • CreateCrypt( Crypt , "your string" ) - Create a crypt string; this basically copies a string, it's size, and encrypt\decrypt status into a struct which can then be used in CryptString() function. Crypt is a variable sized by the CryptInfo enum: new Crypt[ CryptInfo ]
  • CryptString( Crypt , "key" ) - This will toggle-crypt the string (encrypt->decrypt, decrypt->encrypt).

Code/Example
PHP Code:
#include <amxmodx>

new const Version[] = "0.1";

const 
CRYPT_MAX_LEN 512;

enum CryptInfo
{
    
C_Len,
    
C_StringCRYPT_MAX_LEN ],
    
bool:C_IsEncrypted
}

public 
plugin_init()
{
    
register_plugin"Crypt Example" Version "bugsy" );
    
    new 
CryptCryptInfo ];

    if ( 
CreateCryptCrypt "This is the string I want to [en\de]crypt" ) )
    {
        
server_print"Original: [%s] [Len=%d] [Encrypted? %s]" CryptC_String ] , CryptC_Len ] , CryptC_IsEncrypted ] ? "Yes" "No" );
    
        
CryptStringCrypt "this is a key" );
        
server_print"Encrypted: [%s] [Len=%d] [Encrypted? %s]" CryptC_String ] , CryptC_Len ] , CryptC_IsEncrypted ] ? "Yes" "No" );
    
        
CryptStringCrypt "this is a key" );
        
server_print"Decrypted: [%s] [Len=%d] [Encrypted? %s]" CryptC_String ] , CryptC_Len ] , CryptC_IsEncrypted ] ? "Yes" "No" );
    }
}

CreateCryptCryptCryptInfo ] , const szSource[] )
{
    if ( ( 
CryptC_Len ] = strlenszSource ) ) > CRYPT_MAX_LEN )
        return ( 
CryptC_Len ] = );
    
    
CryptC_IsEncrypted ] = false;
    return 
copyCryptC_String ] , clampCryptC_Len ] , CRYPT_MAX_LEN ) , szSource );
}

CryptStringCryptCryptInfo ] , const szKey[] )
{
    new 
iSrcPos iKeyPos iKeyLen;
    
    if ( !( 
iKeyLen strlenszKey ) ) )
        return 
0;

    for ( 
iSrcPos iKeyPos iSrcPos CryptC_Len ] ; iSrcPos++ )
    {
        
CryptC_String ][ iSrcPos ] = CryptC_String ][ iSrcPos ] ^ szKeyiKeyPos ];
    
        if ( ++
iKeyPos == iKeyLen )
            
iKeyPos 0;
    }
    
    
//I get a warning with: var = !var
    
CryptC_IsEncrypted ] = CryptC_IsEncrypted ] ? false true;

    return 
iSrcPos;

Visual Basic by bugsy
PHP Code:
Private Function Crypt(ByVal szText As StringByVal szKey As String) As String
Dim szEncrypt 
As StringiPos As IntegeriKeyPos As Integer

szEncrypt 
szText

iKeyPos 
1
For iPos 1 To Len(szText)
    
Mid(szEncryptiPos1) = Chr(Asc(Mid(szEncryptiPos1)) Xor Asc(Mid(szKeyiKeyPos1)))
    
    
iKeyPos iKeyPos 1
    
If (iKeyPos Len(szKey)) Then iKeyPos 1
Next iPos
Crypt 
szEncrypt
End 
Function

Dim szTest As String
szTest 
"This is a test string."
MsgBox szTest
szTest 
Cypt(szTest"$#@@!@#")
MsgBox szTest
szTest 
Crypt(szTest"$#@@!@#")
MsgBox szTest
szTest 
Crypt(szTest"$#@@!@#")
MsgBox szTest
szTest 
Crypt(szTest"$#@@!@#")
MsgBox szTest 
php by Xellath
live sample - http://testmepls.hostoi.com/encrypt.php
PHP Code:
function EncryptString$szSource$szKey )
{
    
$szDest $szSource;
    
    
$iKeyLen strlen$szKey ); 
    for( 
$iPos 0$iKeyPos 0$iPos strlen$szSource ); $iPos++ ) 
    { 
        
$szDest$iPos ] = $szSource$iPos ] ^ $szKey$iKeyPos ]; 
     
        if ( ++
$iKeyPos == $iKeyLen 
        {
            
$iKeyPos 0;
        }
    } 
    
    return 
$szDest;
}

$szTest "Hello, this is a test string"
echo 
"Original: " $szTest "<br>";
$szString EncryptString$szTest"$#@@!@#" );
echo 
"Encrypted: " $szString "<br>";
$szString EncryptString$szString"$#@@!@#" );
echo 
"Decrypted: " $szString ""
C originally by micapat, modified by bugsy
PHP Code:
void CryptStringchar *szString unsigned int iSize , const char *szKey )
{
    if( !
iSize 
        return;

    const 
char *szTmpKey szKey;

    while( 
iSize-- )
    {
        *
szString = *szString++ ^ *szKey++;
 
        if( !*
szKey )
            
szKey szTmpKey;
    }
}

int main()
{
    
char string[] = { "Hello, this is a test string" };
    
int iLen strlen( string );

    
printf"Original : %s [Len=%d]\n" string iLen );
    
CryptStringstring iLen "$#@@!@#" );
    
printf"Encrypted: %s [Len=%d]\n"string iLen );
    
CryptStringstring iLen "$#@@!@#" );
    
printf"Decrypted: %s [Len=%d]\n" string iLen );
 
    return 
0;

C# by OvidiuS
PHP Code:
static void Main(string[] args)
{
    
string szTest "Hello, this is a test string";
    
string szKey "$#@@!@#";
    
    
char[] cTest szTest.ToCharArray();
    
char[] cKey szKey.ToCharArray();
    
    
Console.WriteLine("Original: {0}"szTest);
    
Console.Write("Encrypted: ");
    
Console.WriteLine(EncryptString(cTestszTest.LengthcTestcKey));
    
Console.Write("Decrypted: ");
    
Console.WriteLine(EncryptString(cTestszTest.LengthcTestcKey));
    
    
Console.ReadLine();
}

static 
char[] EncryptString(char[] szSourceint iLenchar[] szDestchar[] szKey)
{
    
int iPosiKeyPosiKeyLen szKey.Length;
    
iKeyPos 0;
    
    for (
iPos 0iPos iLeniPos++)
    {
        
szDest[iPos] = (char)(szSource[iPos] ^ szKey[iKeyPos]);
    
        if (++
iKeyPos == iKeyLen)
            
iKeyPos 0;
    }
    return 
szDest;

Attached Files
File Type: inc crypt.inc (1.0 KB, 633 views)
__________________

Last edited by Bugsy; 06-13-2012 at 11:33.
Bugsy is offline