So nun hier wieder etwas für alle
Verschlüsselungs-Erweiterung für CNetConn
also fangen wir mal an^^
datei namens crypto.h
Code:
/*
Cryptography extensions for CNetConn
Currently only implements simple XOR encoding
Dependencies: CString
*/
#ifndef _CRYPTO_H_INC_
#define _CRYPTO_H_INC_
#include <windows.h>
#include "string.h"
#define CRYPT_NONE 0
#define CRYPT_XOR 1
#define CRYPT_STD_KEY "A"
/*
* class CCrypto
*
* En/decrypts generic data e.g. for network communication or secure file storage
*
*/
class CCrypto
{
public:
// Main constructor
// Initializes class and some variables
CCrypto();
// Main destructor
// Frees memory and terminates app
~CCrypto();
int SetMethod(int iMethod);
CString Encrypt(CString strData);
CString Decrypt(CString strData);
CString EncryptXOR(CString, CString);
CString DecryptXOR(CString, CString);
private:
int m_iMethod;
};
#endif // ifdef _CRYPTO_H_INC_
und nun die datei namens crypto.cpp
Code:
/*
Cryptography extensions for CNetConn
Currently only implements simple XOR encoding
Dependencies: CString
*/
#include "crypto.h"
/*
* default constructor
*
*/
CCrypto::CCrypto()
{
m_iMethod = CRYPT_NONE;
}
CCrypto::~CCrypto()
{
}
int CCrypto::SetMethod(int iMethod)
{
m_iMethod = iMethod;
return 0;
}
CString CCrypto::Encrypt(CString strData)
{
CString strStdKey = CRYPT_STD_KEY;
switch (m_iMethod)
{
case CRYPT_NONE:
return strData;
case CRYPT_XOR:
return EncryptXOR(strData, strStdKey);
default:
return strData;
}
}
CString CCrypto::Decrypt(CString strData)
{
CString strStdKey = CRYPT_STD_KEY;
switch (m_iMethod)
{
case CRYPT_NONE:
return strData;
case CRYPT_XOR:
return DecryptXOR(strData, strStdKey);
default:
return strData;
}
}
CString CCrypto::EncryptXOR(CString strData, CString strKey)
{
CString strOut;
int iPos;
int iKeyPos = 0;
int iLen = strData.GetLength();
int iKeyLen = strKey.GetLength();
if (!iKeyLen || !iLen) return strOut; // return empty String if data or key is empty
for (iPos = 0; iPos < iLen; iPos++)
{
strOut += strData[iPos] ^ strKey[iKeyPos++];
if (iKeyPos >= iKeyLen) iKeyPos = 0;
}
return strOut;
}
CString CCrypto::DecryptXOR(CString strData, CString strKey)
{
return EncryptXOR(strData, strKey);
}
viel spaß^^