Link to home
Create AccountLog in
Avatar of XK8ER
XK8ERFlag for United States of America

asked on

encode a string

Hello,
what would be the easiest way to encode a string with a password..
I am trying to use this C++ function in a dll file so that if I call the function like this

StringEncode("MyString", "MyPassword")

it will return the encoded string..
Avatar of kaufmed
kaufmed
Flag of United States of America image

Do you mean "encrypt" a string? Encoding and encryption are two different things. One is for changing representation; the other is for providing security.
Avatar of XK8ER

ASKER

yes that's correct encrypt a string with a password.
If you don't need 'strong' encryption, the following will do:

void	EncryptDecryptString	(	LPBYTE	lpcbBuffer,
                                    WORD	wBuffer,
                                    LPCTSTR	lpszPassword
                                )
{

    register	int	iPassword	=	strlen (lpszPassword);
    register	int	iSeed		=	1;
    register	int	i;
    register	int	j;

    /* initialize seed */
    for	(	i	=	0;	lpszPassword	[	i];	i++)
            iSeed	=	(	iSeed + lpszPassword	[	i] + i) % 254;

    ++iSeed;
 
    /* encrypt string */
    j	=	0;

    for (	i	=	0;	i	<	wBuffer;	i++)
        {
            iSeed	=	((( iSeed + ( i - ( int) wBuffer)) - 1) % 254) + 1;

            lpcbBuffer	[	i] = lpcbBuffer	[	i] ^ iSeed ^ lpszPassword	[	j];

            j	=	j >= iPassword ? 0 : j++;
        }
}

Open in new window


This is a simple XOR encryption with one nice aspect - you can use it for the decryption as well.
Avatar of XK8ER

ASKER

alright, this is what I have it compiles fine and all but the vb.net button wont work.

    <DllImport("myFile.dll", CallingConvention:=CallingConvention.Cdecl)> _
    Shared Function EncryptDecryptString(ByVal iString As String, ByVal iPass As String) As IntPtr
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Debug.Print(EncryptDecryptString("this is a string to encode", "Temp123564"))
    End Sub

extern "C" void __declspec(dllexport) 
EncryptDecryptString(LPBYTE lpcbBuffer, WORD wBuffer, LPCTSTR lpszPassword)
{
    register int iPassword = strlen (lpszPassword);
    register int iSeed  = 1;
    register int i;
    register int j;
    /* initialize seed */
    for ( i = 0; lpszPassword [ i]; i++)
            iSeed = ( iSeed + lpszPassword [ i] + i) % 254;
    ++iSeed; 
    /* encrypt string */
    j = 0;
    for ( i = 0; i < wBuffer; i++)
        {
            iSeed = ((( iSeed + ( i - ( int) wBuffer)) - 1) % 254) + 1;
            lpcbBuffer [ i] = lpcbBuffer [ i] ^ iSeed ^ lpszPassword [ j];
            j = j >= iPassword ? 0 : j++;
        }
}

Open in new window

You are missing the 2nd parameter, that should be the length of the string. Also, it'd be better to use a string variable and not a literal. Plus: The function returns nothing, you will have to call it and can inspect the result after it returns...
Avatar of XK8ER

ASKER

how can I make it return tne encrypted string..
im not getting error now with this, but the return is empty

    <DllImport("myFile.dll", CallingConvention:=CallingConvention.Cdecl)> _
    Shared Function EncryptDecryptString(ByVal iString As String, ByVal iLen As Integer, ByVal iPass As String) As IntPtr
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim daString As String = "this is a string to encode"
        Debug.Print(Marshal.PtrToStringAnsi(EncryptDecryptString(daString, daString.Length, "Temp123564")))
    End Sub

Open in new window

OK, try

extern "C" LPBYTE* __declspec(dllexport) 
EncryptDecryptString(LPBYTE lpcbBuffer, WORD wBuffer, LPCTSTR lpszPassword)
{
    register int iPassword = strlen (lpszPassword);
    register int iSeed  = 1;
    register int i;
    register int j;
    /* initialize seed */
    for ( i = 0; lpszPassword [ i]; i++)
            iSeed = ( iSeed + lpszPassword [ i] + i) % 254;
    ++iSeed; 
    /* encrypt string */
    j = 0;
    for ( i = 0; i < wBuffer; i++)
        {
            iSeed = ((( iSeed + ( i - ( int) wBuffer)) - 1) % 254) + 1;
            lpcbBuffer [ i] = lpcbBuffer [ i] ^ iSeed ^ lpszPassword [ j];
            j = j >= iPassword ? 0 : j++;
        }
}
return lpcbBuffer;                                            

Open in new window


But note that the returned string is not necessarily terminated by a NULL character, so that might be a problem....
Avatar of XK8ER

ASKER

I tried this two and got errors on both

return lpcbBuffer;

or


extern "C" __declspec(dllexport) 
double GetEncryptDecryptString()
{
    return lpcbBuffer;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of XK8ER

ASKER

well I am looking for a way to encrypt a string.. sort of like base64 but with a password so it can only be decoded with it..

when I encode base64 like this
dGhpcyBpcyBhIHN0cmluZyB0byBlbmNvZGU=
anyone can decode the string.. that's an issue in facing right now...
I know what you mean - but why not just base64-encode the string after the encryption has been done and vice-versa? ;o)

BTW, the same would apply to any other encryption method as well...
Avatar of XK8ER

ASKER

I dont know if you are familiar with AES but I am using this function in VB.NET
and it looks like at the end it convert the string into a base64..
so is this technique what you're talking about?

    Public Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String
            Dim AES As New System.Security.Cryptography.RijndaelManaged
            Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
            Dim encrypted As String = ""
            Dim hash(31) As Byte
            Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
            Array.Copy(temp, 0, hash, 0, 16)
            Array.Copy(temp, 0, hash, 15, 16)
            AES.Key = hash
            AES.Mode = System.Security.Cryptography.CipherMode.ECB
            Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
            Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
            encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
            Return encrypted
    End Sub

Open in new window

If you mean 'Convert.ToBase64String()', yes, that is what I was thinking about.