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

📄 cdma f8dlg.cpp

📁 WCDMA移动通信系统中机密性算法F8的VC++实现程序
💻 CPP
字号:
// CDMA f8Dlg.cpp : implementation file

#include "stdafx.h"
#include "CDMA f8.h"
#include "CDMA f8Dlg.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()

/////////////////////////////////////////////////////////////////////////////
// CCDMAf8Dlg dialog

CCDMAf8Dlg::CCDMAf8Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCDMAf8Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCDMAf8Dlg)
	m_plain_text = _T("");
	m_count_c = _T("");
	m_bearer = _T("");
	m_direction = _T("");
	m_ck = _T("");
	m_hex_f8_indata = _T("");
	m_key = _T("");
	m_cipher_text = _T("");
	m_text_after_decryption = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCDMAf8Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCDMAf8Dlg)
	DDX_Text(pDX, IDC_PLAIN_TEXT, m_plain_text);
	DDX_Text(pDX, IDC_COUNT_C, m_count_c);
	DDV_MaxChars(pDX, m_count_c, 32);
	DDX_Text(pDX, IDC_BEARER, m_bearer);
	DDV_MaxChars(pDX, m_bearer, 5);
	DDX_Text(pDX, IDC_DIRECTION, m_direction);
	DDV_MaxChars(pDX, m_direction, 1);
	DDX_Text(pDX, IDC_CK, m_ck);
	DDV_MaxChars(pDX, m_ck, 32);
	DDX_Text(pDX, IDC_HEX_f8_INDATA, m_hex_f8_indata);
	DDX_Text(pDX, IDC_KEY, m_key);
	DDX_Text(pDX, IDC_CIPHER_TEXT, m_cipher_text);
	DDX_Text(pDX, IDC_TEXT_AFTER_DECRYPTION, m_text_after_decryption);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCDMAf8Dlg, CDialog)
	//{{AFX_MSG_MAP(CCDMAf8Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_EN_CHANGE(IDC_PLAIN_TEXT, OnChangePlainText)
	ON_EN_CHANGE(IDC_COUNT_C, OnChangeCountC)
	ON_EN_CHANGE(IDC_BEARER, OnChangeBearer)
	ON_EN_CHANGE(IDC_DIRECTION, OnChangeDirection)
	ON_EN_CHANGE(IDC_CK, OnChangeCk)
	ON_BN_CLICKED(IDC_CLEAR, OnClear)
	ON_BN_CLICKED(IDC_AUTOINPUT, OnAutoinput)
	ON_BN_CLICKED(IDC_CLACULATE_KEY, OnClaculateKey)
	ON_BN_CLICKED(IDC_ENCRYPTION, OnEncryption)
	ON_BN_CLICKED(IDC_DECRYPTION, OnDecryption)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_F8HELP, OnHelp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCDMAf8Dlg message handlers

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

// 明文的输入
void CCDMAf8Dlg::OnChangePlainText() 
{
	UpdateData(TRUE);
	
	m_hex_f8_indata="";
	m_key="";
	m_cipher_text="";
	m_text_after_decryption="";

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

	UpdateData(FALSE);	
}


// 参数COUNT-C的输入
void CCDMAf8Dlg::OnChangeCountC() 
{
	UpdateData(TRUE);
	
	m_hex_f8_indata="";
	m_key="";
	m_cipher_text="";
	m_text_after_decryption="";

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


// 参数BEARER的输入
void CCDMAf8Dlg::OnChangeBearer() 
{
	UpdateData(TRUE);
	
	m_hex_f8_indata="";
	m_key="";
	m_cipher_text="";
	m_text_after_decryption="";
	
	int i;
	
	for(i=0;i<m_bearer.GetLength();i++)
		if(m_bearer[i]!='0' && m_bearer[i]!='1')
			m_bearer.Delete(i);
	UpdateData(FALSE);	
}


// 参数DIRECTION的输入
void CCDMAf8Dlg::OnChangeDirection() 
{
	UpdateData(TRUE);

	m_hex_f8_indata="";
	m_key="";
	m_cipher_text="";
	m_text_after_decryption="";
	
	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);	
}


// 参数CK的输入
void CCDMAf8Dlg::OnChangeCk() 
{
	UpdateData(TRUE);

	m_key="";
	m_cipher_text="";
	m_text_after_decryption="";
	
	int i;
	for(i=0;i<m_ck.GetLength();i++)
		if(!isxdigit(m_ck[i]))
			m_ck.Delete(i);
	
	UpdateData(FALSE);		
}


// 清零按钮
void CCDMAf8Dlg::OnClear() 
{
	m_plain_text="";
	m_count_c="";
	m_bearer="";
	m_direction="";
	m_hex_f8_indata="";
	m_ck="";
	m_key="";
	m_cipher_text="";
	m_text_after_decryption="";
	
	UpdateData(FALSE);	
}


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

	srand((unsigned)time(NULL));
	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);
		f8in[j]=temp[i];
	}
	temp[i]='\0';
	m_count_c=temp;

	for(i=0;i<5;i++,j++)
	{
		temp[i]=inttohex(rand()%2);
		f8in[j]=temp[i];
	}
	temp[i]='\0';
	m_bearer=temp;

	for(i=0;i<1;i++,j++)
	{
		temp[i]=inttohex(rand()%2);
		f8in[j]=temp[i];
	}
	temp[i]='\0';
	m_direction=temp;

	for(j=38;j<64;j++)
		f8in[j]='0';
	f8in[47]='1';
	
	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(f8in[i])<<(3-i%4));
	for(i=0;i<16;i++)
		temp[i]=inttohex(sum[i]);
	temp[i]='\0';
	m_hex_f8_indata=temp;

	for(i=0;i<32;i++)
		temp[i]=inttohex(rand()%16);
	temp[i]='\0';
	m_ck=temp;
	
	m_key="";
	m_cipher_text="";
	m_text_after_decryption="";
	
	UpdateData(FALSE);
	
}


// 计算密钥流按钮
void CCDMAf8Dlg::OnClaculateKey() 
{
	UpdateData(TRUE);

	m_cipher_text="";
	m_text_after_decryption="";

	// 就各种参数的错误输入进行报错
	if(m_plain_text.GetLength()==0)
	{	
		MessageBox("请输入明文!", "参数输入错误", MB_OK | MB_ICONERROR);
		return ;
	}
	
	if(m_count_c.GetLength()!=32)
	{	
		MessageBox("请检查参数 COUNT-C 的输入!", "参数输入错误", MB_OK | MB_ICONERROR);
		return ;
	}
	
	if(m_bearer.GetLength()!=5)
	{	
		MessageBox("请检查参数 BEARER 的输入!", "参数输入错误", MB_OK | MB_ICONERROR);
		return ;
	}
	
	if(m_direction.GetLength()!=1)
	{	
		MessageBox("请检查参数 DIRECTION 的输入!", "参数输入错误", MB_OK | MB_ICONERROR);
		return ;
	}
	
	if(m_ck.GetLength()!=32)
	{	
		MessageBox("请检查参数 CK 的输入!", "参数输入错误", MB_OK | MB_ICONERROR);
		return ;
	}

	int i,j;
	unsigned short rol;
	char temp[33];
	j=0;

	// 计算实时f8输入

	char f8in[64];
	
	// COUNTC,BEARER,DIRECTION
	for(i=0;i<32;i++,j++)
		f8in[j]=m_count_c[i];
	for(i=0;i<5;i++,j++)
		f8in[j]=m_bearer[i];
	for(i=0;i<1;i++,j++)
		f8in[j]=m_direction[i];
	
	// LENGTH
	unsigned short length=m_plain_text.GetLength()*4;
	for(i=0;i<16;i++)
	{
		rol=length<<i;
		f8in[38+i]=inttohex(rol>>15);
	}
	
	// 添加的O
	for(i=54;i<64;i++)
		f8in[i]='0';
	
	// 16进制显示
	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(f8in[i])<<(3-i%4));
	for(i=0;i<16;i++)
		temp[i]=inttohex(sum[i]);
	temp[i]='\0';
	m_hex_f8_indata=temp;
	UpdateData(FALSE);


	// 常数km,128bit
	char km_128bit[]="0123456789ABCDEF0123456789ABCDEF";
	// 初始化A
	char A[]="0000000000000000";

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

	char *p;
	p=new char[m_hex_f8_indata.GetLength()+1];
	strcpy(p,(LPCTSTR)m_hex_f8_indata);
	p[m_hex_f8_indata.GetLength()]='\0';

	char *q;
	q=new char[m_ck.GetLength()+1];
	strcpy(q,(LPCTSTR)m_ck);
	q[m_ck.GetLength()]='\0';

	Kasumi(p,temp);

	int blocks;
	if (length%64==0)
		blocks=length/64;
	else	blocks=length/64+1;
	
	char *ks;
	ks = new char[16*blocks];
	for(i=0;i<16;i++)
		ks[i]='0';

	for(i=0;i<blocks;i++)
	{	
		int j;
		for (j=0;j<16;j++)
			A[j]=p[j];

		A[14]=inttohex(hextoint(A[14])^(i>>4));
		A[15]=inttohex(hextoint(A[15])^(i&15));

		if(i!=0)
		for(j=0;j<16;j++)
			A[j]=inttohex( hextoint(A[j]) ^ hextoint(ks[j+(i-1)*16]) );	
	
		Kasumi(A,q);

		for(j=0;j<16;j++)
		ks[j+i*16]=A[j];
	}

	int num;
	if (length%4!=0)
	{
		num=(int)floor(length/4);//135,num=33
		int left_over=length%4;//135%4=3
		ks[num]=inttohex( hextoint(ks[num]) >> (4-left_over) );
	}
	else
		num=(int)floor(length/4)-1;//136,num=33

	char *ks_display;
	ks_display = new char[num+1];
	for(i=0;i<num+1;i++)
	ks_display[i]=ks[i];
	ks_display[i]='\0';

	m_key=ks_display;


	UpdateData(FALSE);
		delete []p;
		delete []q;
		delete []ks;
		//delete []ks_display;
}

// 加密按钮
void CCDMAf8Dlg::OnEncryption() 
{
	UpdateData(TRUE);

	if(m_plain_text.GetLength()!=0 && m_key.GetLength()==0)
	{	
		MessageBox("请先计算密钥流!", "还未计算密钥流", MB_OK | MB_ICONERROR);
		return ;
	}


	int i;
	char *temp;
	temp = new char[m_key.GetLength()+1];
	for(i=0;i<m_key.GetLength();i++)
		temp[i] = inttohex(hextoint(m_plain_text[i])^hextoint(m_key[i]));
	temp[i] = '\0';
	m_cipher_text = temp;
	UpdateData(FALSE);
	delete []temp;
	
}


// 解密按钮
void CCDMAf8Dlg::OnDecryption() 
{
	UpdateData(TRUE);

	if(m_plain_text.GetLength()!=0 && m_key.GetLength()==0)
	{	
		MessageBox("请先计算密钥流!", "还未计算密钥流", MB_OK | MB_ICONERROR);
		return ;
	}
	
	if(m_plain_text.GetLength()!=0 && m_key.GetLength()!=0 && m_cipher_text.GetLength()==0)
	{	
		MessageBox("请先加密!", "还未加密", MB_OK | MB_ICONERROR);
		return ;
	}

	int i;
	char *temp;
	temp = new char[m_key.GetLength()+1];
	
	for(i=0;i<m_key.GetLength();i++)
		temp[i] = inttohex(hextoint(m_cipher_text[i])^hextoint(m_key[i]));
	temp[i] = '\0';
	m_text_after_decryption = temp;
	UpdateData(FALSE);
	delete []temp;
}


// 保存数据
void CCDMAf8Dlg::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 机密性算法 f8 ( Kasumi ) ★★★\n\n");
	file.WriteString("明文:\n");
	file.WriteString(m_plain_text+"\n\n");
	file.WriteString("f8函数的输入数据:\n");
	file.WriteString(m_hex_f8_indata+"\n\n");
	file.WriteString("CK:\n");
	file.WriteString(m_ck+"\n\n");
	file.WriteString("密钥流:\n");
	file.WriteString(m_key+"\n\n");
	file.WriteString("密文:\n");
	file.WriteString(m_cipher_text+"\n\n");
	file.WriteString("解密后的数据:\n");
	file.WriteString(m_text_after_decryption+"\n\n");

	file.Close();
}


// 退出
void CCDMAf8Dlg::OnExit() 
{
	OnCancel();
}


// 帮助
void CCDMAf8Dlg::OnHelp() 
{
	::ShellExecute(m_hWnd, "open", "f8help.chm", NULL, NULL, SW_SHOWDEFAULT);	
}

⌨️ 快捷键说明

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