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]);
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM