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

📄 keygenmedlg.cpp

📁 Crypto api 使用实例
💻 CPP
字号:
// keygenmeDlg.cpp : implementation file
//

#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"	

#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)   //Integer转换成string
{
	 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;		//输入的名字

	//从code.txt读入数据并分成八部分
	byte bCode[4096];	
	byte incode1[65],incode2[8],incode3[8],incode4[8],incode5[8],incode6[8],incode7[256],incode8[256];  //输入的
	
	string name1,name2,name3,name4,name5,name6,name7,name8,name; //存放各种对名字进行单向算法的结果
	
	//存放对code.txt进行处理(对称密码,公钥密码)后的结果
	string code1,code;			
 	byte code2[100],code3[100],code4[100],code5[100],code6[100],code7[100],code8[100];

     try
     {

		GetDlgItemText(IDC_NAME,sName);	//取输入的名字

		//进行各种单向算法,结果编码成16进制
		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))));
		
		//取各部分的前八位合成name
		name=name1.substr(0,8)+name2.substr(0,8)+name3.substr(0,8)+name4.substr(0,8)+name5.substr(0,8)+name6.substr(0,8)+name7.substr(0,8)+name8.substr(0,8);

		//从文件code.txt读入code
		FileSource ("code.txt", true, new HexDecoder(new ArraySink(bCode,612)));
		int len1=60,len2=8,len3=8,len4=8,len5=8,len6=8,len7=256,len8=256;
		
		//分解为八部分
		incode1[0]='0';	incode1[1]='x';   //构成16进制形式的字符串
		memcpy(incode1+2,bCode,len1);	incode1[2+len1]=0;

		memcpy(incode2,bCode+len1,len2);
		memcpy(incode3,bCode+len1+len2,len3);
		memcpy(incode4,bCode+len1+len2+len3,len4);
		memcpy(incode5,bCode+len1+len2+len3+len4,len5);
		memcpy(incode6,bCode+len1+len2+len3+len4+len5,len6);
		memcpy(incode7,bCode+len1+len2+len3+len4+len5+len6,len7);
		memcpy(incode8,bCode+len1+len2+len3+len4+len5+len6+len7,len8);


		//1 RSA   
		RSAFunction  rsa;		
		Integer m((const char *)incode1);			//以16进制初始化

		Integer n("0x24DFDA27FA14D3F27DDF62CEA5D2381F9");	
		Integer e("0xE401C1B");	
		Integer c;

		rsa.Initialize(n,e);		
		c = rsa.ApplyFunction(m);	  //这里c可能不足8位,所以在判断部分在前面补0了
		code1=IntegerToString(c,16);  //Integer转换成string (库中可能有现成的方法,但我没找到)

		//以下五种对称密码的mode均采用CFB
		//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>::Encryption cfbEncryption2(key2, AES::DEFAULT_KEYLENGTH, iv2); //加密
		cfbEncryption2.ProcessData(code2,(byte const*) incode2, len2);
		code2[len2] = 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>::Encryption cfbEncryption3(key3, DES::DEFAULT_KEYLENGTH, iv3);
		cfbEncryption3.ProcessData(code3,(byte const*) incode3, len3);
		code3[len3] = 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>::Encryption cfbEncryption4(key4, IDEA::DEFAULT_KEYLENGTH, iv4);
		cfbEncryption4.ProcessData(code4,(byte const*) incode4, len4);
		code4[len4] = 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>::Encryption cfbEncryption5(key5, Blowfish::DEFAULT_KEYLENGTH, iv5);
		cfbEncryption5.ProcessData(code5,(byte const*) incode5, len5);
		code5[len5] = 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>::Encryption cfbEncryption6(key6, TEA::DEFAULT_KEYLENGTH, iv6);
		cfbEncryption6.ProcessData(code6,(byte const*) incode6, len6);
		code6[len6] = 0;

		//7	ElGamal 
		//取自库中的elgc1024.dat
		string elgc1024="3082018E028181008B333697371663F8869E3EC80A414E46BBAFE41F6D40E754A01ADA60FE7D12ACD16DE311C4115293114F6B92A54195909276380F04BCD4ED5CD993ED7F516DF7A752B928E5035E0D3A1A979A1CDE8387734338793C02001D59B662D4FC8F2BF0EABB1F553F9F46F57E74BCABCBA4E458812DB601FCD04609D435317181236B9702010202818038FBC56751763146BC107ECC59E9BAD3852EBC38799B41B40EF5745810BCF9DCC6D569B7E61063EA358B0DF2A194910029B72A9CFD11AD240681D3F976EDCB18D79C0530AB2944DC1E314C2B520BE23066C802754C19BF2EC15DE0439E2663383CEA5163DC857B6A5F91079F54FB47C9B33F23A9EB6B3FCBA8581524B3EC5C75028181008B333697371663F8869E3EC80A414E46BBAFE41F6D40E754A01ADA60FE7D12ACD16DE311C4115293114F6B92A54195909276380F04BCD4ED5CD993ED7F516DF7A752B928E5035E0D3A1A979A1CDE8387734338793C02001D59B662D4FC8F2BF0EABB1F553F9F46F57E74BC7F3EC6725F2FC0A6155ADCA43CEE7319E623824852";
		StringSource fc(elgc1024, true, new HexDecoder);
		ElGamalDecryptor privC(fc);
		privC.AccessKey().Precompute();
		ByteQueue queue;
		privC.AccessKey().SavePrecomputation(queue);
		privC.AccessKey().LoadPrecomputation(queue);
		privC.Decrypt(incode7, 256, code7);  //就是256了
		code7[8] = 0;
	
		//8	ECC	ec2n
		//密钥对通过另一程序产生,已作base64编码了
		//string privkey="MIHhAgEAMIG5BgcqhkjOPQIBMIGtAgEBMB0GByqGSM49AQIwEgICAMEGCSqGSM49AQIDAgIBDzA2BBkAF4WP63qYl1Fp4XH3e0CH3gmKyKkR33sBBBkA/ftJv+bDqJ+srap6Hlu8fMHC5dgxR4gUBDMEAfSBvF8P+Ep0rWzfb970v2F5YlNy2MDF4QAl45nykDcSzPPqnjoa0X+wsyAbavfOGwUCGQEAAAAAAAAAAAAAAADH80p3j0Q6zJIOukkCAQIEIDAeAgEBBBkALS8iRX1lQxt+1iMx7E0lbnHDyG4dKHZY";
		string privkey="MIHhAgEAMIG5BgcqhkjOPQIBMIGtAgEBMB0GByqGSM49AQIwEgICAMEGCSqGSM49AQIDAgIBDzA2BBkAF4WP63qYl1Fp4XH3e0CH3gmKyKkR33sBBBkA/ftJv+bDqJ+srap6Hlu8fMHC5dgxR4gUBDMEAfSBvF8P+Ep0rWzfb970v2F5YlNy2MDF4QAl45nykDcSzPPqnjoa0X+wsyAbavfOGwUCGQEAAAAAAAAAAAAAAADH80p3j0Q6zJIOukkCAQIEIDAeAgEBBBkAXB8RJH4/KEvA2Xl7gNbSEjt3YQ0RtAFm";
		ECIES<EC2N>::Decryptor privkey2(StringSource(privkey, true,new Base64Decoder));
		privkey2.Decrypt(incode8, privkey2.CiphertextLength(8), code8);		
		code8[8] = 0;

		//合成namebing判断 name是否等于code
		string kkk;
		code="";
		if (code1.size()==7) code="0";		//不足八位补0
		if (code1.size()==6) code="00";
		if (code1.size()==5) code="000";
		kkk=(const char*)code2;code=code+code1+kkk;
		kkk=(const char*)code3;code=code+kkk;
		kkk=(const char*)code4;code=code+kkk;
		kkk=(const char*)code5;code=code+kkk;
		kkk=(const char*)code6;code=code+kkk;
		kkk=(const char*)code7;code=code+kkk;
		kkk=(const char*)code8;code=code+kkk;

		if (name == code) MessageBox("恭喜您,注册成功!");
		else MessageBox("try again!");
     }
     catch (CryptoPP::Exception const& e)
     {
		MessageBox(e.what());
     }
 
}

⌨️ 快捷键说明

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