📄 cryptographydlg.cpp
字号:
char tempChar; //定义密文变量
CString cipherText = "";
UpdateData(TRUE);
Length = m_nFirstText.GetLength();
m_nFirstText.MakeUpper(); //将输入的字符串全部变为大些
LengthKey = strKey.GetLength();
for(;i < Length; i++)
{
j = j % LengthKey;
//处于字符'A'与'Z'之间
if(m_nFirstText.GetAt(i) > 64 && m_nFirstText.GetAt(i) < 91)
{
tempChar = (m_nFirstText.GetAt(i) - 65 +
strKey.GetAt(j) - 65) % 26 + 65;
j++;
}
cipherText = cipherText + tempChar;
}
m_nLastText = cipherText;
UpdateData(FALSE);
}
break;
case 3: //置换密码(Permutation密码)加密
{
int Length = 0;
int LengthKey = 0;
int i = 0;
int j = 0;
int index = 0;
int k = 0;
char tempChar; //定义一个零时变量
CString CipherText = "";
CString strKey("4312567"); //定义密钥为4312567
UpdateData(TRUE);
m_nFirstText.MakeUpper(); //将字符串转换为大写
Length = m_nFirstText.GetLength();
LengthKey = strKey.GetLength();
int num1 = Length % LengthKey; //空缺的字符个数
int num2 = (int)ceil(((double)Length) / (double)LengthKey); //行数
if (num1 == 0) //刚好除尽
{
//Find()函数用于查找字符串,从字符串的开始位置进行查找
for(char ch = '1';ch <= '7';ch ++)
{
int index = strKey.Find(ch);
for(k = index; k < Length;k = k + LengthKey)
{
tempChar = m_nFirstText.GetAt(k);
CipherText = CipherText + tempChar;
}
}
m_nLastText = CipherText;
UpdateData(FALSE);
}
else if(num1 != 0) //没有除尽,将空缺的部分以字符'*'填充
{
for (int kk = 0;kk < LengthKey - num1 ;kk ++)
{
m_nFirstText += 'X'; //将空缺的部分用字符X填充
}
Length = m_nFirstText.GetLength();
for(char ch = '1';ch <= '7';ch ++)
{
int index = strKey.Find(ch);
for(k = index; k < Length;k = k + LengthKey)
{
tempChar = m_nFirstText.GetAt(k);
CipherText = CipherText + tempChar;
}
}
m_nLastText = CipherText;
UpdateData(FALSE);
}
}
break;
default:
break;
}
}
//////////////////////////////////////////////////////////////////////////
/////////////////////////////对文件解密/////////////////////////////////
void CCryptographyDlg::OnBreakSecret()
{
UpdateData(TRUE);
if(m_nPasswordType == -1)
{
MessageBox("请选择你的解密类型!",NULL,MB_OK);
return;
}
UpdateData(TRUE);
if (m_nFirstText == "")
{
MessageBox("请输入你要解密的密文!",NULL,MB_OK);
return;
}
UpdateData(TRUE);
switch(m_nPasswordType)
{
case 0: //Caesar密码解密
{
UpdateData(TRUE); //获取文本框中输入的内容
CString PlainText = ""; //定义明文字符串
char tempChar; //定义一个临时的字符变量
int i = 0;
int Length = m_nFirstText.GetLength();
m_nFirstText.MakeUpper(); //将字符串全部转换为大写
for (i = 0;i < Length;i ++)
{
tempChar = m_nFirstText.GetAt(i);
if (' ' == tempChar) //对于空格符,直接保留空格,不做转换
{
tempChar = tempChar;
}
else if (tempChar == 'A' || tempChar == 'B' || tempChar == 'C')
{
tempChar = tempChar - 3 + 26;
}
else
{
tempChar = tempChar - 3;
}
PlainText = PlainText + tempChar;
}
m_nLastText = PlainText;
UpdateData(FALSE);
}
break;
case 1: //Playfair密码解密
{
int Length = 0; //密文长度
int LengthKey = 0; //密钥长度
int i = 0;
int j = 0;
char tempChar1; //用于保存加密后该字符对第一个字符的值
char tempChar2; //用于保存加密后该字符对第二个字符的值
CString StrCipherTemp = ""; //定义密文
CString strKey = "monarchy";
strKey.MakeUpper();
Length = m_nFirstText.GetLength();
m_nFirstText.MakeUpper();
LengthKey = strKey.GetLength();
int RowX1; //第一个字母的行号
int ColumnY1 ; //第一个字母的列号
int RowX2; //第二个字母的行号
int ColumnY2; //第二个字母的列号
//判断密文的字符个数是否为偶数,如果不是偶数则密文
//属于非法密文,要求重新输入,因为通过明文加密得到的
//正规密文的字符个数不可能为奇数
if (Length % 2 != 0)
{
MessageBox("你输入的密文为非法密文,请重新输入!!",NULL,MB_OK);
return;
}
//碰到字母j则将字母J所在的位置用字母i来代替
if(m_nFirstText.GetAt(i) == 'J')
m_nFirstText.SetAt(i,'I');
if(m_nFirstText.GetAt(i + 1) == 'J')
m_nFirstText.SetAt(i + 1,'I');
///////////////////////////////////对密文解密/////////////////////////////////
for (i = 0;i < Length;i = i + 2)
{
//获得指定字符对的行号和列号
RowX1 = GetX(m_nFirstText.GetAt(i));
ColumnY1 = GetY(m_nFirstText.GetAt(i));
RowX2 = GetX(m_nFirstText.GetAt(i + 1));
ColumnY2 = GetY(m_nFirstText.GetAt(i + 1));
//如果该字母对落在矩阵同一行则由其左边的字母来代替
if(RowX1 == RowX2) //行号相同
{
tempChar1 = Metrix[RowX1][((ColumnY1 + 4) % 5)];
tempChar2 = Metrix[RowX1][((ColumnY2 + 4) % 5)];
}
//如果该字母对落在矩阵同一列则由其上边的字母来代替
else if(ColumnY1 == ColumnY2) //列号相同
{
tempChar1 = Metrix[((RowX1 + 4) % 5)][ColumnY1];
tempChar2 = Metrix[((RowX2 + 4) % 5)][ColumnY1];
}
////对于其他情况的,该字母所在的行为明文所在行,
////另一字母所在列为明文所在列
else
{
tempChar1 = Metrix[RowX1][ColumnY2];
tempChar2 = Metrix[RowX2][ColumnY1];
}
StrCipherTemp = StrCipherTemp + tempChar1 + tempChar2;
m_nLastText = StrCipherTemp;
m_nLastText.MakeLower();
UpdateData(FALSE);
}
return;
}
break;
case 2: //Vigenere密码解密算法
{
///密钥字母决定行,改行里的密文字母所在列的顶部字母就是明文字母
int Length = 0; //定义密文的长度
int LengthKey = 0; //定义密钥的长度
int i = 0;
int j = 0;
int tmpValue; //定义一个零时值
CString strKey = "deceptive"; //定义密钥
strKey.MakeUpper(); //将密钥字符串全部变为大些
char tempChar; //定义明文变量
CString PlainText = ""; //定义明文字符串变量
UpdateData(TRUE);
Length = m_nFirstText.GetLength();
m_nFirstText.MakeUpper(); //将输入的字符串全部变为大些
LengthKey = strKey.GetLength();
for(i=0, j=0; i < Length; i++)
{
j = j % LengthKey;
///输入的密文是位于字符'A'与'Z'之间的
if(m_nFirstText.GetAt(i) > 64 && m_nFirstText.GetAt(i) < 91)
{
tmpValue = m_nFirstText.GetAt(i) - 65 - (strKey.GetAt(j) - 65);
if(tmpValue >= 0)
tempChar = (tmpValue % 26) + 97;
else
tempChar = (tmpValue + 26) % 26 + 97;
j++;
}
PlainText = PlainText + tempChar;
}
m_nLastText = PlainText;
UpdateData(FALSE);
break;
}
case 3: //置换密码解密
{
int Length; //密文的长度
int LengthKey; //密钥的长度
char temchar;
CString PlianText = ""; //定义明文字符串
CString strKey = "4312567";
LengthKey = strKey.GetLength();
//m_nFirstText = "CJQDKRBIPAHOELSFMTGNV";
Length = m_nFirstText.GetLength();
m_nFirstText.MakeUpper();
strKey.MakeUpper();
int num1 = Length % LengthKey;
if (num1 != 0)
{
for (int p = 0;p < LengthKey - num1; p ++)
{
m_nFirstText.Insert(Length,'X');
}
Length = m_nFirstText.GetLength();
}
int num2 = Length / LengthKey;
for (int i = 0;i < num2;i ++)
{
for (int j = 0;j < LengthKey;j ++)
{
temchar = m_nFirstText.GetAt((strKey.GetAt(j) - 49) * num2 + i);
PlianText = PlianText + temchar;
}
}
m_nLastText = PlianText;
UpdateData(FALSE);
}
break;
default:
break;
}
}
//退出程序
void CCryptographyDlg::OnExitPrograme()
{
if(MessageBox("您是否确定要退出?", "加密解密程序", MB_YESNO|MB_DEFBUTTON2) ==IDNO)
//return;
CDialog::OnClose();
}
//关于作者
void CCryptographyDlg::OnAboutAuthor()
{
MessageBox("本程序由四川师范大学计算机\n科学学院殷国云同学编写!!",NULL,MB_OK);
}
int CCryptographyDlg::GetX(char ch) //获取当前字母的行号
{
int i,j,k;
for(i = 0;i < 5;i ++)
for(j = 0;j < 5;j ++)
//同一字母小写和大写做一种类型处理
if(Metrix[i][j] == ch || Metrix[i][j] == ch - 32)
k = i;
return k;
}
int CCryptographyDlg::GetY(char ch) //获取当前字母的列号
{
int i,j,k;
for(i = 0;i < 5;i ++)
for(j = 0;j < 5;j ++)
//同一字母小写和大写做一种类型处理
if(Metrix[i][j] == ch || Metrix[i][j] == ch - 32)
k = j;
return k;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -