Professional Documents
Culture Documents
////////////////////////////////////////////////////////////////////////////////
/////////////////////
// Function prototype
//DWORD WINAPI ServerThread(LPVOID lpParam);
//////////////////////////////////////////////////////////////////////////////
#define HASH_CREATE_ALGORITHM CALG_MD2 // MD2
// #define HASH_CREATE_ALGORITHM CALG_SHA // SHA
// #define HASH_CREATE_ALGORITHM CALG_MD5 // MD5
#define HASH_DERIVE_ALGORITHM CALG_RC2 //RC2
//#define HASH_DERIVE_ALGORITHM CALG_DES //DES
// Defined in lots of samples but rarely used
//#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
#define HASH_DERIVE_KEY_LENGHT 0x00280000 // 40 bits for RC_2
//#define HASH_DERIVE_KEY_LENGHT 0x00380000 // 56 bits for DES
/* Comment out next line if you want to use stream cipher */
#define USE_BLOCK_CIPHER
#ifdef USE_BLOCK_CIPHER
// defines for RC2 block cipher
#define ENCRYPT_ALGORITHM CALG_RC2
//#define DECRYPT_ALGORITHM CALG_DES
//#define ENCRYPT_ALGORITHM CALG_DES
//#define ENCRYPT_ALGORITHM CALG_RC4
//#define ENCRYPT_BLOCK_SIZE 8
#define ENCRYPT_BLOCK_SIZE 3
#else
// defines for RC4 stream cipher
#define DECRYPT_ALGORITHM CALG_RC4
#define DECRYPT_BLOCK_SIZE 1
#endif
/****************************************************************************/
LPSTR glueEncr(LPSTR key, LPSTR PlainText, LPSTR buffer, DWORD & dataLen, DWORD
buffLen);
LPSTR glueDecr(LPSTR key, LPSTR CipherText, LPSTR buffer, DWORD& dataLen);
int initCrypt(HCRYPTPROV &hCryptProv, HCRYPTHASH &hHash,
HCRYPTKEY &hKey, LPSTR key);
void cleanUp(HCRYPTPROV &hCryptProv, HCRYPTHASH &hHash, HCRYPTKEY &hKey);
void checkErr();
// build up crypto objects
// on error cleanup and return negative
// 1 if ok
int initCrypt(HCRYPTPROV &hCryptProv, HCRYPTHASH &hHash,
HCRYPTKEY &hKey, LPSTR key)
{
hCryptProv = 0;
hHash = 0;
hKey = 0;
// Get handle to the default provider.
// try MS_DEF_PROV, MS_STRONG_PROV, MS_ENHANCED_PROV
// try NULL instead of "altoth" (my username)
// try CRYPT_NEWKEYSET instead of 0
if (!CryptAcquireContext(&hCryptProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
CRYPT_NEWKEYSET))
{
if (!CryptAcquireContext(&hCryptProv, NULL, MS_DEF_PROV, PROV_RS
A_FULL, 0))
{
checkErr();
cleanUp(hCryptProv, hHash, hKey);
return -1;
}
}
checkErr();
// Create a hash object, hHash
if (!CryptCreateHash(hCryptProv, HASH_CREATE_ALGORITHM, 0, 0, &hHash))
{
checkErr();
cleanUp(hCryptProv, hHash, hKey);
return -2;
}
checkErr();
// Use the hash object, hHash, to hash the password.
if (!CryptHashData(hHash, (PBYTE)key, strlen(key)+1, 0))
{
checkErr();
cleanUp(hCryptProv, hHash, hKey);
return -3;
}
checkErr();
// Derive a session key, hKey, from the hash object, hHash.
// try CRYPT_NO_SALT and explicit HASH_DERIVE_KEY_LENGHT
if (!CryptDeriveKey(hCryptProv, HASH_DERIVE_ALGORITHM, hHash,
HASH_DERIVE_KEY_LENGHT | CRYPT_EXPORTABLE | CRYPT_NO_SALT, &hKey
))
{
checkErr();
cleanUp(hCryptProv, hHash, hKey);
return -4;
}
checkErr();
return 1;
}
//
// Function: main
//
// Description:
// Parse the command line and attempt to connect to the given server.
// The client will attempt to connect to each address returned by the
// getaddrinfo call until one succeeds afterwhich it will initiate
// echoing the data. Once the requested number of sends are issued,
// the connection is closed. For UDP, this is simply connecting the
// UDP socket to and endpoint and issuing the requested number of
// datagram sends and receives.
//
int __cdecl main(int argc, char *argv[])
{
WSADATA wsaData;
SOCKET conn_socket = INVALID_SOCKET;
struct addrinfo *results = NULL,
*addrptr = NULL,
hints;
char *server_name = "localhost",
*port = DEFAULT_PORT,
Buffer[DEFAULT_BUFFER_LEN],
cipher_text[DEFAULT_BUFFER_LEN],
hoststr[NI_MAXHOST],
servstr[NI_MAXSERV];
int address_family = AF_UNSPEC,
socket_type = DEFAULT_PROTO;
int retval,
loopflag = 0,
loopcount,
maxloop = -1,
i;
///////////////////////////////////////////////////////
SetLastError(0);
char *passwd = "1"; // Password is used as a key
//char plain_text[SIZE];
//char cipher_text[SIZE];
char new_plain_text[SIZE];
char new_plain_text1[SIZE];
char Buffer_P[DEFAULT_BUFFER_LEN];
char Buffer_C[SIZE];
char Buffer_T[10]="123";
//memset(plain_text, 0, SIZE);
memset(new_plain_text, 0, DEFAULT_BUFFER_LEN);
memset(Buffer_C, 0, DEFAULT_BUFFER_LEN);
//memset(Buffer_T, 0, DEFAULT_BUFFER_LEN);
///////////////////////////////////////////////////////
// Parse the command line
if (argc >1)
{
for (i=1; i < argc; i++)
{
if ( (strlen(argv[i]) == 2) && ((argv[i][0] == '-') || (argv[i][0] =
= '/') ) )
{
switch (tolower(argv[i][1]))
{
case '4': // Force IPv4
address_family = AF_INET;
break;
case '6': // Force IPv6
address_family = AF_INET6;
break;
case 'p': // Protocol (UDP or TCP)
if (!_strnicmp(argv[i+1], "TCP", 3) )
socket_type = SOCK_STREAM;
else if (!_strnicmp(argv[i+1], "UDP", 3) )
socket_type = SOCK_DGRAM;
else
Usage(argv[0]);
i++;
break;
case 'n': // Server name to connect to
server_name = argv[++i];
break;
case 'e': // Port number to connect to
port = argv[++i];
break;
case 'l': // Number of iterations
loopflag = 1;
if (i+1 >= argc)
Usage(argv[0]);
if (argv[i+1][0] != '-')
maxloop = atoi(argv[i+1]);
else
maxloop = -1;
i++;
break;
default:
Usage(argv[0]);
break;
}
}
else
Usage(argv[0]);
}
}
// Load Winsock
if ((retval = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
{
fprintf(stderr,"WSAStartup failed with error %d\n",retval);
WSACleanup();
return -1;
}
// Make sure the wildcard port wasn't specified
if (_strnicmp(port, "0", 1) == 0)
Usage(argv[0]);
//
// Resolve the server name
//
memset(&hints, 0, sizeof(hints));
hints.ai_family = address_family;
hints.ai_socktype = socket_type;
hints.ai_protocol = ((socket_type == SOCK_STREAM) ? IPPROTO_TCP : IPPROTO_UD
P);
retval = getaddrinfo(
server_name,
port,
&hints,
&results
);
if (retval != 0)
{
fprintf(stderr, "getaddrinfo failed: %d\n", retval);
goto cleanup;
}
// Make sure we got at least one address
if (results == NULL)
{
fprintf(stderr, "Server (%s) name could not be resolved!\n", server_name
);
goto cleanup;
}
//
// Walk through the list of addresses returned and connect to each one.
// Take the first successful connection.
//
addrptr = results;
while (addrptr)
{
conn_socket = socket(addrptr->ai_family, addrptr->ai_socktype, addrptr->
ai_protocol);
if (conn_socket == INVALID_SOCKET)
{
fprintf(stderr, "socket failed: %d\n", WSAGetLastError());
goto cleanup;
}
//
// Notice that nothing in this code is specific to whether we
// are using UDP or TCP.
// We achieve this by using a simple trick.
// When connect() is called on a datagram socket, it does not
// actually establish the connection as a stream (TCP) socket
// would. Instead, TCP/IP establishes the remote half of the
// ( LocalIPAddress, LocalPort, RemoteIP, RemotePort) mapping.
// This enables us to use send() and recv() on datagram sockets,
// instead of recvfrom() and sendto()
retval = getnameinfo(
addrptr->ai_addr,
(socklen_t)addrptr->ai_addrlen,
hoststr,
NI_MAXHOST,
servstr,
NI_MAXSERV,
NI_NUMERICHOST | NI_NUMERICSERV
);
if (retval != 0)
{
fprintf(stderr, "getnameinfo failed: %d\n", retval);
goto cleanup;
}
printf("Client attempting connection to: %s port: %s\n", hoststr, servst
r);
retval = connect(conn_socket, addrptr->ai_addr, (int)addrptr->ai_addrlen
);
if (retval == SOCKET_ERROR)
{
closesocket(conn_socket);
conn_socket = INVALID_SOCKET;
addrptr = addrptr->ai_next;
}
else
{
break;
}
}
freeaddrinfo(results);
results = NULL;
// Make sure we got a connection established
if (conn_socket == INVALID_SOCKET)
{
printf("Unable to establish connection...\n");
goto cleanup;
}
else
{
printf("Connection established...\n");
}
////////////EXTRE IMPORTANT//////////////////////////////////
memset(Buffer, 0, DEFAULT_BUFFER_LEN);
////Clear Buffer before using, otherwise, null will occur////
spc_rand((unsigned char *)Buffer, 7);
//printf("Buffer is %s\n", Buffer);
loopcount = 0;
for(;;)
{
//printf("Plaintext is %s\n", Buffer);
//_snprintf(Buffer, SIZE-1,
// "This is a small test message [number %d]",
//loopcount++
//);
// Send the data
retval = send(conn_socket, Buffer, DEFAULT_BUFFER_LEN, 0);
if (retval == SOCKET_ERROR)
{
fprintf(stderr,"send failed: error %d\n", WSAGetLastError());
goto cleanup;
}
printf("wrote %d bytes, Plaintext = [%s]\n",retval,Buffer);
strcpy(Buffer_P, Buffer);