如何加密C++ string字符串?
2个回答
展开全部
#include
#define MAX 100//字符串的最大长度
using namespace std;
void encrypter ( char *, int );
void decrypter (char *, int);
int main()
{
char string[MAX] = "\0";
int key;
/* Encrypter */
cout<<"**** ENCRIPTER ****"<<endl;
cout<<"Insert the secret sentence: ";
gets(string);//读取字符串
cout<<"Insert the incripting code : ";
cin>>key;//读取加密码
encrypter ( string, key );//加密
cout<<"The incrypted sentence is: "<<string<<endl<<endl;
/* Decrypter */
cout<<"**** DECRYPTER ****"<<endl;
cout<<"Insert the code: ";
cin>>key;//输入加密码
decrypter( string, key );//解密
cout<<"The decrypted sentence is: "<<string<<endl<<endl;
return 0;
}
/* 加密函数 */
void encrypter ( char *pstr, int key )
{
while ( *pstr != '\0' )
{
*pstr += key;
pstr++;
}
}
/* 解密函数 */
void decrypter ( char *pstr, int key )
{
while ( *pstr != '\0' )
{
*pstr -= key;
pstr++;
}
}
#define MAX 100//字符串的最大长度
using namespace std;
void encrypter ( char *, int );
void decrypter (char *, int);
int main()
{
char string[MAX] = "\0";
int key;
/* Encrypter */
cout<<"**** ENCRIPTER ****"<<endl;
cout<<"Insert the secret sentence: ";
gets(string);//读取字符串
cout<<"Insert the incripting code : ";
cin>>key;//读取加密码
encrypter ( string, key );//加密
cout<<"The incrypted sentence is: "<<string<<endl<<endl;
/* Decrypter */
cout<<"**** DECRYPTER ****"<<endl;
cout<<"Insert the code: ";
cin>>key;//输入加密码
decrypter( string, key );//解密
cout<<"The decrypted sentence is: "<<string<<endl<<endl;
return 0;
}
/* 加密函数 */
void encrypter ( char *pstr, int key )
{
while ( *pstr != '\0' )
{
*pstr += key;
pstr++;
}
}
/* 解密函数 */
void decrypter ( char *pstr, int key )
{
while ( *pstr != '\0' )
{
*pstr -= key;
pstr++;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
字符串是可以如此加密的,当然在文件里加密字符串还是一样道理的
#include <cstring>
#include <string>
using namespace std;
void main()
{
string str;
char ch;
int i;
cin >> str;
for (i = 0; i != str.length(); ++i)
{
ch = str.at(i);
if (ch >= 'A' && ch <= 'Y')
{
ch += 1;
str.insert(i, 1, ch);
str.erase(i + 1, 1);
}
else if (ch == 'Z')
{
ch -= 25;
str.insert(i, 1, ch);
str.erase(i + 1, 1);
}
}
cout << str << endl;
}
#include <cstring>
#include <string>
using namespace std;
void main()
{
string str;
char ch;
int i;
cin >> str;
for (i = 0; i != str.length(); ++i)
{
ch = str.at(i);
if (ch >= 'A' && ch <= 'Y')
{
ch += 1;
str.insert(i, 1, ch);
str.erase(i + 1, 1);
}
else if (ch == 'Z')
{
ch -= 25;
str.insert(i, 1, ch);
str.erase(i + 1, 1);
}
}
cout << str << endl;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询