Raised This Month: $32 Target: $400
 8% 

[INC] Crypt


Post New Thread Reply   
 
Thread Tools Display Modes
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, 618 views)
__________________

Last edited by Bugsy; 06-13-2012 at 11:33.
Bugsy is offline
teol
Veteran Member
Join Date: Oct 2009
Location: Marbella
Old 10-12-2011 , 01:00   Re: [INC] Crypt
Reply With Quote #2

Have you got the code in PHP or Python or C# ? VB is just useless for me ^^"
teol is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-12-2011 , 07:06   Re: [INC] Crypt
Reply With Quote #3

Added php (by Xellath)
__________________

Last edited by Bugsy; 10-12-2011 at 09:11.
Bugsy is offline
teol
Veteran Member
Join Date: Oct 2009
Location: Marbella
Old 10-12-2011 , 10:07   Re: [INC] Crypt
Reply With Quote #4

Thanks a lot
teol is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-13-2012 , 11:34   Re: [INC] Crypt
Reply With Quote #5

Added C# (by OvidiuS)
__________________
Bugsy is offline
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 06-13-2012 , 20:19   Re: [INC] Crypt
Reply With Quote #6

That, IMHO, is an INSANELY easy encryption algorithm. Might i suggest hashing, or even a bitwise encryption method. It'd be simple to decrypt string where each letter is just the letter before.

Even better....do it in blocks and use the key as a bitwise encryptor, moving each block as per the key's point of interest. If that makes sense....

EDIT: though i do see how that makes it simple to decrypt using the same algorithm. But that makes its strength its greatest weakness.
I wrote a bitwise encryption in Java a couple years back. Not sure if i even finished it, but i liked where it was going.
__________________
What an elegant solution to a problem that doesn't need solving....

Last edited by Liverwiz; 06-13-2012 at 20:25.
Liverwiz is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-13-2012 , 20:24   Re: [INC] Crypt
Reply With Quote #7

It isn't meant to be secure by any means, it's just a basic encryption.
__________________
Bugsy is offline
K.K.Lv
Veteran Member
Join Date: Aug 2008
Location: GameFolder
Old 06-14-2012 , 23:27   Re: [INC] Crypt
Reply With Quote #8

only Encrypt the string ?
__________________
QQ:116268742
K.K.Lv is offline
Send a message via MSN to K.K.Lv
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-15-2012 , 01:21   Re: [INC] Crypt
Reply With Quote #9

Quote:
Originally Posted by K.K.Lv View Post
only Encrypt the string ?
CryptString( Crypt , "key" ) - This will toggle-crypt the string (encrypt->decrypt, decrypt->encrypt).
__________________
Bugsy is offline
lucas_7_94
Leche Loco
Join Date: Mar 2009
Location: Argentina
Old 06-15-2012 , 13:33   Re: [INC] Crypt
Reply With Quote #10

I remember when you helped me with the Visual example, really god job bugsy.
__________________
ATWWMH - MiniDuels
Madness is like gravity, just need a little push.
lucas_7_94 is offline
Send a message via Skype™ to lucas_7_94
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 16:46.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode