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

📄 cdma f9dlg.cpp

📁 第三代移动系统中以Kasumi为内核的另一加密算法-f8算法的VC++实现
💻 CPP
字号:
// CDMA f9Dlg.cpp : implementation file


#include "stdafx.h"
#include "CDMA f9.h"
#include "CDMA f9Dlg.h"

#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

extern void Kasumi(char*,const char*);
extern int hextoint(char x);
extern char inttohex(int x);

#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()

/////////////////////////////////////////////////////////////////////////////
// CCDMAf9Dlg dialog

CCDMAf9Dlg::CCDMAf9Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCDMAf9Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCDMAf9Dlg)
	m_plain_text = _T("");
	m_count_i = _T("");
	m_fresh = _T("");
	m_message = _T("");
	m_direction = _T("");
	m_hex_f9_indata = _T("");
	m_ik = _T("");
	m_mac_i = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCDMAf9Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCDMAf9Dlg)
	DDX_Text(pDX, IDC_PLAIN_TEXT, m_plain_text);
	DDX_Text(pDX, IDC_COUNT_I, m_count_i);
	DDV_MaxChars(pDX, m_count_i, 32);
	DDX_Text(pDX, IDC_FRESH, m_fresh);
	DDV_MaxChars(pDX, m_fresh, 32);
	DDX_Text(pDX, IDC_MESSAGE, m_message);
	DDX_Text(pDX, IDC_DIRECTION, m_direction);
	DDV_MaxChars(pDX, m_direction, 1);
	DDX_Text(pDX, IDC_HEX_f9_INDATA, m_hex_f9_indata);
	DDX_Text(pDX, IDC_IK, m_ik);
	DDV_MaxChars(pDX, m_ik, 32);
	DDX_Text(pDX, IDC_MAC_I, m_mac_i);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCDMAf9Dlg, CDialog)
	//{{AFX_MSG_MAP(CCDMAf9Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_EN_CHANGE(IDC_PLAIN_TEXT, OnChangePlainText)
	ON_EN_CHANGE(IDC_COUNT_I, OnChangeCountI)
	ON_EN_CHANGE(IDC_FRESH, OnChangeFresh)
	ON_EN_CHANGE(IDC_DIRECTION, OnChangeDirection)
	ON_EN_CHANGE(IDC_IK, OnChangeIk)
	ON_BN_CLICKED(IDC_CLEAR, OnClear)
	ON_BN_CLICKED(IDC_AUTOINPUT, OnAutoinput)
	ON_BN_CLICKED(IDC_CLACULATE_MAC_I, OnClaculateMacI)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_F9HELP, OnF9help)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCDMAf9Dlg message handlers

BOOL CCDMAf9Dlg::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 CCDMAf9Dlg::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 CCDMAf9Dlg::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 CCDMAf9Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


// 明文的输入
void CCDMAf9Dlg::OnChangePlainText() 
{
	UpdateData(TRUE);

	m_hex_f9_indata="";
	m_mac_i="";
	
	int i;
	for(i=0;i<m_plain_text.GetLength();i++)
		if(!isxdigit(m_plain_text[i]))
			m_plain_text.Delete(i);
	m_message=m_plain_text;

	UpdateData(FALSE);		
}


// 参数COUNT-I的输入
void CCDMAf9Dlg::OnChangeCountI() 
{
	UpdateData(TRUE);

	m_hex_f9_indata="";
	m_mac_i="";

	int i;
	for(i=0;i<m_count_i.GetLength();i++)
		if(m_count_i[i]!='0' && m_count_i[i]!='1')
			m_count_i.Delete(i);
	UpdateData(FALSE);	
}


// 参数FRESH的输入
void CCDMAf9Dlg::OnChangeFresh() 
{
	UpdateData(TRUE);

	m_hex_f9_indata="";
	m_mac_i="";
	
	int i;
	for(i=0;i<m_fresh.GetLength();i++)
		if(m_fresh[i]!='0' && m_fresh[i]!='1')
			m_fresh.Delete(i);
	
	UpdateData(FALSE);	
}


// 参数DIRECTION的输入
void CCDMAf9Dlg::OnChangeDirection() 
{
	UpdateData(TRUE);
	
	m_hex_f9_indata="";
	m_mac_i="";
	
	int i;
	for(i=0;i<m_direction.GetLength();i++)
		if(m_direction[i]!='0' && m_direction[i]!='1')
			m_direction.Delete(i);
	
	UpdateData(FALSE);	
}


// 参数IK的输入 
void CCDMAf9Dlg::OnChangeIk() 
{
	UpdateData(TRUE);
	
	m_mac_i="";
	
	int i;
	for(i=0;i<m_ik.GetLength();i++)
		if(!isxdigit(m_ik[i]))
			m_ik.Delete(i);
	
	UpdateData(FALSE);		
}


// 清零按钮
void CCDMAf9Dlg::OnClear() 
{
	m_plain_text="";
	m_count_i="";
	m_fresh="";
	m_message="";
	m_direction="";
	m_hex_f9_indata="";
	m_ik="";
	m_mac_i="";
	
	UpdateData(FALSE);	
}


// 自动输入按钮,设置明文为64bit
void CCDMAf9Dlg::OnAutoinput() 
{
	int i;
	int j=0;
	char temp[49];
	char f9in[64];

	srand((unsigned)time(NULL));
	for(i=0;i<1;i++)
	{
		temp[i]=inttohex(rand()%2);
	}
	temp[i]='\0';
	m_direction=temp;

	for(i=0;i<16;i++)
		temp[i]=inttohex(rand()%16);
	temp[i]='\0';
	m_plain_text=temp;
	
	for(i=0;i<32;i++,j++)
	{
		temp[i]=inttohex(rand()%2);
		f9in[j]=temp[i];
	}
	temp[i]='\0';
	m_count_i=temp;

	for(i=0;i<32;i++,j++)
	{
		temp[i]=inttohex(rand()%2);
		f9in[j]=temp[i];
	}
	temp[i]='\0';
	m_fresh=temp;

	m_message=m_plain_text;

	int sum[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

	// count-i 和 fresh
	for(i=0;i<64;i++)
		sum[i/4] += (hextoint(f9in[i])<<(3-i%4));
	for(i=0;i<16;i++)
		temp[i]=inttohex(sum[i]);

	// message 直接给temp
	for(i=16;i<32;i++)
		temp[i]=m_message[i-16];
	if(m_direction[0]=='0')
		temp[i]='4';
	else temp[i]='C';

	for(i=33;i<48;i++)
		temp[i]='0';
	temp[i]='\0';
	m_hex_f9_indata=temp;

	for(i=0;i<32;i++)
		temp[i]=inttohex(rand()%16);
	temp[i]='\0';
	m_ik=temp;
	
	m_mac_i="";
	
	UpdateData(FALSE);
	
}


// 计算MAC-I
void CCDMAf9Dlg::OnClaculateMacI() 
{
	UpdateData(TRUE);

	// 就各种参数的错误输入进行报错
	if(m_plain_text.GetLength()==0)
	{	
		MessageBox("请输入明文!", "参数输入错误", MB_OK | MB_ICONERROR);
		return ;
	}

	if(m_count_i.GetLength()!=32)
	{	
		MessageBox("请检查参数 COUNT-I 的输入!", "参数输入错误", MB_OK | MB_ICONERROR);
		return ;
	}
	
	if(m_fresh.GetLength()!=32)
	{	
		MessageBox("请检查参数 FRESH 的输入!", "参数输入错误", MB_OK | MB_ICONERROR);
		return ;
	}
	
	if(m_direction.GetLength()!=1)
	{	
		MessageBox("请检查参数 DIRECTION 的输入!", "参数输入错误", MB_OK | MB_ICONERROR);
		return ;
	}
	
	if(m_ik.GetLength()!=32)
	{	
		MessageBox("请检查参数 IK 的输入!", "参数输入错误", MB_OK | MB_ICONERROR);
		return ;
	}

	m_message=m_plain_text;

	// 计算f9的实时输入

	char *f9in;
	if( (64+m_plain_text.GetLength()*4+2)%64==0)
		f9in = new char[64+m_plain_text.GetLength()*4+2];
	else f9in = new char[((64+m_plain_text.GetLength()*4+2)/64+1)*64];

	int i,j;
	j=0;

	for(i=0;i<32;i++,j++)
		f9in[j]=m_count_i[i];
	
	for(i=0;i<32;i++,j++)
		f9in[j]=m_fresh[i];
	
	f9in[64+m_message.GetLength()*4]=m_direction[0];
	f9in[64+m_message.GetLength()*4+1]='1';

	// 非64bit的整数倍,加若干个0使其成为64bit的整数倍
	if( (64+m_plain_text.GetLength()+2)%64!=0)
		for(i=64+m_message.GetLength()*4+2;i<(((64+m_plain_text.GetLength()*4+2)/64+1)*64);i++)
			f9in[i]='0';

	// 把各个参数整合
	char *temp;
	temp = new char[((64+m_plain_text.GetLength()*4+2)/64+1)*64/4];
	int sum[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	for(i=0;i<64;i++)
		sum[i/4] += (hextoint(f9in[i])<<(3-i%4));
	for(i=0;i<16;i++)
		temp[i]=inttohex(sum[i]);
	for(i=0;i<m_message.GetLength();i++)
		temp[i+16]=m_message[i];

	if((64+m_plain_text.GetLength()*4+2)%64!=0)
	{
		if(m_direction[0]=='0')
			temp[16+m_message.GetLength()]='4';
		else temp[16+m_message.GetLength()]='C';
		for(i=16+m_message.GetLength()+1;i<((64+m_plain_text.GetLength()*4+2)/64+1)*64/4;i++)
		temp[i]='0';
	}
	temp[i]='\0';

	// 16进制显示
	m_hex_f9_indata=temp;

	char km_128bit[]="ABCDEF01234567890123456789ABCDEF";
	char A[]="0000000000000000";
	char B[]="0000000000000000";
	int blocks;
	blocks = int(ceil((double)m_hex_f9_indata.GetLength()/16));

	for (j=0;j<blocks;j++)
	{
		for (i=0;i<16;i++)
			A[i]=inttohex(hextoint(m_hex_f9_indata[i+j*16])^hextoint(A[i]));

		Kasumi(A,m_ik);

		for (i=0;i<16;i++)
			B[i]=inttohex(hextoint(B[i])^hextoint(A[i]));
	}

	for (i=0;i<32;i++)
		temp[i]=inttohex(hextoint(m_ik[i])^hextoint(km_128bit[i]));

	Kasumi(B,temp);

	B[8]='\0';

	m_mac_i=B;

	UpdateData(FALSE);

}


// 保存按钮
void CCDMAf9Dlg::OnSave() 
{
	CFileDialog dlg(FALSE, ".txt", "", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "text file(*.txt)|*.txt||", this);

	if (dlg.DoModal() != IDOK)
		return;

	CStdioFile file;

	if (!file.Open(dlg.GetPathName(), CFile::modeWrite | CFile::modeCreate))
	{
		MessageBox("open failed!");
		return;
	}

	file.WriteString("★★★ 3G 完整性算法 f9 ( Kasumi ) ★★★\n\n");
	file.WriteString("明文:\n");
	file.WriteString(m_plain_text+"\n\n");
	file.WriteString("f9函数的输入数据:\n");
	file.WriteString(m_hex_f9_indata+"\n\n");
	file.WriteString("IK:\n");
	file.WriteString(m_ik+"\n\n");
	file.WriteString("数据完整性的消息认证码:\n");
	file.WriteString(m_mac_i+"\n\n");

	file.Close();
}


// 退出按钮
void CCDMAf9Dlg::OnExit() 
{
	OnCancel();	
}


// 帮助按钮
void CCDMAf9Dlg::OnF9help() 
{
	::ShellExecute(m_hWnd, "open", "f9help.chm", NULL, NULL, SW_SHOWDEFAULT);	
}

⌨️ 快捷键说明

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