📄 rc4_cipher.cpp
字号:
// RC4_Cipher.cpp : implementation file
//
#include "stdafx.h"
#include "MY_CAP.h"
#include "math.h"
#include "RC4_Cipher.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRC4_Cipher dialog
CMY_CAPDlg *Main_CapR;
CRC4_Cipher::CRC4_Cipher(CWnd* pParent /*=NULL*/)
: CDialog(CRC4_Cipher::IDD, pParent)
{
//{{AFX_DATA_INIT(CRC4_Cipher)
m_Key1 = 0;
m_Key2 = 0;
m_Key3 = 0;
m_Key4 = 0;
m_CK = _T("");
m_CS = _T("");
m_CKey_Stream = _T("");
m_IStream_Size = 0;
//}}AFX_DATA_INIT
}
void CRC4_Cipher::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
// DDX_Text(pDX, IDD_CREATE,m_BCreate;);
//{{AFX_DATA_MAP(CRC4_Cipher)
DDX_Text(pDX, IDC_KEY1, m_Key1);
DDV_MinMaxInt(pDX, m_Key1, 0, 255);
DDX_Text(pDX, IDC_KEY2, m_Key2);
DDV_MinMaxInt(pDX, m_Key2, 0, 255);
DDX_Text(pDX, IDC_KEY3, m_Key3);
DDV_MinMaxInt(pDX, m_Key3, 0, 255);
DDX_Text(pDX, IDC_KEY4, m_Key4);
DDV_MinMaxInt(pDX, m_Key4, 0, 255);
DDX_Text(pDX, IDC_K, m_CK);
DDX_Text(pDX, IDC_S, m_CS);
DDX_Text(pDX, IDC_KEY_STREAM, m_CKey_Stream);
DDX_Text(pDX, IDC_STREAM_SIZE, m_IStream_Size);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRC4_Cipher, CDialog)
//{{AFX_MSG_MAP(CRC4_Cipher)
ON_COMMAND(IDD_ENCIPHER, OnEncipher)
ON_COMMAND(IDD_DECIPHER, OnDecipher)
ON_COMMAND(IDD_INITIALIZE_KEY, OnInitializeKey)
ON_COMMAND(IDD_QUIT, OnQuit)
ON_COMMAND(IDD_CREATE, OnCreate)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRC4_Cipher message handlers
////////////////////////////////////////////////////////////////////////////
////////////////////////// RC4 Cipher //////////////////////////////////
///////////////////////////////////////////////////////////////////////////
void CRC4_Cipher::OnInitializeKey() //初始化密钥
{
// TODO: Add your command handler code here
OnPreInitializeKey();
m_CK = Output_CString(IK);
m_CS = Output_CString(IS);
UpdateData(FALSE);
}
void CRC4_Cipher::OnCreate() //产生密钥序列
{
// TODO: Add your command handler code here
Evaluate_Key();
UpdateData(TRUE);
m_CKey_Stream = "";
int m = -1, j = 0, t, k, i, temp, l;
for (i = 0; i < ceil(m_IStream_Size/8.0); i++)
{
m = (m + 1) % 256;
j = (j + IS[m]) % 256;
Swap(IS[m],IS[j]);
t = (IS[m] + IS[j]) % 256;
k = IS[t];
temp = k;
if ( i % 2 == 0 && i != 0)
{
m_CKey_Stream += 13;
m_CKey_Stream += '\n';
}
for (l = 7; l >= 0; l--)
{
m_CKey_Stream += (temp / (int)pow(2, l) + '0');
temp = temp % (int) pow(2, l);
}
}
m_CS = Output_CString(IS);
UpdateData(FALSE);
}
void CRC4_Cipher::OnEncipher() //加密
{
// TODO: Add your command handler code here
Evaluate_Key();
Main_CapR -> UpdateData(TRUE);
m_Plaintext = Main_CapR -> m_Plaintext;
m_Ciphertext = "";
m_CKey_Stream = "";
m_IStream_Size = 0;
int count, i, j = 0, t, k, m = -1, temp, l;
count = m_Plaintext.GetLength ();
for (i = 0; i < count; i++)
{
m = (m + 1) % 256;
j = (j + IS[m]) % 256;
Swap(IS[m],IS[j]);
t = (IS[m] + IS[j]) % 256;
k = IS[t];
temp = k;
for (l = 7; l >= 0; l--)
{
m_CKey_Stream += (temp / (int)pow(2, l) + '0');
temp = temp % (int) pow(2, l);
m_IStream_Size++;
}
if (m_IStream_Size % 16 == 0)
{
m_CKey_Stream += 13;
m_CKey_Stream += '\n';
}
temp = (int)m_Plaintext[i];
temp = temp ^ k;
for (l = 7; l >= 0; l--)
{
m_Ciphertext += (temp / (int)pow(2, l) + '0');
temp = temp % (int) pow(2, l);
}
}
Main_CapR -> m_Ciphertext = m_Ciphertext;
m_CS = Output_CString(IS);
Main_CapR ->UpdateData(FALSE);
UpdateData(FALSE);
if (Main_CapR ->m_Ciphertext.GetLength () != 0)
Main_CapR -> OnSave_Ciphertext ();
}
void CRC4_Cipher::OnDecipher() //解密
{
// TODO: Add your command handler code here
Evaluate_Key();
Main_CapR -> UpdateData(TRUE);
m_Ciphertext = Main_CapR -> m_Ciphertext;
m_Plaintext = "";
m_CKey_Stream = "";
m_IStream_Size = 0;
int count, i, j = 0, t, k, m = -1, temp, l, data;
count = m_Ciphertext.GetLength ();
for (i = 0; i < count;)
{
m = (m + 1) % 256;
j = (j + IS[m]) % 256;
Swap(IS[m],IS[j]);
t = (IS[m] + IS[j]) % 256;
k = IS[t];
temp = k;
for (l = 7; l >= 0; l--)
{
m_CKey_Stream += (temp / (int)pow(2, l) + '0');
temp = temp % (int) pow(2, l);
m_IStream_Size++;
}
if (m_IStream_Size % 16 == 0)
{
m_CKey_Stream += 13;
m_CKey_Stream += '\n';
}
data = 0;
for (l = 7; l >= 0; l--)
{
if (i >= count) break;
if (m_Ciphertext[i] == '0' || m_Ciphertext[i] == '1')
{
temp = m_Ciphertext[i] - '0';
temp = temp ^ (k / (int)pow(2, l));
temp = temp * (int)pow(2, l);
data += temp;
k = k % (int)pow(2, l);
}
else if (m_Ciphertext[i] == ' ')
{
l++;
i++;
continue;
}
else if (m_Ciphertext[i] == 13 && m_Ciphertext[i+1] == '\n')
{
i += 2;
l++;
continue;
}
else
{
MessageBox("Your input is illegal");
return;
}
i++;
}
m_Plaintext += data;
}
Main_CapR -> m_Plaintext = m_Plaintext;
m_CS = Output_CString(IS);
Main_CapR -> UpdateData(FALSE);
UpdateData(FALSE);
if (Main_CapR ->m_Plaintext.GetLength () != 0)
Main_CapR ->OnSave_Plaintext ();
}
void CRC4_Cipher::OnQuit() //退出
{
// TODO: Add your command handler code here
OnOK();
}
void CRC4_Cipher::Swap(int &x, int &y) //交换两个整数
{
int temp;
temp = x;
x = y;
y = temp;
}
CString Thans_IntToChar(CString& Chars, int data) //将整形转化为字符型
{
CString temp,Temp;
while(true)
{
temp = ('0' + data % 10);
temp += Temp;
Temp = temp;
data = data / 10;
if (data == 0) break;
}
Chars += temp;
return Chars;
}
CString CRC4_Cipher::Output_CString(int *Pinput) //将整形数组的内容,存进EDIT对象,方便输出
{
int i;
CString temp = "";
for (i = 0; i < 256; i++)
{
temp = Thans_IntToChar(temp,i);
temp += " ";
temp = Thans_IntToChar(temp,Pinput[i]);
temp += 13;
temp += '\n';
}
return temp;
}
void CRC4_Cipher::Trans_Dialog(CWnd *temp) //传递主界面指针
{
Main_CapR = (CMY_CAPDlg *)temp;
}
void CRC4_Cipher::OnPreInitializeKey() //预处理密钥
{
UpdateData(TRUE);
int i, j = 0;
int temp[4];
temp[0] = m_Key1;
temp[1] = m_Key2;
temp[2] = m_Key3;
temp[3] = m_Key4;
for (i = 0; i < 256; i++)
{
IS[i] = i;
m_IS[i] = i;
IK[i] = temp[i%4];
}
for (i = 0; i < 256; i++)
{
j = (j + IK[i]+ IS[i]) % 256;
Swap(IS[i],IS[j]);
Swap(m_IS[i], m_IS[j]);
}
}
void CRC4_Cipher::Evaluate_Key() // 还原初始密钥
{
for (int i = 0; i < 256; i++)
IS[i] = m_IS[i];
}
BOOL CRC4_Cipher::OnInitDialog()
{
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -