顺移加密
来源:互联网 发布:手机淘宝抢购软件 编辑:程序博客网 时间:2024/04/29 00:01
#include <iostream>#define CRYPT_OK 1#define CRYPT_ERROR 0#define PUSH_BACK 2using namespace std;const int a = int('a');const int z = int('z');const int A = int('A');const int Z = int('Z');string str;string cryptstr ;string decryptstr ;int init(string strr){ str =strr; cryptstr = str; decryptstr =str;}int encrypt(){ int index=0; int push_back = PUSH_BACK; while(str[index]) { if(int(str[index])<=z&&int(str[index])>=a) { if(int(str[index])+push_back>z) cryptstr[index] = char(int(str[index])-(z-a)+push_back); else cryptstr[index] = char(int(str[index])+push_back); } else if(int(str[index])<=Z&&int(str[index])>=A) { if(int(str[index])+push_back>Z) cryptstr[index] = char(int(str[index])-(Z-A)+push_back); else cryptstr[index] = char(int(str[index])+push_back); } else cryptstr[index] =str[index]; index++; } return 1;}int decrypt(){ int index=0; int push_back = PUSH_BACK; while(cryptstr[index]) { if(int(cryptstr[index])<=z&&int(cryptstr[index])>=a) { if(int(cryptstr[index])-push_back<a) decryptstr[index] = char(int(cryptstr[index])+(z-a)-push_back); else decryptstr[index] = char(int(cryptstr[index])-push_back); } else if(int(cryptstr[index])<=Z&&int(cryptstr[index])>=A) { if(int(cryptstr[index])-push_back<A) decryptstr[index] = char(int(cryptstr[index])+(Z-A)-push_back); else decryptstr[index] = char(int(cryptstr[index])-push_back); } else decryptstr[index] =cryptstr[index]; index++; } return 1;}int main(){ init("Hello world"); cout<<"加密移位:"<<PUSH_BACK<<endl; cout << "原字符串:" << str<<endl; if(encrypt()) { cout<<"加密成功"<<endl; } cout << "加密后字符串:" << cryptstr<<endl; if(decrypt()) { cout<<"解密成功"<<endl; } cout << "解密后字符串:" << decryptstr<<endl; return 0;}