[Release][C++]COEncryption.dll

03/02/2010 16:35 Nullable#1
This is a dynamic library that contains the encryption for 5017(if someone is up for a Cpp project), Diffie Hellman's KE and Blowfish(thanks to [Only registered and activated users can see links. Click Here To Register...] for those :)) might as well upgrade it later to include all the encryption algorithms existing so far, headers and static library also included
USAGE
1- Add the dll in the debug/release folder(both is preferred)
2- Add the header to your include path
3- Add the library to your libraries
4- to use the classes in the header:
PHP Code:
#include <blowfish.h> // obviously for blowfish :}
#include <dhke.h> // For the Diffie Hellman KE's class
#include <coencd.h> // For the 5017 encryption
using namespace ConquerEncryptionClasses
5- if you are using MSVC++ 2008 or higher use this statement to link the library
PHP Code:
#pragma comment(lib, "COEncryption.lib") 
else, you're on your own, you should know how to do it anyway..
6- Create instances..
7- ???
8- PROFIT :)

#Added Rev 3, proved working 100% after fixing some bugs in code.
PS. Make sure you already have OpenSSL's libraries and headers before using Blowfish or DH KE's classes.
03/02/2010 16:37 Decker_#2
nice
03/02/2010 16:49 tanelipe#3
Nice release hopefully it'll spark some interest in developing programs in programming language that isn't C# :). I'm sure there'll be a lot of more interest in this when/if the new cryptography is added, I haven't seen C++ implementation of it yet.
03/02/2010 16:54 LegalConquer#4
i have XD but just a converted version
03/02/2010 16:57 Nullable#5
Quote:
Originally Posted by tanelipe View Post
Nice release hopefully it'll spark some interest in developing programs in programming language that isn't C# :). I'm sure there'll be a lot of more interest in this when/if the new cryptography is added, I haven't seen C++ implementation of it yet.
Thanks, might take some time to include the latest encryption because I'm currently working with some fellows on a project, also school takes most of my time, hopefully i will finish it by this weekend or the next weekend :)
03/02/2010 18:21 tanelipe#6
I've basically written the Blowfish.h/cpp now, all I need to do now is to wrap the DH exchange part in C++ and it should be good to go. I haven't tested this code so far, I have no idea if it really works, lmao.

Blowfish.h
PHP Code:
#pragma once

#include <openssl/blowfish.h>
#pragma comment(lib, "libeay32.lib")

class CBlowfish
{
private:
    
unsigned char *DecryptIV;
    
unsigned char *EncryptIV;

    
int DecryptCounter;
    
int EncryptCounter;

    
BF_KEY *Key;
public:
    
CBlowfish(unsigned char *Dataint Length);
    ~
CBlowfish(void);

    
unsigned charEncrypt(unsigned char *Dataint Length);
    
unsigned charDecrypt(unsigned char *Dataint Length);

    
void SetKey(unsigned char *Dataint Length);
    
void SetIVs(unsigned char *DecryptIVunsigned char *EncryptIV);
}; 

Blowfish.cpp
PHP Code:
#include "StdAfx.h"
#include "Blowfish.h"

CBlowfish::CBlowfish(unsigned char *Dataint Length)
{
    
DecryptIV = new unsigned char[8];
    
EncryptIV = new unsigned char[8];
    
DecryptCounter 0;
    
EncryptCounter 0;

    
Key = new BF_KEY();
    
BF_set_key(KeyLengthData);
}

CBlowfish::~CBlowfish(void)
{
    
delete[] DecryptIV;
    
delete[] EncryptIV;
    
delete[] Key;
}

unsigned charCBlowfish::Encrypt(unsigned char *Dataint Length)
{
    
unsigned charOut = new unsigned char[Length];
    
BF_cfb64_encrypt(DataOutLengthKeyEncryptIV, &EncryptCounter1);
    return 
Out;
}
unsigned charCBlowfish::Decrypt(unsigned char *Dataint Length)
{
    
unsigned charOut = new unsigned char[Length];
    
BF_cfb64_encrypt(DataOutLengthKeyDecryptIV, &DecryptCounter0);
    return 
Out;
}
void CBlowfish::SetKey(unsigned char *Dataint Length)
{
    
BF_set_key(KeyLengthData);
    
DecryptCounter 0;
    
EncryptCounter 0;
}
void CBlowfish::SetIVs(unsigned char *DecryptIVunsigned char *EncryptIV)
{
    
memcpy(this->DecryptIVDecryptIV8);
    
memcpy(this->EncryptIVEncryptIV8);

03/02/2010 18:31 Nullable#7
Quote:
Originally Posted by tanelipe View Post
I've basically written the Blowfish.h/cpp now, all I need to do now is to wrap the DH exchange part in C++ and it should be good to go. I haven't tested this code so far, I have no idea if it really works, lmao....
Nice :) it should work nonetheless.
03/02/2010 19:01 Øblivion#8
Sick release ;)
03/02/2010 20:42 tanelipe#9
If anyone is kind enough to convert the ClientKeyPacket (Handshake reply) and ServerKeyPacket (Handshake) to C++ (structures atleast), it would be greatly appreciated. I'm not really into doing it, it's pain in the arse in C# already.

EDIT:

DH.h
PHP Code:
#pragma once

#include <openssl/dh.h>
#include <openssl/bn.h>
class CDH
{
private:
    
DH *dh;
public:
    
CDH(BIGNUM *PBIGNUM *G);
    ~
CDH(void);

    
unsigned charComputeKey(BIGNUM *PublicKey);
    
void GenerateKeys();


    
void SetG(BIGNUM *G);
    
void SetP(BIGNUM *P);
    
void SetPrivate(BIGNUM *PrivateKey);
    
void SetPublic(BIGNUM *PublicKey);

    
BIGNUMGetG();
    
BIGNUMGetP();
    
BIGNUMGetPrivate();
    
BIGNUMGetPublic();

    
__declspec(property(get GetGput SetG)) BIGNUM *G;
    
__declspec(property(get GetPput SetP)) BIGNUM *P;

    
__declspec(property(get GetPrivateput SetPrivate)) BIGNUM *PrivateKey;
    
__declspec(property(get GetPublicput SetPublic)) BIGNUM *PublicKey;
}; 
DH.cpp

PHP Code:
#include "StdAfx.h"
#include "DH.h"

CDH::CDH(BIGNUM *PBIGNUM *G)
{
    
dh = new DH();
    
dh->P;
    
dh->G;
}

unsigned charCDH::ComputeKey(BIGNUM *PublicKey)
{
    
unsigned char *Key = new unsigned char[DH_size(dh)];
    
DH_compute_key(KeyPublicKeydh);
    return 
Key;
}
void CDH::GenerateKeys() 
{
    
DH_generate_key(dh);
}

void CDH::SetG(BIGNUM *G)
{
    
dh->BN_dup(G);
}

void CDH::SetP(BIGNUM *P)
{
    
dh->BN_dup(P);
}
void CDH::SetPrivate(BIGNUMPrivateKey)
{
    
dh->priv_key BN_dup(PrivateKey);
}
void CDH::SetPublic(BIGNUMPublicKey)
{
    
dh->pub_key BN_dup(PublicKey);
}
BIGNUMCDH::GetP()
{
    return 
dh->p;
}
BIGNUMCDH::GetG()
{
    return 
dh->g;
}
BIGNUMCDH::GetPrivate()
{
    return 
dh->priv_key;
}
BIGNUMCDH::GetPublic()
{
    return 
dh->pub_key;
}

CDH::~CDH(void)
{
    
delete[] dh;

03/02/2010 23:12 Arcо#10
Haha tempts me to try cpp lol.
03/02/2010 23:41 tanelipe#11
Noticed sligth error in the code, if anyone is using it replace

PHP Code:
CDH::CDH(BIGNUM *PBIGNUM *G)
{
    
dh = new DH();
    
dh->P;
    
dh->G;

with

PHP Code:
dh DH_new();
    
dh->BN_dup(P);
    
dh->BN_dup(G); 
03/03/2010 16:07 Nullable#12
Diffie Hellman and Blowfish included, thanks to tanelipe for those. :)
03/03/2010 18:06 CptSky#13
I have made the class for the ServerKeyPacket. I don't code a lot in C++ and I make it on Xcode. I'm not sure of the exact structure of the packet. I don't really know the new encryption. I prefer the old Co2 :)

PHP Code:
//////////////////////////////////////////////////////////////////////
// ServerKeyPacket.h
//////////////////////////////////////////////////////////////////////

#pragma once

// include head file(s) here...

// define constant(s) here...
const int _PAD_LENGTH 11;
const 
int _JUNK_LENGTH 10;
const 
int _IV_LENGTH 8;
const 
int _KEY_LENGTH 128;
const 
char P[128] = "A320A85EDD79171C341459E94807D71D39BB3B3F3B5161CA84894F3AC3FC7FEC317A2DDEC83B66D30C29261C6492643061AECFCF4A051816D7C359A6A7B7D8FB";
const 
char G[2] = "05";

class 
CServerKeyPacket
    
{
    public:
        
CServerKeyPacket();
        
virtual ~CServerKeyPacket();

        
bool Create    (char Key[_KEY_LENGTH], unsigned char ServerIV[_IV_LENGTH], unsigned char ClientIV[_IV_LENGTH]);        
        
    private:
        
typedef struct
            
{
                
unsigned char    dwPad[_PAD_LENGTH];
                
int                MsgSize;
                
unsigned char    dwJunk[_JUNK_LENGTH];
                
unsigned char    dwServerIV[_IV_LENGTH];
                
unsigned char    dwClientIV[_IV_LENGTH];
                
char            dwP[128];
                
char            dwG[2];
                
char            dwKey[_KEY_LENGTH];
                };
            }
MSG_Info;
        
        
MSG_Info*    m_pInfo;
    }; 
PHP Code:
//////////////////////////////////////////////////////////////////////
// ServerKeyPacket.cpp
//////////////////////////////////////////////////////////////////////

// include head file(s) here...
#include "ServerKeyPacket.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CServerKeyPacket::CServerKeyPacket()
{

}

CServerKeyPacket::~CServerKeyPacket()
{
    
}

//////////////////////////////////////////////////////////////////////
bool CServerKeyPacket::Create(char Key[_KEY_LENGTH], unsigned char ServerIV[_IV_LENGTH], unsigned char ClientIV[_IV_LENGTH])
{
    
unsigned char Pad[_PAD_LENGTH];
    
unsigned char Junk[_JUNK_LENGTH];
    
    for (
int i 0_PAD_LENGTHi++)
        
Pad[i] = (rand() / (RAND_MAX 1) * 255);
    
    for (
int i 0_JUNK_LENGTHi++)
        
Junk[i] = (rand() / (RAND_MAX 1) * 255);
    
    
// fill info now
    
SafeCopy(m_pInfo->dwPadPad_PAD_LENGTH);
    
SafeCopy(m_pInfo->dwJunkJunk_JUNK_LENGTH);
    
SafeCopy(m_pInfo->dwServerIVServerIV_IV_LENGTH);
    
SafeCopy(m_pInfo->dwClientIVClientIV_IV_LENGTH);
    
SafeCopy(m_pInfo->dwPP128);
    
SafeCopy(m_pInfo->dwGG2);
    
SafeCopy(m_pInfo->dwKeyKey_KEY_LENGTH);

    
MsgSize sizeof(MSG_Info);
    
    return 
true;

03/03/2010 18:24 tanelipe#14
I've already got the parsing done for handshake/-reply

PHP Code:
#pragma once

#include "PacketReader.h"

class Handshake
{
public:
    
Handshake(void) { }
    ~
Handshake(void) { }

    
unsigned char *ServerIV, *ClientIV;
    
char *P, *G, *PublicKey;
    
void Process(unsigned char *Packet
    {
        
PacketReader *Reader = new PacketReader(Packetsizeof(Packet), 15);
        
Reader->Advance(Reader->ReadDWord());
        
ServerIV Reader->ReadBytes(Reader->ReadDWord());
        
ClientIV Reader->ReadBytes(Reader->ReadDWord());
        
Reader->ReadString(Reader->ReadDWord());
        
Reader->ReadString(Reader->ReadDWord());
        
PublicKey Reader->ReadString(Reader->ReadDWord());
    }
}; 
PHP Code:
#pragma once

#include "PacketReader.h"

class HandshakeReply
{
public:
    
HandshakeReply(void) { }
    ~
HandshakeReply(void) { }

    
char *PublicKey;
    
void Process(unsigned char *Packet
    {
        
PacketReader *Reader = new PacketReader(Packetsizeof(Packet), 11);
        
Reader->Advance(Reader->ReadDWord());
        
PublicKey Reader->ReadString(Reader->ReadDWord());
    }
}; 
PacketReader is just a simple class which reads/advances the current position of index, looks neater.
03/03/2010 21:36 Nullable#15
You guys are making me lazy.. lol you did almost all the work, where was that when i was searching for it? :P
Anyhow, I'll be writing the reply in a while