求大佬看看我这个C++程序哪里有问题,是一个简单加密和解密的
求大佬看看我这个C++程序哪里有问题,是一个简单加密和解密的编译没问题,但是一运行就出事,好像是data损坏之类的#include<iostream>#include<s...
求大佬看看我这个C++程序哪里有问题,是一个简单加密和解密的编译没问题,但是一运行就出事,好像是data损坏之类的#include<iostream>
#include<string>
using namespace std;
int s = 8, c;int b[100] = { 0 };
void secret(char data[]) { for (int a = 0; a < strlen(data); a++);{
data[a] += s;
c = data[a]/128;
b[a] = c;
data[a] %= 128;
}
}
void desecret(char data[]) {
for (int a = 0; a < strlen(data); a++) {
data[a] += b[a]* 128 - s;
}
}
int main() {
string x; char data[] = { 0 };
cout << "请输入你想加密的文本:";
getline(cin, x);
for (int i = 0; i < x.size(); i++) {
data[i] = x[i];
}
secret(data);
cout << data<<endl;
desecret(data);
cout << data<<endl;
return 0;
} 展开
#include<string>
using namespace std;
int s = 8, c;int b[100] = { 0 };
void secret(char data[]) { for (int a = 0; a < strlen(data); a++);{
data[a] += s;
c = data[a]/128;
b[a] = c;
data[a] %= 128;
}
}
void desecret(char data[]) {
for (int a = 0; a < strlen(data); a++) {
data[a] += b[a]* 128 - s;
}
}
int main() {
string x; char data[] = { 0 };
cout << "请输入你想加密的文本:";
getline(cin, x);
for (int i = 0; i < x.size(); i++) {
data[i] = x[i];
}
secret(data);
cout << data<<endl;
desecret(data);
cout << data<<endl;
return 0;
} 展开
7个回答
展开全部
程序的问题:secret函数中for循环的括号后加上了分号,导致for循环没有执行循环体,而且会导致循环体中的a变量未定义,编译不通过。
解决方案:去掉secret中for循环括号后的分号。
解决方案:去掉secret中for循环括号后的分号。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//已为你改好
#include<iostream>
#include<cstring>
using namespace std;
int s = 8, c;
int b[1024] = { 0 };
char data[1024] = { 0 };
void secret(char data[])
{
for (int a = 0; a < strlen(data); a++)
{
c=data[a]+s;
b[a]=c/128;
data[a]=c%128;
//data[a] += s;
//c = data[a]/128;
//b[a] = c;
//data[a] %= 128;
}
}
void desecret(char data[])
{
for (int a = 0; a < strlen(data); a++)
{
//data[a]=b[a]*128+data[a]-s;
data[a] += b[a]* 128 - s;
}
}
int main()
{
string x;
//char data[] = { 0 };
cout << "请输入你想加密的文本:";
getline(cin, x);
for (int i = 0; i < x.size(); i++)
{
data[i] = x[i];
}
secret(data);
cout << data<<endl;
desecret(data);
cout << data<<endl;
//标记
cout<<endl<<endl<<endl;
char mymy[8]={37,28,14,6,29,8,42,25};
char mymystr[]={-21,-50,-69,-62,-52,-13,-19,-14,
-25,-9,93,86,43,61,18,77,17,-65,-94,-78,
-17,-73,-128,-87,-4,-86,-58,-48,-73,-67,
-22,120,85,108,-60,-30,-43,-29,-8,-94,
-22,-34,-66,-49,-66,-87,'\0'};
int xi=0;
for(xi=0;mymystr[xi];xi++)
mymystr[xi]^=mymy[xi%8];
cout<<mymystr<<endl;
cout<<endl<<endl<<endl;
//标记
return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
int s = 8, c;
int b[1024] = { 0 };
char data[1024] = { 0 };
void secret(char data[])
{
for (int a = 0; a < strlen(data); a++)
{
c=data[a]+s;
b[a]=c/128;
data[a]=c%128;
//data[a] += s;
//c = data[a]/128;
//b[a] = c;
//data[a] %= 128;
}
}
void desecret(char data[])
{
for (int a = 0; a < strlen(data); a++)
{
//data[a]=b[a]*128+data[a]-s;
data[a] += b[a]* 128 - s;
}
}
int main()
{
string x;
//char data[] = { 0 };
cout << "请输入你想加密的文本:";
getline(cin, x);
for (int i = 0; i < x.size(); i++)
{
data[i] = x[i];
}
secret(data);
cout << data<<endl;
desecret(data);
cout << data<<endl;
//标记
cout<<endl<<endl<<endl;
char mymy[8]={37,28,14,6,29,8,42,25};
char mymystr[]={-21,-50,-69,-62,-52,-13,-19,-14,
-25,-9,93,86,43,61,18,77,17,-65,-94,-78,
-17,-73,-128,-87,-4,-86,-58,-48,-73,-67,
-22,120,85,108,-60,-30,-43,-29,-8,-94,
-22,-34,-66,-49,-66,-87,'\0'};
int xi=0;
for(xi=0;mymystr[xi];xi++)
mymystr[xi]^=mymy[xi%8];
cout<<mymystr<<endl;
cout<<endl<<endl<<endl;
//标记
return 0;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
c是char类型变量除以128后得到的整型,只可能是0和1,按你的方法加密解密后会失真
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不知道
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询