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

📄 keygenmedlg.cpp

📁 Crypto api 使用实例
💻 CPP
字号:
//此部分从KeyGenMe复制而来,别混淆了
//跟KeyGenMe相同部分不再解释了

#include "stdafx.h"
#include "keygenme.h"
#include "keygenmeDlg.h"

#include "../crypto50/md2.h"
#include "../crypto50/md5.h"
#include "../crypto50/sha.h"        
#include "../crypto50/ripemd.h"     
#include "../crypto50/crc.h"        
#include "../crypto50/haval.h"
#include "../crypto50/tiger.h"
#include "../crypto50/panama.h"

#include "../crypto50/base64.h"

#include "../crypto50/filters.h"
#include "../crypto50/files.h"
#include "../crypto50/hex.h"

#include "../crypto50/rsa.h"		
#include "../crypto50/nbtheory.h"	//Integer  大整数操作

#include "../crypto50/modes.h"
#include "../crypto50/aes.h"
#include "../crypto50/des.h"
#include "../crypto50/idea.h"
#include "../crypto50/blowfish.h"
#include "../crypto50/tea.h"
#include "../crypto50/osrng.h"
#include "../crypto50/elgamal.h"

#include "../crypto50/ec2n.h"
#include "../crypto50/eccrypto.h"

using namespace std;
using namespace CryptoPP;


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKeygenmeDlg dialog

CKeygenmeDlg::CKeygenmeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CKeygenmeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CKeygenmeDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CKeygenmeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CKeygenmeDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CKeygenmeDlg, CDialog)
	//{{AFX_MSG_MAP(CKeygenmeDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKeygenmeDlg message handlers

BOOL CKeygenmeDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CKeygenmeDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CKeygenmeDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CKeygenmeDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

std::string IntegerToString(Integer &a,int base)
{
	 SecBlock<char> s(a.BitCount() / (BitPrecision(base)-1) + 1);
     Integer temp1=a, temp2;
     unsigned i=0,head=0;
     const char vec[]="0123456789ABCDEF";
	 char outstring[1024];

     if (a.IsNegative())
     {
             outstring[head] = '-';
             temp1.Negate();
     }

     if (!a)
             outstring[head] ='0';

     while (!!temp1)
     {
             word digit;
			 //calculate r and q such that (a == d*q + r) && (0 <= r < abs(d))
             Integer::Divide(digit, temp2, temp1, base);
             s[i++]=vec[digit];
             temp1=temp2;
     }

	 int len=i-1;
     while (i--)
     {
             outstring[head+len-i] = s[i];
     }
	 outstring[len+1]=0;
	 return outstring;
}


void CKeygenmeDlg::OnOK() 
{
	// TODO: Add extra validation here
	CString sName;  //输入的名字
	//单向算法结果的前8位字符
	string name1,name2,name3,name4,name5,name6,name7,name8;
	string code1;			//存放各种对称密码或公钥密码的运算结果
 	byte code2[100],code3[100],code4[100],code5[100],code6[100],code7[500],code8[500],code[2048];
	AutoSeededRandomPool rng;  //随机数

     try
     {

		GetDlgItemText(IDC_NAME,sName);	

		StringSource((std::string)sName, true,new HashFilter(MD5(),new HexEncoder(new StringSink(name1))));
		StringSource((std::string)sName, true,new HashFilter(SHA256(),new HexEncoder(new StringSink(name2))));
		StringSource((std::string)sName, true,new HashFilter(CRC32(),new HexEncoder(new StringSink(name3))));
		StringSource((std::string)sName, true,new HashFilter(RIPEMD160(),new HexEncoder(new StringSink(name4))));
		StringSource((std::string)sName, true,new HashFilter(Tiger(),new HexEncoder(new StringSink(name5))));
		StringSource((std::string)sName, true,new HashFilter(PanamaHash<>(),new HexEncoder(new StringSink(name6))));
		StringSource((std::string)sName, true,new HashFilter(HAVAL(32,5),new HexEncoder(new StringSink(name7))));
		StringSource((std::string)sName, true,new HashFilter(MD2(),new HexEncoder(new StringSink(name8))));

		name1=name1.substr(0,8);
		name2=name2.substr(0,8);
		name3=name3.substr(0,8);
		name4=name4.substr(0,8);
		name5=name5.substr(0,8);
		name6=name6.substr(0,8);
		name7=name7.substr(0,8);
		name8=name8.substr(0,8);

		//1 RSA   
		RSAFunction  rsa;		
		string stemp;
		stemp="0x"+name1;		
		Integer c((const char*) stemp.data());			//c以16进制输入

		Integer n("0x24DFDA27FA14D3F27DDF62CEA5D2381F9");	
		Integer d("0x1E2D9B52ADCBC20DCCDE3C721AA740E83");
		Integer m;

		rsa.Initialize(n,d);		
		m = rsa.ApplyFunction(c);	
		code1 = IntegerToString(m,16);	
		
		//2	AES	-- Rijndael 
		byte *key2 = (byte *) "\x54\x85\x9b\x34\x2c\x49\xea\x2a\x8b\x40\xb4\x48\x6d\x03\xa6\xa5";
		byte *iv2  = (byte *) "\x14\xbd\xdd\x28\xc9\x83\x35\x19\x17\x53\x03\x29\xa9\x34\x90\x74";

   		CFB_Mode<AES>::Decryption cfbDecryption2(key2, AES::DEFAULT_KEYLENGTH, iv2); //这句在论坛上发的可能有问题,但运行是没问题的应该为::Decryption而不是::Encryption
		cfbDecryption2.ProcessData(code2,(byte const*) name2.data(), name2.size());
		code2[8] = 0;

		//3	DES  
		byte *key3 = (byte *) "\x54\x85\x9b\x34\x2c\x49\xea\x2a";
		byte *iv3  = (byte *) "\x14\xbd\xdd\x28\xc9\x83\x35\x19";

   		CFB_Mode<DES>::Decryption cfbDecryption3(key3, DES::DEFAULT_KEYLENGTH, iv3);
		cfbDecryption3.ProcessData(code3,(byte const*)name3.data(),name3.size());
		code3[8] = 0;

		//4	IDEA 
		byte *key4 = (byte *) "\x54\x85\x9b\x34\x2c\x49\xea\x2a\x28\xc9\x83\x35\x19\x17\x53\x03";
		byte *iv4  = (byte *) "\x14\xbd\xdd\x28\xc9\x83\x35\x19";

   		CFB_Mode<IDEA>::Decryption cfbDecryption4(key4, IDEA::DEFAULT_KEYLENGTH, iv4);
		cfbDecryption4.ProcessData(code4,(byte const*)name4.data(), name4.size());
		code4[8] = 0;

		//5	Blowfish
		byte *key5 = (byte *) "\x54\x85\x9b\x34\x2c\x49\xea\x2a";
		byte *iv5  = (byte *) "\x14\xbd\xdd\x28\xc9\x83\x35\x19";

   		CFB_Mode<Blowfish>::Decryption cfbDecryption5(key5, Blowfish::DEFAULT_KEYLENGTH, iv5);
		cfbDecryption5.ProcessData(code5,(byte const*) name5.data(), name5.size());
		code5[8] = 0;

		//6	TEA
		byte *key6 = (byte *) "\x54\x85\x9b\x34\x2c\x49\xea\x2a\x28\xc9\x83\x35\x19\x17\x53\x03";
		byte *iv6  = (byte *) "\x14\xbd\xdd\x28\xc9\x83\x35\x19";

   		CFB_Mode<TEA>::Decryption cfbDecryption6(key6, TEA::DEFAULT_KEYLENGTH, iv6);
		cfbDecryption6.ProcessData(code6,(byte const*) name6.data(), name6.size());
		code6[8] = 0;

		//7	ElGamal 
		string elgc1024="3082018E028181008B333697371663F8869E3EC80A414E46BBAFE41F6D40E754A01ADA60FE7D12ACD16DE311C4115293114F6B92A54195909276380F04BCD4ED5CD993ED7F516DF7A752B928E5035E0D3A1A979A1CDE8387734338793C02001D59B662D4FC8F2BF0EABB1F553F9F46F57E74BCABCBA4E458812DB601FCD04609D435317181236B9702010202818038FBC56751763146BC107ECC59E9BAD3852EBC38799B41B40EF5745810BCF9DCC6D569B7E61063EA358B0DF2A194910029B72A9CFD11AD240681D3F976EDCB18D79C0530AB2944DC1E314C2B520BE23066C802754C19BF2EC15DE0439E2663383CEA5163DC857B6A5F91079F54FB47C9B33F23A9EB6B3FCBA8581524B3EC5C75028181008B333697371663F8869E3EC80A414E46BBAFE41F6D40E754A01ADA60FE7D12ACD16DE311C4115293114F6B92A54195909276380F04BCD4ED5CD993ED7F516DF7A752B928E5035E0D3A1A979A1CDE8387734338793C02001D59B662D4FC8F2BF0EABB1F553F9F46F57E74BC7F3EC6725F2FC0A6155ADCA43CEE7319E623824852";
		StringSource fc(elgc1024, true, new HexDecoder);  

		ElGamalDecryptor privC(fc);
		ElGamalEncryptor pubC(privC);
		privC.AccessKey().Precompute();
		ByteQueue queue;
		privC.AccessKey().SavePrecomputation(queue);
		privC.AccessKey().LoadPrecomputation(queue);
		pubC.Encrypt(rng, (const byte *)name7.data(), 8, code7);
		code7[256]=0;
	
		//8	ECC	ec2n
		//string pubkey="MEgwEAYHKoZIzj0CAQYFK4EEABgDNAAEAVZsz1icZPey/K8omkfutAKYfNJW7wqk1QCUhl4SgcmbKizZEp0YU1C7dzUUA/pFPos=";
		string pubkey="MEgwEAYHKoZIzj0CAQYFK4EEABgDNAAEAF8YRusnv8l4fL3yBs6kaeN96A10cTEHCwH1ogPaeq6iuT87dQ/UKw2oURAToBYNmrk=";
		ECIES<EC2N>::Encryptor pubkey2(StringSource(pubkey, true,new Base64Decoder));
		pubkey2.Encrypt(rng, (const byte *)name8.data(), 8, code8);

		//合成code,并输出到code.txt
		int len1=60;
		memcpy(code,code1.data(),len1);
		memcpy(code+len1,code2,8);
		memcpy(code+len1+8,code3,8);
		memcpy(code+len1+8*2,code4,8);
		memcpy(code+len1+8*3,code5,8);
		memcpy(code+len1+8*4,code6,8);
		memcpy(code+len1+8*5,code7,256);
		memcpy(code+len1+8*5+256,code8,256);
		
		StringSource(code,len1+8*5+256*2, true,new HexEncoder(new FileSink("code.txt")));	

		MessageBox("ok!");
     }
     catch (CryptoPP::Exception const& e)
     {
		MessageBox(e.what());
     }
 
}

⌨️ 快捷键说明

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