开发学院软件开发VC 基于C++的DES加密算法实现代码实例 阅读

基于C++的DES加密算法实现代码实例

 2009-04-02 20:01:06 来源:WEB开发网   
核心提示: unsigned long DES::SP1[64] = { 0x01010400L, 0x00000000L, 0x00010000L, 0x01010404L, 0x01010004L, 0x00010404L, 0x00000004L, 0x00010000L, 0x00000400

unsigned long DES::SP1[64] = {
    0x01010400L, 0x00000000L, 0x00010000L, 0x01010404L,
    0x01010004L, 0x00010404L, 0x00000004L, 0x00010000L,
    0x00000400L, 0x01010400L, 0x01010404L, 0x00000400L,
    0x01000404L, 0x01010004L, 0x01000000L, 0x00000004L,
    0x00000404L, 0x01000400L, 0x01000400L, 0x00010400L,
    0x00010400L, 0x01010000L, 0x01010000L, 0x01000404L,
    0x00010004L, 0x01000004L, 0x01000004L, 0x00010004L,
    0x00000000L, 0x00000404L, 0x00010404L, 0x01000000L,
    0x00010000L, 0x01010404L, 0x00000004L, 0x01010000L,
    0x01010400L, 0x01000000L, 0x01000000L, 0x00000400L,
    0x01010004L, 0x00010000L, 0x00010400L, 0x01000004L,
    0x00000400L, 0x00000004L, 0x01000404L, 0x00010404L,
    0x01010404L, 0x00010004L, 0x01010000L, 0x01000404L,
    0x01000004L, 0x00000404L, 0x00010404L, 0x01010400L,
    0x00000404L, 0x01000400L, 0x01000400L, 0x00000000L,
    0x00010004L, 0x00010400L, 0x00000000L, 0x01010004L };

unsigned long DES::SP2[64] = {
    0x80108020L, 0x80008000L, 0x00008000L, 0x00108020L,
    0x00100000L, 0x00000020L, 0x80100020L, 0x80008020L,
    0x80000020L, 0x80108020L, 0x80108000L, 0x80000000L,
    0x80008000L, 0x00100000L, 0x00000020L, 0x80100020L,
    0x00108000L, 0x00100020L, 0x80008020L, 0x00000000L,
    0x80000000L, 0x00008000L, 0x00108020L, 0x80100000L,
    0x00100020L, 0x80000020L, 0x00000000L, 0x00108000L,
    0x00008020L, 0x80108000L, 0x80100000L, 0x00008020L,
    0x00000000L, 0x00108020L, 0x80100020L, 0x00100000L,
    0x80008020L, 0x80100000L, 0x80108000L, 0x00008000L,
    0x80100000L, 0x80008000L, 0x00000020L, 0x80108020L,
    0x00108020L, 0x00000020L, 0x00008000L, 0x80000000L,
    0x00008020L, 0x80108000L, 0x00100000L, 0x80000020L,
    0x00100020L, 0x80008020L, 0x80000020L, 0x00100020L,
    0x00108000L, 0x00000000L, 0x80008000L, 0x00008020L,
    0x80000000L, 0x80100020L, 0x80108020L, 0x00108000L };

unsigned long DES::SP3[64] = {
    0x00000208L, 0x08020200L, 0x00000000L, 0x08020008L,
    0x08000200L, 0x00000000L, 0x00020208L, 0x08000200L,
    0x00020008L, 0x08000008L, 0x08000008L, 0x00020000L,
    0x08020208L, 0x00020008L, 0x08020000L, 0x00000208L,
    0x08000000L, 0x00000008L, 0x08020200L, 0x00000200L,
    0x00020200L, 0x08020000L, 0x08020008L, 0x00020208L,
    0x08000208L, 0x00020200L, 0x00020000L, 0x08000208L,
    0x00000008L, 0x08020208L, 0x00000200L, 0x08000000L,
    0x08020200L, 0x08000000L, 0x00020008L, 0x00000208L,
    0x00020000L, 0x08020200L, 0x08000200L, 0x00000000L,
    0x00000200L, 0x00020008L, 0x08020208L, 0x08000200L,
    0x08000008L, 0x00000200L, 0x00000000L, 0x08020008L,
    0x08000208L, 0x00020000L, 0x08000000L, 0x08020208L,
    0x00000008L, 0x00020208L, 0x00020200L, 0x08000008L,
    0x08020000L, 0x08000208L, 0x00000208L, 0x08020000L,
    0x00020208L, 0x00000008L, 0x08020008L, 0x00020200L };

unsigned long DES::SP4[64] = {
    0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,
    0x00802080L, 0x00800081L, 0x00800001L, 0x00002001L,
    0x00000000L, 0x00802000L, 0x00802000L, 0x00802081L,
    0x00000081L, 0x00000000L, 0x00800080L, 0x00800001L,
    0x00000001L, 0x00002000L, 0x00800000L, 0x00802001L,
    0x00000080L, 0x00800000L, 0x00002001L, 0x00002080L,
    0x00800081L, 0x00000001L, 0x00002080L, 0x00800080L,
    0x00002000L, 0x00802080L, 0x00802081L, 0x00000081L,
    0x00800080L, 0x00800001L, 0x00802000L, 0x00802081L,
    0x00000081L, 0x00000000L, 0x00000000L, 0x00802000L,
    0x00002080L, 0x00800080L, 0x00800081L, 0x00000001L,
    0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,
    0x00802081L, 0x00000081L, 0x00000001L, 0x00002000L,
    0x00800001L, 0x00002001L, 0x00802080L, 0x00800081L,
    0x00002001L, 0x00002080L, 0x00800000L, 0x00802001L,
    0x00000080L, 0x00800000L, 0x00002000L, 0x00802080L };

unsigned long DES::SP5[64] = {
    0x00000100L, 0x02080100L, 0x02080000L, 0x42000100L,
    0x00080000L, 0x00000100L, 0x40000000L, 0x02080000L,
    0x40080100L, 0x00080000L, 0x02000100L, 0x40080100L,
    0x42000100L, 0x42080000L, 0x00080100L, 0x40000000L,
    0x02000000L, 0x40080000L, 0x40080000L, 0x00000000L,
    0x40000100L, 0x42080100L, 0x42080100L, 0x02000100L,
    0x42080000L, 0x40000100L, 0x00000000L, 0x42000000L,
    0x02080100L, 0x02000000L, 0x42000000L, 0x00080100L,
    0x00080000L, 0x42000100L, 0x00000100L, 0x02000000L,
    0x40000000L, 0x02080000L, 0x42000100L, 0x40080100L,
    0x02000100L, 0x40000000L, 0x42080000L, 0x02080100L,
    0x40080100L, 0x00000100L, 0x02000000L, 0x42080000L,
    0x42080100L, 0x00080100L, 0x42000000L, 0x42080100L,
    0x02080000L, 0x00000000L, 0x40080000L, 0x42000000L,
    0x00080100L, 0x02000100L, 0x40000100L, 0x00080000L,
    0x00000000L, 0x40080000L, 0x02080100L, 0x40000100L };

unsigned long DES::SP6[64] = {
    0x20000010L, 0x20400000L, 0x00004000L, 0x20404010L,
    0x20400000L, 0x00000010L, 0x20404010L, 0x00400000L,
    0x20004000L, 0x00404010L, 0x00400000L, 0x20000010L,
    0x00400010L, 0x20004000L, 0x20000000L, 0x00004010L,
    0x00000000L, 0x00400010L, 0x20004010L, 0x00004000L,
    0x00404000L, 0x20004010L, 0x00000010L, 0x20400010L,
    0x20400010L, 0x00000000L, 0x00404010L, 0x20404000L,
    0x00004010L, 0x00404000L, 0x20404000L, 0x20000000L,
    0x20004000L, 0x00000010L, 0x20400010L, 0x00404000L,
    0x20404010L, 0x00400000L, 0x00004010L, 0x20000010L,
    0x00400000L, 0x20004000L, 0x20000000L, 0x00004010L,
    0x20000010L, 0x20404010L, 0x00404000L, 0x20400000L,
    0x00404010L, 0x20404000L, 0x00000000L, 0x20400010L,
    0x00000010L, 0x00004000L, 0x20400000L, 0x00404010L,
    0x00004000L, 0x00400010L, 0x20004010L, 0x00000000L,
    0x20404000L, 0x20000000L, 0x00400010L, 0x20004010L };

unsigned long DES::SP7[64] = {
    0x00200000L, 0x04200002L, 0x04000802L, 0x00000000L,
    0x00000800L, 0x04000802L, 0x00200802L, 0x04200800L,
    0x04200802L, 0x00200000L, 0x00000000L, 0x04000002L,
    0x00000002L, 0x04000000L, 0x04200002L, 0x00000802L,
    0x04000800L, 0x00200802L, 0x00200002L, 0x04000800L,
    0x04000002L, 0x04200000L, 0x04200800L, 0x00200002L,
    0x04200000L, 0x00000800L, 0x00000802L, 0x04200802L,
    0x00200800L, 0x00000002L, 0x04000000L, 0x00200800L,
    0x04000000L, 0x00200800L, 0x00200000L, 0x04000802L,
    0x04000802L, 0x04200002L, 0x04200002L, 0x00000002L,
    0x00200002L, 0x04000000L, 0x04000800L, 0x00200000L,
    0x04200800L, 0x00000802L, 0x00200802L, 0x04200800L,
    0x00000802L, 0x04000002L, 0x04200802L, 0x04200000L,
    0x00200800L, 0x00000000L, 0x00000002L, 0x04200802L,
    0x00000000L, 0x00200802L, 0x04200000L, 0x00000800L,
    0x04000002L, 0x04000800L, 0x00000800L, 0x00200002L };

unsigned long DES::SP8[64] = {
    0x10001040L, 0x00001000L, 0x00040000L, 0x10041040L,
    0x10000000L, 0x10001040L, 0x00000040L, 0x10000000L,
    0x00040040L, 0x10040000L, 0x10041040L, 0x00041000L,
    0x10041000L, 0x00041040L, 0x00001000L, 0x00000040L,
    0x10040000L, 0x10000040L, 0x10001000L, 0x00001040L,
    0x00041000L, 0x00040040L, 0x10040040L, 0x10041000L,
    0x00001040L, 0x00000000L, 0x00000000L, 0x10040040L,
    0x10000040L, 0x10001000L, 0x00041040L, 0x00040000L,
    0x00041040L, 0x00040000L, 0x10041000L, 0x00001000L,
    0x00000040L, 0x10040040L, 0x00001000L, 0x00041040L,
    0x10001000L, 0x00000040L, 0x10000040L, 0x10040000L,
    0x10040040L, 0x10000000L, 0x00040000L, 0x10001040L,
    0x00000000L, 0x10041040L, 0x00040040L, 0x10000040L,
    0x10040000L, 0x10001000L, 0x10001040L, 0x00000000L,
    0x10041040L, 0x00041000L, 0x00041000L, 0x00001040L,
    0x00001040L, 0x00040040L, 0x10000000L, 0x10041000L };


接下来我们举例说明如何使用DES类。首先定义一个Encrypt类:


//Encrypt.h

#include "des.h"
#include "Base64.h"
class Encrypt{
public :
    char* encrypt ( unsigned char key[8], char* data);
    char* decrypt ( unsigned char key[8], char* data);
    CBase64 base;
};


而后是具体的实现:

//Encrypt.cpp

#include "Encrypt.h"
#include <stdio.h>

char* Encrypt::encrypt ( unsigned char key[8], char* data)
{
//将字符指针转化为字符数组
    char* datavv = data;
    data =(char *) calloc(100,1);
    memcpy(data,datavv,strlen(datavv));
    data[strlen(data)] = '';
//DES加密
    DES des ;
    des.encrypt(key,(unsigned char *)data,1);
//base64编码
    base.Encode(data);
    char* res =(char *) base.EncodedMessage();
    return res;
};
char* Encrypt::decrypt ( unsigned char key[8], char* data)
{
//base64解码
    base.Decode(data);
    char* datares=(char *) base.DecodedMessage();
//将字符指针转化为字符数组
    char* datarea = datares;
    datares = (char *)malloc(256);
    memcpy(datares,datarea,strlen(datarea));
    datares[strlen(datarea)] = '';
//DES解密
    DES des ;
    des.decrypt(key,(unsigned char*) datares,1);
    return datares;
};

注意,这里使用了BASE 64编码。其实现代码如下:

// Base64.h
#include <windows.h>

class CBase64
{
    // Internal bucket class.
    class TempBucket
    {
    public:
        BYTE         nData[4];
        BYTE         nSize;
        void         Clear() { ::ZeroMemory(nData, 4); nSize = 0; };
    };

    PBYTE                       m_pDBuffer;
    PBYTE                       m_pEBuffer;
    DWORD                       m_nDBufLen;
    DWORD                       m_nEBufLen;
    DWORD                       m_nDDataLen;
    DWORD                       m_nEDataLen;

public:
    CBase64();
    virtual ~CBase64();

public:
    virtual void         Encode(const PBYTE, DWORD);
    virtual void         Decode(const PBYTE, DWORD);
    virtual void         Encode(LPCSTR sMessage);
    virtual void         Decode(LPCSTR sMessage);

    virtual LPCSTR     DecodedMessage() const;
    virtual LPCSTR     EncodedMessage() const;

    virtual void         AllocEncode(DWORD);
    virtual void         AllocDecode(DWORD);
    virtual void         SetEncodeBuffer(const PBYTE pBuffer, DWORD nBufLen);
    virtual void         SetDecodeBuffer(const PBYTE pBuffer, DWORD nBufLen);

protected:
    virtual void         _EncodeToBuffer(const TempBucket &Decode, PBYTE pBuffer);
    virtual ULONG         _DecodeToBuffer(const TempBucket &Decode, PBYTE pBuffer);
    virtual void         _EncodeRaw(TempBucket &, const TempBucket &);
    virtual void         _DecodeRaw(TempBucket &, const TempBucket &);
    virtual BOOL         _IsBadMimeChar(BYTE);

    static char         m_DecodeTable[256];
    static BOOL         m_Init;
    void                       _Init();
};
////////////////////////////////////

//CBase64.cpp
// CBase64.cpp: implementation of the CBase64 class.
//
//////////////////////////////////////////////////////////////////////

#include "Base64.h"

// Digits...
static char Base64Digits[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

BOOL CBase64::m_Init   = FALSE;
char CBase64::m_DecodeTable[256];

#ifndef PAGESIZE
#define PAGESIZE       4096
#endif

#ifndef ROUNDTOPAGE
#define ROUNDTOPAGE(a)     (((a/4096)+1)*4096)
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CBase64::CBase64()
: m_pDBuffer(NULL),
m_pEBuffer(NULL),
m_nDBufLen(0),
m_nEBufLen(0)
{

}

CBase64::~CBase64()
{
if(m_pDBuffer != NULL)
  delete [] m_pDBuffer;

if(m_pEBuffer != NULL)
  delete [] m_pEBuffer;
}

LPCSTR CBase64::DecodedMessage() const
{
return (LPCSTR) m_pDBuffer;
}

LPCSTR CBase64::EncodedMessage() const
{
return (LPCSTR) m_pEBuffer;
}

void CBase64::AllocEncode(DWORD nSize)
{
if(m_nEBufLen < nSize)
{
  if(m_pEBuffer != NULL)
    delete [] m_pEBuffer;

  m_nEBufLen = ROUNDTOPAGE(nSize);
  m_pEBuffer = new BYTE[m_nEBufLen];
}

  ::ZeroMemory(m_pEBuffer, m_nEBufLen);
m_nEDataLen = 0;
}

void CBase64::AllocDecode(DWORD nSize)
{
if(m_nDBufLen < nSize)
{
  if(m_pDBuffer != NULL)
    delete [] m_pDBuffer;

  m_nDBufLen = ROUNDTOPAGE(nSize);
  m_pDBuffer = new BYTE[m_nDBufLen];
}

::ZeroMemory(m_pDBuffer, m_nDBufLen);
m_nDDataLen = 0;
}

void CBase64::SetEncodeBuffer(const PBYTE pBuffer, DWORD nBufLen)
{
DWORD ii = 0;

AllocEncode(nBufLen);
while(ii < nBufLen)
{
  if(!_IsBadMimeChar(pBuffer[ii]))
  {
    m_pEBuffer[m_nEDataLen] = pBuffer[ii];
    m_nEDataLen++;
  }

  ii++;
}
}

void CBase64::SetDecodeBuffer(const PBYTE pBuffer, DWORD nBufLen)
{
AllocDecode(nBufLen);
::CopyMemory(m_pDBuffer, pBuffer, nBufLen);
m_nDDataLen = nBufLen;
}

void CBase64::Encode(const PBYTE pBuffer, DWORD nBufLen)
{
SetDecodeBuffer(pBuffer, nBufLen);
AllocEncode(nBufLen * 2);

TempBucket     Raw;
DWORD       nIndex = 0;

while((nIndex + 3) <= nBufLen)
{
  Raw.Clear();
  ::CopyMemory(&Raw, m_pDBuffer + nIndex, 3);
  Raw.nSize = 3;
  _EncodeToBuffer(Raw, m_pEBuffer + m_nEDataLen);
  nIndex   += 3;
  m_nEDataLen += 4;
}

if(nBufLen > nIndex)
{
  Raw.Clear();
  Raw.nSize = (BYTE) (nBufLen - nIndex);
  ::CopyMemory(&Raw, m_pDBuffer + nIndex, nBufLen - nIndex);
  _EncodeToBuffer(Raw, m_pEBuffer + m_nEDataLen);
  m_nEDataLen += 4;
}
}

void CBase64::Encode(LPCSTR szMessage)
{
if(szMessage != NULL)
  CBase64::Encode((const PBYTE)szMessage, lstrlenA(szMessage));
}

void CBase64::Decode(const PBYTE pBuffer, DWORD dwBufLen)
{
if(!CBase64::m_Init)
  _Init();

SetEncodeBuffer(pBuffer, dwBufLen);

AllocDecode(dwBufLen);

TempBucket     Raw;

DWORD   nIndex = 0;

while((nIndex + 4) <= m_nEDataLen)
{
  Raw.Clear();
  Raw.nData[0] = CBase64::m_DecodeTable[m_pEBuffer[nIndex]];
  Raw.nData[1] = CBase64::m_DecodeTable[m_pEBuffer[nIndex + 1]];
  Raw.nData[2] = CBase64::m_DecodeTable[m_pEBuffer[nIndex + 2]];
  Raw.nData[3] = CBase64::m_DecodeTable[m_pEBuffer[nIndex + 3]];

  if(Raw.nData[2] == 255)
    Raw.nData[2] = 0;
  if(Raw.nData[3] == 255)
    Raw.nData[3] = 0;

  Raw.nSize = 4;
  _DecodeToBuffer(Raw, m_pDBuffer + m_nDDataLen);
  nIndex += 4;
  m_nDDataLen += 3;
}

// If nIndex < m_nEDataLen, then we got a decode message without padding.
// We may want to throw some kind of warning here, but we are still required
// to handle the decoding as if it was properly padded.
if(nIndex < m_nEDataLen)
{
  Raw.Clear();
  for(DWORD ii = nIndex; ii < m_nEDataLen; ii++)
  {
    Raw.nData[ii - nIndex] = CBase64::m_DecodeTable[m_pEBuffer[ii]];
    Raw.nSize++;
    if(Raw.nData[ii - nIndex] == 255)
    Raw.nData[ii - nIndex] = 0;
  }

  _DecodeToBuffer(Raw, m_pDBuffer + m_nDDataLen);
  m_nDDataLen += (m_nEDataLen - nIndex);
}
}

Tags:基于 DES 加密

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接