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