c++字符串简单加密
View Code
#include "stdafx.h"
#include<iostream>
#include<ctime>
using namespace std;
void Makecode(char *pstr,int *pkey);
void Cutecode(char *pstr,int *pkey);
int _tmain(int argc, _TCHAR* argv[])
{
int key[]={1,2,3,4,5};//加密字符
char s[]="www.xiaozhuanggushi.com";
char *p=s;
cout<<"加密前:"<<p<<endl;
Makecode(s,key);//加密
cout<<"加密后:"<<p<<endl;
Cutecode(s,key);//解密
cout<<"解密后:"<<p<<endl;
int c;
cin>>c;
return 0;
}
//单个字符异或运算
char MakecodeChar(char c,int key){
return c=c^key;
}
//单个字符解密
char CutcodeChar(char c,int key){
return c^key;
}
//加密
void Makecode(char *pstr,int *pkey){
int len=strlen(pstr);//获取长度
for(int i=0;i<len;i++)
*(pstr+i)=MakecodeChar(*(pstr+i),pkey[i%5]);
}
//解密
void Cutecode(char *pstr,int *pkey){
int len=strlen(pstr);
for(int i=0;i<len;i++)
*(pstr+i)=CutcodeChar(*(pstr+i),pkey[i%5]);
}
#include<iostream>
#include<ctime>
using namespace std;
void Makecode(char *pstr,int *pkey);
void Cutecode(char *pstr,int *pkey);
int _tmain(int argc, _TCHAR* argv[])
{
int key[]={1,2,3,4,5};//加密字符
char s[]="www.xiaozhuanggushi.com";
char *p=s;
cout<<"加密前:"<<p<<endl;
Makecode(s,key);//加密
cout<<"加密后:"<<p<<endl;
Cutecode(s,key);//解密
cout<<"解密后:"<<p<<endl;
int c;
cin>>c;
return 0;
}
//单个字符异或运算
char MakecodeChar(char c,int key){
return c=c^key;
}
//单个字符解密
char CutcodeChar(char c,int key){
return c^key;
}
//加密
void Makecode(char *pstr,int *pkey){
int len=strlen(pstr);//获取长度
for(int i=0;i<len;i++)
*(pstr+i)=MakecodeChar(*(pstr+i),pkey[i%5]);
}
//解密
void Cutecode(char *pstr,int *pkey){
int len=strlen(pstr);
for(int i=0;i<len;i++)
*(pstr+i)=CutcodeChar(*(pstr+i),pkey[i%5]);
}
个人签名:天行健,君子以自强不息