| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
 跟大牛学C++学算法数据结构

问题点数：0  回复次数：2

P.S.在下对密码学一点都不了解,所以这次就功利一点,只要C语言加密算法源代码,写程序时直接拿来就用吧
(*￣︶￣)y
P.P.S.加密算法不用太高级,只要有一定安全性即可,或者各位启发一下我,给个思路呗(涉及到C语言的位操作就算了,我当初把这部分跳过了)
得分:0

#include<iostream>
using namespace std;
{
cout << "  1、凯撒加密\n";
cout << "  2、凯撒解密\n";
cout << "  3、换字式加密\n";
cout << "  4、换字式解密\n";
cout << "  5、多表替代加密\n";
cout << "  6、多表替代解密\n";
cout << "\n   请选择： ";
}
void kaisajia()
{
cin.get();
char mi[100];
int n;
cout << "请输入明文：";
gets_s(mi);
cout << "需要移动的位数：";
cin >> n;
for (int i = 0; i < strlen(mi); i++)
for (int j = 0; j < n; j++)
{
if ((mi[i] >= 'a'&&mi[i] <= 'z') || (mi[i] >= 'A'&&mi[i] <= 'Z'))
{
if (mi[i] == 'z' || mi[i] == 'Z')
mi[i] -= 26;
mi[i]++;
}

}
cout << "得到的密文为：" << mi << endl;
cin.get();
cin.get();
system("cls");
}
void kaisajie()
{
cin.get();
char mi[100];
cout << "请输入密文：";
gets_s(mi);
for (int j = 0; j < 26; j++)
{
for (int i = 0; i < strlen(mi); i++)
{
if ((mi[i] >= 'a'&&mi[i] <= 'z') || (mi[i] >= 'A'&&mi[i] <= 'Z'))
{
if (mi[i] == 'z' || mi[i] == 'Z')
mi[i] -= 26;
mi[i]++;
}
}
cout << "明文组合" << j + 1 << ""<<mi<<endl;
}
cin.get();
system("cls");
}
void huanzijia()
{
char a[28]{ 'd','c','f', 'e', 'h', 'a', 'b', 'o',
'r', 'g', 't', 'w', 's', 'i','x','z',
'j','l', 'n', 'm', 'u', 'y', 'k','q','v','p' };
char b[28]{ 'P','X','Q', 'K', 'Y', 'U', 'M', 'N',
'L', 'J', 'Z', 'X', 'I', 'S','W','T',
'G','R', 'O', 'B', 'Q', 'H', 'E','F','C','D' };
cin.get();
char mi[100];
cout << "请输入明文：";
gets_s(mi);
for (int i = 0; i < strlen(mi); i++)
{
if (mi[i] >= 'a'&&mi[i] <= 'z')
mi[i] = a[(int)mi[i]-97];
if (mi[i] >= 'A'&&mi[i] <= 'Z')
mi[i] = a[(int)mi[i] - 65];
}
cout << "密文为："<<mi << endl;
cin.get();
system("cls");
}
void huanzijie()
{
char a[28]{ 'd','c','f', 'e', 'h', 'a', 'b', 'o',
'r', 'g', 't', 'w', 's', 'i','x','z',
'j','l', 'n', 'm', 'u', 'y', 'k','q','v','p' };
char b[28]{ 'P','X','Q', 'K', 'Y', 'U', 'M', 'N',
'L', 'J', 'Z', 'X', 'I', 'S','W','T',
'G','R', 'O', 'B', 'Q', 'H', 'E','F','C','D' };
cin.get();
char mi[100];
cout << "请输入密文：";
gets_s(mi);
for (int i = 0; i < strlen(mi); i++)
{
if (mi[i] >= 'a'&&mi[i] <= 'z')
{
int j = 0;
while (mi[i] != a[j])
j++;
mi[i] = (char)(j + 97);
}
if (mi[i] >= 'A'&&mi[i] <= 'Z')
{
int j = 0;
while (mi[i] != a[j])
j++;
mi[i] = (char)(j + 65);
}
}
cout << "明文为：" << mi << endl;
cin.get();
system("cls");
}
void duobiaojia()
{
cin.get();
char mi[100];
int n,a,flag=0,ow=0;
cout << "请输入明文：";
gets_s(mi);
cout << "请问需要多少字符一组：";
cin >> n;
if (strlen(mi) % n)
flag++;
for (int i = 0; i <strlen(mi)/n+flag; i++)
{
cout << "" << i+1 << "组移动字符数：";
cin >> a;
for (int j = 0; j < n; j++)
{
if ((mi[j+i*n] >= 'a'&&mi[j + i * n] <= 'z') || (mi[j + i * n] >= 'A'&&mi[j + i * n] <= 'Z'))
{
for (int k = 0; k < a; k++)
{
if (mi[j + i * n] == 'z' || mi[j + i * n] == 'Z')
mi[j + i * n] -= 26;
mi[j + i * n]++;
}
}
}
}
cout << "密文为：" << mi << endl;
cin.get();
cin.get();
system("cls");
}
void duobiaojie()
{
cin.get();
char mi[100],ow[100];
cout << "请输入密文：";
gets_s(mi);
strcpy_s(ow, mi);
int flag = 0;
for (int n = 1; n < 10; n++) //假设的一组的个数
{
if (strlen(mi) > n)
{
if (strlen(mi) % n)
flag++;
for (int k = 1; k <26; k++)//假设的步长
{
for (int j = 0; j < n; j++) //一组的个数
{
for (int i = 0; i < strlen(mi) / n + flag; i++) //分组数
if ((mi[j + i * n] >= 'a'&&mi[j + i * n] <= 'z') || (mi[j + i * n] >= 'A'&&mi[j + i * n] <= 'Z'))
{
if (mi[j + i * n] == 'z' || mi[j + i * n] == 'Z')
mi[j + i * n] -= 26;
mi[j + i * n]++;
cout <<"分组-" << n<<"步长"<<26-k << " 明文：" << mi << endl;
}

}

}

}
strcpy_s(mi, ow);
}
cin.get();
system("cls");
}
void main()
{
while (true)
{
int n;
cin >> n;
system("cls");
switch (n)
{
case 1:
kaisajia();
break;
case 2:
kaisajie();
break;
case 3:
huanzijia();
break;
case 4:
huanzijie();
break;
case 5:
duobiaojia();
break;
case 6:
duobiaojie();
break;
default:
break;
}
}
system("pause");
}
得分:0

• 3
• 1/1页
• 1