XK8ER
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..
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..
Do you mean "encrypt" a string? Encoding and encryption are two different things. One is for changing representation; the other is for providing security.
ASKER
yes that's correct encrypt a string with a password.
If you don't need 'strong' encryption, the following will do:
This is a simple XOR encryption with one nice aspect - you can use it for the decryption as well.
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++;
}
}
This is a simple XOR encryption with one nice aspect - you can use it for the decryption as well.
ASKER
alright, this is what I have it compiles fine and all but the vb.net button wont work.
<DllImport("myFile.dll", CallingConvention:=Calling Convention .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(EncryptDecrypt String("th is is a string to encode", "Temp123564"))
End Sub
<DllImport("myFile.dll", CallingConvention:=Calling
Shared Function EncryptDecryptString(ByVal
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Debug.Print(EncryptDecrypt
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++;
}
}
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...
ASKER
how can I make it return tne encrypted string..
im not getting error now with this, but the return is empty
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
OK, try
But note that the returned string is not necessarily terminated by a NULL character, so that might be a problem....
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;
But note that the returned string is not necessarily terminated by a NULL character, so that might be a problem....
ASKER
I tried this two and got errors on both
return lpcbBuffer;
or
extern "C" __declspec(dllexport)
double GetEncryptDecryptString()
{
return lpcbBuffer;
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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
dGhpcyBpcyBhIHN0cmluZyB0by BlbmNvZGU=
anyone can decode the string.. that's an issue in facing right now...
when I encode base64 like this
dGhpcyBpcyBhIHN0cmluZyB0by
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...
BTW, the same would apply to any other encryption method as well...
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?
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
If you mean 'Convert.ToBase64String()' , yes, that is what I was thinking about.