PMH4514
asked on
C++ XOR hashing a CString with printable characters
I need to perform a simple XOR "encryption" to hide a password from plain text.
I am unsure how to modify the attached snippet such that the result contains only printable characters (ie. that can be copied/pasted and entered manually)
thanks!
I am unsure how to modify the attached snippet such that the result contains only printable characters (ie. that can be copied/pasted and entered manually)
thanks!
CString XOREncrypt(CString a_sValue, CString a_sKey)
{
CString sRet=_T("");
sRet=a_sValue;
int i = 0;
int j = a_sValue.GetLength();
int k = 0;
int l = a_sKey.GetLength();
while (i < j)
{
sRet.SetAt(i, sRet.GetAt(i) ^ a_sKey.GetAt(k));
i++;
k++;
if (k >= l)
k = 0;
}
return sRet;
}
You could use the old UUencode or just try basE91: "basE91 is an advanced method for encoding binary data as ASCII characters. It is similar to UUencode or base64, but is more efficient." See basE91 encoding. This link has "Source code package (C, Java, PHP, 8086 assembly, AWK)"
ASKER
Actually I'm finding a Visual Basic example that uses Asc() and Hex() to do an ascii to hex conversion to accomplish the same task. The XOR encryption does the XOR and then hex() to get a printable result, and then to decrypt uses asc() to convert the next to ASCII.
what would be the equivalent for me to perform those ASCII to hex conversions on an MFC CString?
what would be the equivalent for me to perform those ASCII to hex conversions on an MFC CString?
I don't have MFC, so I looked up Need help converting CString to hex for you. It has various code snippets that should help. Below is one snippet to try out.
LPSTR p = m_strMessage.GetBuffer(1);
int len = m_strMessage.GetLength();
for (int n = 0 ; n < len ; n++) {
TCHAR tmp[4] = { 0 };
_stprintf(tmp, TEXT("%02x "), p[n]);
m_strOutput += tmp;
}
m_strMessage.ReleaseBuffer();
ASKER
Nice I found myself reading the same link!
!!
ASKER
I'm not finding any details anywhere on how to then convert it back.
for reference attached is the VB code I am trying to reproduce in C++.. It doesn't have to use MFC actually.
for reference attached is the VB code I am trying to reproduce in C++.. It doesn't have to use MFC actually.
Public Function XORDecryption(CodeKey As String, DataIn As String) As String
Dim lonDataPtr As Long
Dim strDataOut As String
Dim intXOrValue1 As Integer
Dim intXOrValue2 As Integer
For lonDataPtr = 1 To (Len(DataIn) / 2)
'The first value to be XOr-ed comes from the data to be encrypted
intXOrValue1 = Val("&H" & (Mid$(DataIn, (2 * lonDataPtr) - 1, 2)))
'The second value comes from the code key
intXOrValue2 = Asc(Mid$(CodeKey, ((lonDataPtr Mod Len(CodeKey)) + 1), 1))
strDataOut = strDataOut + Chr(intXOrValue1 Xor intXOrValue2)
Next lonDataPtr
XORDecryption = strDataOut
End Function
Public Function XOREncryption(CodeKey As String, DataIn As String) As String
Dim lonDataPtr As Long
Dim strDataOut As String
Dim temp As Integer
Dim tempstring As String
Dim intXOrValue1 As Integer
Dim intXOrValue2 As Integer
For lonDataPtr = 1 To Len(DataIn)
'The first value to be XOr-ed comes from the data to be encrypted
intXOrValue1 = Asc(Mid$(DataIn, lonDataPtr, 1))
'The second value comes from the code key
intXOrValue2 = Asc(Mid$(CodeKey, ((lonDataPtr Mod Len(CodeKey)) + 1), 1))
temp = (intXOrValue1 Xor intXOrValue2)
tempstring = Hex(temp)
If Len(tempstring) = 1 Then tempstring = "0" & tempstring
strDataOut = strDataOut + tempstring
Next lonDataPtr
XOREncryption = strDataOut
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
very nice I understand. Thank you kindly for your guidance.
You are very welcome. Glad it worked out.
Where there's a "%2x", maybe it would be better to make it a "%02x". And for functions whose arguments are input, it is better to add the "const" qualifier.
Where there's a "%2x", maybe it would be better to make it a "%02x". And for functions whose arguments are input, it is better to add the "const" qualifier.
ASKER
Thank you