⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 desserver.h

📁 整理3des算法,采用C++类实现,便于移植
💻 H
字号:
// DESserver.h: interface for the DESserver class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_DESserver_H__A18F0A54_FC9A_4610_9A8F_1E96764C089F__INCLUDED_)
#define AFX_DESserver_H__A18F0A54_FC9A_4610_9A8F_1E96764C089F__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

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

	static DESserver* m_pInstance;

public:
	static DESserver* GetInstance()
	{

		if(m_pInstance == NULL)
			m_pInstance = new DESserver();

		return m_pInstance;
	}

	static void ReleaseInstance()
	{
		if(m_pInstance)
		{
			delete m_pInstance;
			m_pInstance = NULL;
		}
	}
	#define D2_DES		/* include double-length support */
#define D3_DES		/* include triple-length support */

#ifdef D3_DES
#ifndef D2_DES
#define D2_DES		/* D2_DES is needed for D3_DES */
#endif
#endif
	
#define EN0	0	/* MODE == encrypt */
#define DE1	1	/* MODE == decrypt */

/* A useful alias on 68000-ish machines, but NOT USED HERE. */

typedef union {
	unsigned long blok[2];
	unsigned short word[4];
	unsigned char byte[8];
	} M68K;

	void deskey(unsigned char * hexkey, short MODE);
/*		      hexkey[8]     MODE
 * Sets the internal key register according to the hexadecimal
 * key contained in the 8 bytes of hexkey, according to the DES,
 * for encryption or decryption according to MODE.
 */

	void usekey(unsigned long * cookedkey);
/*		    cookedkey[32]
 * Loads the internal key register with the data in cookedkey.
 */

	void cpkey(unsigned long * cookedkey);
/*		   cookedkey[32]
 * Copies the contents of the internal key register into the storage
 * located at &cookedkey[0].
 */

	void des(unsigned char * from, unsigned char * to);
/*		    from[8]	      to[8]
 * Encrypts/Decrypts (according to the key currently loaded in the
 * internal key register) one block of eight bytes at address 'from'
 * into the block at address 'to'.  They can be the same.
 */

#ifdef D2_DES

#define desDkey(a,b)	des2key((a),(b))
	void des2key(unsigned char * hexkey, short MODE);
/*		      hexkey[16]     MODE
 * Sets the internal key registerS according to the hexadecimal
 * keyS contained in the 16 bytes of hexkey, according to the DES,
 * for DOUBLE encryption or decryption according to MODE.
 * NOTE: this clobbers all three key registers!
 */

	void Ddes(unsigned char *from, unsigned char *to);
/*		    from[8]	      to[8]
 * Encrypts/Decrypts (according to the keyS currently loaded in the
 * internal key registerS) one block of eight bytes at address 'from'
 * into the block at address 'to'.  They can be the same.
 */

	void D2des(unsigned char *from, unsigned char *to);
/*		    from[16]	      to[16]
 * Encrypts/Decrypts (according to the keyS currently loaded in the
 * internal key registerS) one block of SIXTEEN bytes at address 'from'
 * into the block at address 'to'.  They can be the same.
 */

	void makekey(char *password, unsigned char *key);
/*		*password,	single-length key[8]
 * With a double-length default key, this routine hashes a NULL-terminated
 * string into an eight-byte random-looking key, suitable for use with the
 * deskey() routine.
 */

#define makeDkey(a,b)	make2key((a),(b))
	void make2key(char *password, unsigned char *key);
/*		*password,	double-length key[16]
 * With a double-length default key, this routine hashes a NULL-terminated
 * string into a sixteen-byte random-looking key, suitable for use with the
 * des2key() routine.
 */

#ifndef D3_DES	/* D2_DES only */

#define useDkey(a)	use2key((a))
#define cpDkey(a)	cp2key((a))

	void use2key(unsigned long *cookedkey);
/*		    cookedkey[64]
 * Loads the internal key registerS with the data in cookedkey.
 * NOTE: this clobbers all three key registers!
 */

	void cp2key(unsigned long *cookedkey);
/*		   cookedkey[64]
 * Copies the contents of the internal key registerS into the storage
 * located at &cookedkey[0].
 */

#else	/* D3_DES too */

#define useDkey(a)	use3key((a))
#define cpDkey(a)	cp3key((a))


	void des3key(unsigned char *hexkey, short MODE);
/*		      hexkey[24]     MODE
 * Sets the internal key registerS according to the hexadecimal
 * keyS contained in the 24 bytes of hexkey, according to the DES,
 * for DOUBLE encryption or decryption according to MODE.
 */

	void use3key(unsigned long *cookedkey);
/*		    cookedkey[96]
 * Loads the 3 internal key registerS with the data in cookedkey.
 */

	void cp3key(unsigned long *cookedkey);
/*		   cookedkey[96]
 * Copies the contents of the 3 internal key registerS into the storage
 * located at &cookedkey[0].
 */
 
	void make3key(char *password, unsigned char *key);
/*		*password,	triple-length key[24]
 * With a triple-length default key, this routine hashes a NULL-terminated
 * string into a twenty-four-byte random-looking key, suitable for use with
 * the des3key() routine.
 */
	void D3des(unsigned char *from, unsigned char *into);
	void cookey(unsigned long *raw1);
	void scrunch(unsigned char *outof,unsigned long *into);
	void unscrun(unsigned long *outof,unsigned char *into);
	void desfunc(unsigned long *block, unsigned long *keys);
#endif	/* D3_DES */
#endif	/* D2_DES */

	void Lock()
	{
		EnterCriticalSection(&m_cs);
	}

	void Unlock()
	{
		LeaveCriticalSection(&m_cs);
	}

	CRITICAL_SECTION m_cs;

};

#endif // !defined(AFX_DESserver_H__A18F0A54_FC9A_4610_9A8F_1E96764C089F__INCLUDED_)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -