Click here to Skip to main content
15,894,540 members
Home / Discussions / C#
   

C#

 
AnswerRe: TopMost Window Pin
DigitalKing17-Jan-06 14:18
DigitalKing17-Jan-06 14:18 
Questionhey guys check this out Pin
abstarsss17-Jan-06 13:30
abstarsss17-Jan-06 13:30 
AnswerRe: hey guys check this out Pin
Christian Graus17-Jan-06 14:15
protectorChristian Graus17-Jan-06 14:15 
QuestionTripleDES encryption. Anyone want to help? Pin
jbergetun17-Jan-06 12:31
jbergetun17-Jan-06 12:31 
AnswerRe: TripleDES encryption. Anyone want to help? Pin
Colin Angus Mackay17-Jan-06 23:03
Colin Angus Mackay17-Jan-06 23:03 
GeneralRe: TripleDES encryption. Anyone want to help? Pin
jbergetun18-Jan-06 7:48
jbergetun18-Jan-06 7:48 
GeneralRe: TripleDES encryption. Anyone want to help? Pin
Colin Angus Mackay18-Jan-06 11:39
Colin Angus Mackay18-Jan-06 11:39 
GeneralRe: TripleDES encryption. Anyone want to help? Pin
jbergetun19-Jan-06 10:21
jbergetun19-Jan-06 10:21 
I found some source code for a client programmed in C.
Dont understand much but maybe someone else does.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <rpc des_crypt.h="">
#include <sys socket.h="">
#include <netinet in.h="">
#include <netdb.h>

#define CWS_NETMSGSIZE 240

typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned long long uint64;

#define CWS_FIRSTCMDNO 0xe0

typedef enum
{
MSG_CLIENT_2_SERVER_LOGIN = CWS_FIRSTCMDNO,
MSG_CLIENT_2_SERVER_LOGIN_ACK,
MSG_CLIENT_2_SERVER_LOGIN_NAK,
MSG_CARD_DATA_REQ,
MSG_CARD_DATA,
MSG_SERVER_2_CLIENT_NAME,
MSG_SERVER_2_CLIENT_NAME_ACK,
MSG_SERVER_2_CLIENT_NAME_NAK,
MSG_SERVER_2_CLIENT_LOGIN,
MSG_SERVER_2_CLIENT_LOGIN_ACK,
MSG_SERVER_2_CLIENT_LOGIN_NAK,
MSG_ADMIN,
MSG_ADMIN_ACK,
MSG_ADMIN_LOGIN,
MSG_ADMIN_LOGIN_ACK,
MSG_ADMIN_LOGIN_NAK,
MSG_ADMIN_COMMAND,
MSG_ADMIN_COMMAND_ACK,
MSG_ADMIN_COMMAND_NAK,
MSG_KEEPALIVE = CWS_FIRSTCMDNO + 0x1d
} net_msg_type_t;

typedef enum
{
COMMTYPE_CLIENT,
COMMTYPE_SERVER
} comm_type_t;

typedef struct customData_struct
{
uint16 sid;
} customData_t;

void des_key_parity_adjust(uint8 *key, uint8 len)
{
uint8 i, j, parity;

for (i = 0; i < len; i++)
{
parity = 1;
for (j = 1; j < 8; j++) if ((key[i] >> j) & 0x1) parity = ~parity & 0x01;
key[i] |= parity;
}
}

uint8 *des_key_spread(uint8 *normal)
{
static uint8 spread[16];

spread[ 0] = normal[ 0] & 0xfe;
spread[ 1] = ((normal[ 0] << 7) | (normal[ 1] >> 1)) & 0xfe;
spread[ 2] = ((normal[ 1] << 6) | (normal[ 2] >> 2)) & 0xfe;
spread[ 3] = ((normal[ 2] << 5) | (normal[ 3] >> 3)) & 0xfe;
spread[ 4] = ((normal[ 3] << 4) | (normal[ 4] >> 4)) & 0xfe;
spread[ 5] = ((normal[ 4] << 3) | (normal[ 5] >> 5)) & 0xfe;
spread[ 6] = ((normal[ 5] << 2) | (normal[ 6] >> 6)) & 0xfe;
spread[ 7] = normal[ 6] << 1;
spread[ 8] = normal[ 7] & 0xfe;
spread[ 9] = ((normal[ 7] << 7) | (normal[ 8] >> 1)) & 0xfe;
spread[10] = ((normal[ 8] << 6) | (normal[ 9] >> 2)) & 0xfe;
spread[11] = ((normal[ 9] << 5) | (normal[10] >> 3)) & 0xfe;
spread[12] = ((normal[10] << 4) | (normal[11] >> 4)) & 0xfe;
spread[13] = ((normal[11] << 3) | (normal[12] >> 5)) & 0xfe;
spread[14] = ((normal[12] << 2) | (normal[13] >> 6)) & 0xfe;
spread[15] = normal[13] << 1;

des_key_parity_adjust(spread, 16);
return spread;
}

void des_random_get(uint8 *buffer, uint8 len)
{
uint8 idx = 0;
int randomNo = 0;

for (idx = 0; idx < len; idx++)
{
if (!(idx % 3)) randomNo = rand();
buffer[idx] = (randomNo >> ((idx % 3) << 3)) & 0xff;
}
}

int des_encrypt(uint8 *buffer, int len, uint8 *deskey)
{
uint8 checksum = 0;
uint8 noPadBytes;
uint8 padBytes[7];
char ivec[8];
uint16 i;

if (!deskey) return len;
noPadBytes = (8 - ((len - 1) % 8)) % 8;
if (len + noPadBytes + 1 >= CWS_NETMSGSIZE-8) return -1;
des_random_get(padBytes, noPadBytes);
for (i = 0; i < noPadBytes; i++) buffer[len++] = padBytes[i];
for (i = 2; i < len; i++) checksum ^= buffer[i];
buffer[len++] = checksum;
des_random_get((uint8 *)ivec, 8);
memcpy(buffer+len, ivec, 8);
for (i = 2; i < len; i += 8)
{
cbc_crypt(deskey , (char *) buffer+i, 8, DES_ENCRYPT, ivec);
ecb_crypt(deskey+8, (char *) buffer+i, 8, DES_DECRYPT);
ecb_crypt(deskey , (char *) buffer+i, 8, DES_ENCRYPT);
memcpy(ivec, buffer+i, 8);
}
len += 8;
return len;
}

int des_decrypt(uint8 *buffer, int len, uint8 *deskey)
{
char ivec[8];
char nextIvec[8];
int i;
uint8 checksum = 0;

if (!deskey) return len;
if ((len-2) % 8 || (len-2) < 16) return -1;
len -= 8;
memcpy(nextIvec, buffer+len, 8);
for (i = 2; i < len; i += 8)
{
memcpy(ivec, nextIvec, 8);
memcpy(nextIvec, buffer+i, 8);
ecb_crypt(deskey , (char *) buffer+i, 8, DES_DECRYPT);
ecb_crypt(deskey+8, (char *) buffer+i, 8, DES_ENCRYPT);
cbc_crypt(deskey , (char *) buffer+i, 8, DES_DECRYPT, ivec);
}
for (i = 2; i < len; i++) checksum ^= buffer[i];
if (checksum) return -1;
return len;
}

uint8 *des_login_key_get(uint8 *key1, uint8 *key2)
{
uint8 des14[14];
static uint8 des16[16];
int i;

for (i = 0; i < 14; i++)
{
des14[i] = key1[i] ^ key2[i];
}

memcpy(des16, des_key_spread(des14), 16);
return des16;
}

int network_message_send(int handle, uint16 *netMsgId, customData_t *customData, uint8 *buffer, int len, uint8 *deskey, comm_type_t commType)
{
uint8 netbuf[CWS_NETMSGSIZE];

if (len < 3 || len + 12 > CWS_NETMSGSIZE || handle < 0) return -1;
buffer[1] = (buffer[1] & 0xf0) | (((len - 3) >> 8) & 0x0f);
buffer[2] = (len - 3) & 0xff;
memcpy(netbuf+12, buffer, len);
len += 12;
if (netMsgId) { if (commType == COMMTYPE_CLIENT) (*netMsgId)++; netbuf[2] = (*netMsgId) >> 8; netbuf[3] = (*netMsgId) & 0xff; }
else netbuf[2] = netbuf[3] = 0;
if (customData)
{
netbuf[4] = customData->sid >> 8;
netbuf[5] = customData->sid & 0xff;
memset(netbuf+6, 0, 6);
}
else memset(netbuf+4, 0, 8);
if ((len = des_encrypt(netbuf, len, deskey)) < 0) return -1;
netbuf[0] = (len - 2) >> 8;
netbuf[1] = (len - 2) & 0xff;
write(handle, netbuf, len);
return 0;
}

int network_message_receive(int handle, uint16 *netMsgId, customData_t *customData, uint8 *buffer, uint8 *deskey, comm_type_t commType)
{
int len;
uint8 netbuf[CWS_NETMSGSIZE];
int returnLen;

if (customData) memset(customData, 0, sizeof(customData_t));
if (!buffer || handle < 0) return -1;
len = read(handle, netbuf, 2);
if (!len) return 0;
if (len != 2) return -1;
if (((netbuf[0] << 8) | netbuf[1]) > CWS_NETMSGSIZE - 2) return -1;
len = read(handle, netbuf+2, (netbuf[0] << 8) | netbuf[1]);
if (!len) return 0;
if (len != ((netbuf[0] << 8) | netbuf[1])) return -1;
len += 2;
if ((len = des_decrypt(netbuf, len, deskey)) < 15) return -1;
if ((returnLen = (((netbuf[13] & 0x0f) << 8) | netbuf[14]) + 3) > len-12) return -1;
if (netMsgId)
{
switch (commType)
{
case COMMTYPE_SERVER:
*netMsgId = (netbuf[2] << 8) | netbuf[3];
break;

case COMMTYPE_CLIENT:
if (*netMsgId != ((netbuf[2] << 8) | netbuf[3])) return -1;
break;

default:
return -1;
break;
}
}
if (customData)
{
customData->sid = (netbuf[4] << 8) | netbuf[5];
}
memcpy(buffer, netbuf+12, returnLen);
return returnLen;
}

void network_cmd_no_data_send(int handle, uint16 *netMsgId, customData_t *customData, net_msg_type_t cmd, uint8 *deskey, comm_type_t commType)
{
uint8 buffer[CWS_NETMSGSIZE];

buffer[0] = cmd; buffer[1] = 0;
network_message_send(handle, netMsgId, customData, buffer, 3, deskey, commType);
}

int network_cmd_no_data_receive(int handle, uint16 *netMsgId, customData_t *customData, uint8 *deskey, comm_type_t commType)
{
uint8 buffer[CWS_NETMSGSIZE];

if (network_message_receive(handle, netMsgId, customData, buffer, deskey, commType) != 3) return -1;
return buffer[0];
}

int network_tcp_incoming_port_open(uint16 port)
{
struct sockaddr_in socketAddr;
int socketOptActive = 1;
int handle;

if (!port) return -1;

if ((handle = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
fprintf(stderr, "network port %u open: ", port);
perror("socket");
return -1;
}

if (setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &socketOptActive, sizeof(int)) < 0)
{
fprintf(stderr, "network port %u open: error setsockopt\n", port);
close(handle);
return -1;
}

socketAddr.sin_family = AF_INET;
socketAddr.sin_port = htons(port);
socketAddr.sin_addr.s_addr = htonl(INADDR_ANY);

if (bind(handle, (struct sockaddr *) &socketAddr, sizeof (socketAddr)) < 0)
{
fprintf(stderr, "network port %u open: ", port);
perror("bind");
close(handle);
return -1;
}

if (listen(handle, 5) < 0)
{
fprintf(stderr, "network port %u open: ", port);
perror("listen");
close(handle);
return -1;
}
return handle;
}

int network_tcp_connection_accept(int socketHandle)
{
int connHandle;
struct sockaddr_in peerAddr;
struct sockaddr_in myAddr;
socklen_t peerAddrLen;
socklen_t myAddrLen;
uint16 peerPort, myPort;
uint32 peerIp, myIp;

if (socketHandle < 0) return -1;
peerAddrLen = sizeof(peerAddr);
myAddrLen = sizeof(myAddr);
if ((connHandle = accept(socketHandle, (struct sockaddr *) &peerAddr, &peerAddrLen)) < 0) { fprintf(stderr, "error network accept connection\n"); return -1; }
peerPort = ntohs(peerAddr.sin_port);
peerIp = ntohl(peerAddr.sin_addr.s_addr);
myPort = ntohs(myAddr.sin_port);
myIp = ntohl(myAddr.sin_addr.s_addr);

/* optional: do checks on or log IP of incoming connections */

return connHandle;
}

int network_tcp_connection_open(uint8 *hostname, uint16 port)
{
int handle;
struct hostent *hostaddr;
struct sockaddr_in socketAddr;

if (!(hostaddr = gethostbyname(hostname))) { fprintf(stderr, "Host lookup of %s failed\n", hostname); return -1; }
if ((handle = socket(PF_INET, SOCK_STREAM, 0)) < 0) { fprintf(stderr, "network make connection: couldn't create socket\n"); return -1; }
socketAddr.sin_family = AF_INET;
socketAddr.sin_port = htons(port);
socketAddr.sin_addr.s_addr = ((struct in_addr *)hostaddr->h_addr)->s_addr;
if (connect(handle, (struct sockaddr *)&socketAddr, sizeof(socketAddr)) < 0) { fprintf(stderr, "network make connection: error connect\n"); close(handle); return -1; }
return handle;
}
QuestionWaiting for Event to fire Pin
Bralyan17-Jan-06 12:29
Bralyan17-Jan-06 12:29 
Questioncalculating fraction in c# Pin
two_man_only17-Jan-06 12:02
two_man_only17-Jan-06 12:02 
AnswerRe: calculating fraction in c# Pin
Christian Graus17-Jan-06 14:03
protectorChristian Graus17-Jan-06 14:03 
GeneralRe: calculating fraction in c# Pin
two_man_only24-Jan-06 12:02
two_man_only24-Jan-06 12:02 
GeneralRe: calculating fraction in c# Pin
Christian Graus24-Jan-06 12:04
protectorChristian Graus24-Jan-06 12:04 
GeneralRe: calculating fraction in c# Pin
two_man_only25-Jan-06 1:11
two_man_only25-Jan-06 1:11 
QuestionGracefully shutting down threads Pin
User 665817-Jan-06 11:44
User 665817-Jan-06 11:44 
AnswerRe: Gracefully shutting down threads Pin
CWIZO17-Jan-06 21:15
CWIZO17-Jan-06 21:15 
GeneralRe: Gracefully shutting down threads Pin
User 665818-Jan-06 2:12
User 665818-Jan-06 2:12 
GeneralRe: Gracefully shutting down threads Pin
CWIZO18-Jan-06 3:01
CWIZO18-Jan-06 3:01 
QuestionVS 2005 UK edition Pin
pieterman17-Jan-06 11:44
pieterman17-Jan-06 11:44 
AnswerRe: VS 2005 UK edition Pin
Christian Graus17-Jan-06 14:03
protectorChristian Graus17-Jan-06 14:03 
GeneralRe: VS 2005 UK edition Pin
pieterman18-Jan-06 7:03
pieterman18-Jan-06 7:03 
QuestionTry/Catch Variable Scoping Pin
RobertF5717-Jan-06 11:34
RobertF5717-Jan-06 11:34 
AnswerRe: Try/Catch Variable Scoping Pin
Guffa17-Jan-06 11:50
Guffa17-Jan-06 11:50 
GeneralRe: Try/Catch Variable Scoping Pin
RobertF5717-Jan-06 11:55
RobertF5717-Jan-06 11:55 
AnswerRe: Try/Catch Variable Scoping Pin
Guffa17-Jan-06 12:14
Guffa17-Jan-06 12:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.