Page 1 of 1

[INC] Crypt

Posted: Sat Oct 03, 2026 9:04 pm
by Archive-Poster
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

Code: Select all

#include <amxmodx>

new const Version[] = "0.1";

const CRYPT_MAX_LEN = 512;

enum CryptInfo
{
    C_Len,
    C_String[ CRYPT_MAX_LEN + 1 ],
    bool:C_IsEncrypted
}

public plugin_init()
{
    register_plugin( "Crypt Example" , Version , "bugsy" );
    
    new Crypt[ CryptInfo ];

    if ( CreateCrypt( Crypt , "This is the string I want to [en\de]crypt" ) )
    {
        server_print( "Original: [%s] [Len=%d] [Encrypted? %s]" , Crypt[ C_String ] , Crypt[ C_Len ] , Crypt[ C_IsEncrypted ] ? "Yes" : "No" );
    
        CryptString( Crypt , "this is a key" );
        server_print( "Encrypted: [%s] [Len=%d] [Encrypted? %s]" , Crypt[ C_String ] , Crypt[ C_Len ] , Crypt[ C_IsEncrypted ] ? "Yes" : "No" );
    
        CryptString( Crypt , "this is a key" );
        server_print( "Decrypted: [%s] [Len=%d] [Encrypted? %s]" , Crypt[ C_String ] , Crypt[ C_Len ] , Crypt[ C_IsEncrypted ] ? "Yes" : "No" );
    }
}

CreateCrypt( Crypt[ CryptInfo ] , const szSource[] )
{
    if ( ( Crypt[ C_Len ] = strlen( szSource ) ) > CRYPT_MAX_LEN )
        return ( Crypt[ C_Len ] = 0 );
    
    Crypt[ C_IsEncrypted ] = false;
    return copy( Crypt[ C_String ] , clamp( Crypt[ C_Len ] , 1 , CRYPT_MAX_LEN ) , szSource );
}

CryptString( Crypt[ CryptInfo ] , const szKey[] )
{
    new iSrcPos , iKeyPos , iKeyLen;
    
    if ( !( iKeyLen = strlen( szKey ) ) )
        return 0;

    for ( iSrcPos = 0 , iKeyPos = 0 ; iSrcPos < Crypt[ C_Len ] ; iSrcPos++ )
    {
        Crypt[ C_String ][ iSrcPos ] = Crypt[ C_String ][ iSrcPos ] ^ szKey[ iKeyPos ];
    
        if ( ++iKeyPos == iKeyLen )
            iKeyPos = 0;
    }
    
    //I get a warning with: var = !var
    Crypt[ C_IsEncrypted ] = Crypt[ C_IsEncrypted ] ? false : true;

    return iSrcPos;
} 
Visual Basic

Code: Select all

Private Function Crypt(ByVal szText As String, ByVal szKey As String) As String
Dim szEncrypt As String, iPos As Integer, iKeyPos As Integer

szEncrypt = szText

iKeyPos = 1
For iPos = 1 To Len(szText)
    Mid(szEncrypt, iPos, 1) = Chr(Asc(Mid(szEncrypt, iPos, 1)) Xor Asc(Mid(szKey, iKeyPos, 1)))
    
    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

Code: Select all

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

Code: Select all

void CryptString( char *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 );
    CryptString( string , iLen , "$#@@!@#" );
    printf( "Encrypted: %s [Len=%d]\n", string , iLen );
    CryptString( string , iLen , "$#@@!@#" );
    printf( "Decrypted: %s [Len=%d]\n" , string , iLen );
 
    return 0;
} 
C#

Code: Select all

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(cTest, szTest.Length, cTest, cKey));
    Console.Write("Decrypted: ");
    Console.WriteLine(EncryptString(cTest, szTest.Length, cTest, cKey));
    
    Console.ReadLine();
}

static char[] EncryptString(char[] szSource, int iLen, char[] szDest, char[] szKey)
{
    int iPos, iKeyPos, iKeyLen = szKey.Length;
    iKeyPos = 0;
    
    for (iPos = 0; iPos < iLen; iPos++)
    {
        szDest[iPos] = (char)(szSource[iPos] ^ szKey[iKeyPos]);
    
        if (++iKeyPos == iKeyLen)
            iKeyPos = 0;
    }
    return szDest;
}