📄 difcrack.h
字号:
// DifCrack.h: interface for the CDifCrack class.
//
//////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
//File :DifCrack.h
//Author :郭瑞杰
//Description:DES差分攻击
//Data :2004.4.10
//Mail :guonanm@mails.gscas.ac.cn
//你可以任意修改复制传播此源码,但不得用于商业用途。如果你修改了此源码,
//请给我发一份。谢谢!
///////////////////////////////////////////////////////////////////////
#if !defined(AFX_DIFCRACK_H__9800F601_32A3_4E2C_9D70_754637D3985B__INCLUDED_)
#define AFX_DIFCRACK_H__9800F601_32A3_4E2C_9D70_754637D3985B__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "des.h"
#include <Afxtempl.h>
static const char PInvertTable[][4] =
{
{ 9,17,23,31},
{13,28, 2,18},
{24,10,30, 6},
{26,20,16, 1},
{ 8,14,25, 3},
{ 4,29,11,19},
{32,12,22, 7},
{ 5,27,15,21}
/*{16, 7,20,21},
{29,12,28,17},
{ 1,15,23,26},
{ 5,18,31,10},
{ 2, 8,24,14},
{32,27, 3, 9},
{19,13,30, 6},
{22,11, 4,25}*/
};
static const char PC2InvertTable[][8] =
{
{ 5,24, 7,16, 6,10,20,18},
{-1,12, 3,15,23, 1, 9,19},
{ 2,-1,14,22,11,-1,13, 4},
{-1,17,21, 8,47,31,27,48},
{35,41,-1,46,28,-1,39,32},
{25,44,-1,37,34,43,29,36},
{38,45,33,26,42,-1,30,40}
};
static const char PC1InvertTable[][8] =
{
{ 8,16,24,56,52,44,36, 1},
{ 7,15,23,55,51,43,35, 1},
{ 6,14,22,54,50,42,34, 1},
{ 5,13,21,53,49,41,33, 1},
{ 4,12,20,28,48,40,32, 1},
{ 3,11,19,27,47,39,31, 1},
{ 2,10,18,26,46,38,30, 1},
{ 1, 9,17,25,45,37,29, 1}
};
typedef struct _tagCrackData
{
bool plaintext1[64];
bool plaintext2[64];
bool cryptograph1[64];
bool cryptograph2[64];
}CRACKDATA;
class CDifCrack
{
public:
CDifCrack();
virtual ~CDifCrack();
//获取第iBox个盒的所有 INj 集合
void GetIN(bool *&pResult,int &nLen,const bool B[6],const bool C[4],const int iBox);
//获取第iBox个盒的所有 TEST 集合
void GetTEST(bool *&pResult,int &nLen,const bool E1[6],
const bool E2[6],const bool C[4],const int iBox);
//加入一组破解数据,明文p1,p2的后32位必须相等
void AddData(char* p1,char* p2,char* c1,char* c2,int nLen);
//进行破解
bool CrackIt();
//获取破解密钥
void GetKey(bool bKey[48],int &nLen);
bool GetKey(char bKey[8],int &nKeyLen,int nRound);
//P^-1变换
void PInvert(bool bDest[32],bool bSrc[32]);
//随机产生用于破解的明密文
void GenerateCrackData(DWORD nPair,char *pKey,int nKeyLen);
//从文件中读取破解数据
bool ReadCrackData(DWORD nPair = 0);
void ClearCrackData();
//设置是否将攻击过程记录到文件(此文件是difCrack.txt在当前工作目录
void LogIt(BOOL bLog = TRUE);
BOOL IsLog(){return m_bLogIt;}
//将数据写到文件
void DumpArray(const bool array[],int nLength,int nLine,int nIndent);
void DumpArray(const int array[],int nLength,int nLine,int nIndent);
void DumpString(CString str,int nIndent);
void DumpString(CString str,int n,int nIndent);
void ByteToBit(bool *Out, const char *In, int bits);
protected:
//得到所有的nBit位的组合
void GetAll(bool* &bResult,int &nLen,int nBit = 6);
bool IsEqual(const bool* p1,const bool* p2,int len);
void Xor(bool* pDest,const bool* pSrc,int len);
//s盒操作
void S(bool pDest[4],bool pSrc[6],int iBox);
void BitToByte(char *Out, const bool *In, int bits);
//扩展
void Expansion(bool bDest[48],const bool bSrc[32]);
//IP置换
void IP(bool bDest[64],const bool bSrc[64]);
//IP逆置换
void IPInvert(bool bDest[64],const bool bSrc[64]);
//循环右移
void RotateR(bool *In, int len, int loop);
//PC2置换
void PC2(bool bDest[48],const bool bSrc[56]);
//PC2的逆置换
void PC2Invert(bool bDest[56],const bool bSrc[48]);
//PC1的逆置换
void PC1Invert(bool bDest[64],const bool bSrc[56]);
//通过穷举法获取56Bit密钥中的另外8Bit
bool GetNext8BitKey(bool bKey[56],bool bKeyLast[48],int nRound);
protected:
CArray<CRACKDATA*,CRACKDATA*>m_CrackData;
int m_KeyMap[8][64];
BOOL m_bLogIt;
FILE* m_logfile;
};
#endif // !defined(AFX_DIFCRACK_H__9800F601_32A3_4E2C_9D70_754637D3985B__INCLUDED_)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -