AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
|
10-11-2011
, 21:33
[INC] Crypt
|
#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_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 by bugsy
PHP Code:
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 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 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# 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(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;
}
__________________
Last edited by Bugsy; 06-13-2012 at 11:33.
|
|