C++中字符串的加密解密

#include <iostream>
using namespace std;
char* enciphering(char *ch,int &key)
{
    char *str=new char[100];
    int i=0,x;
    bool decide=true;
    while(ch[i])
    {
        if(ch[i]>='a'&&ch[i]<='z')
        {
            x=ch[i]+i+key+1;
            while(decide)
            {
                if(x>'z')
                    x-=26;
                else
                    decide=false;
            }
            decide=true;
            str[i]=x;
        }
        else
            str[i]=ch[i];
        i++;
    }
    return str;
}
char* deciphering(char *ch,int &key)
{
    char *str=new char[100];
    int i=0,x;
    bool decide=true;
    while(ch[i])
    {
        if(ch[i]>='a'&&ch[i]<='z')
        {
            x=ch[i]-i-1-key;
            while(decide)
            {
                if(x<'a')
                    x+=26;
                else
                    decide=false;
            }
            decide=true;
            str[i]=x;
        }
        else
str[i]=ch[i];
        i++;
    }
    return str;
}
int main()
{
    int key,count;
    char *str=new char[100];
    go:
    cout<<"1.加密字符串/n2.解密字符串/n3.退出程序"<<endl;
    cout<<"请选择行为:";
    cin>>count;
    switch(count)
    {
        case 1:
        cout<<"输入要加密的字符串:";
        cin.get();
        cin.getline(str,100);
        cout<<"输入密钥:";
        cin>>key;
        str=enciphering(str,key);
        cout<<"加密后的字符串:"<<str<<endl;
        break;
        case 2:
        cout<<"输入要解密的字符串:";
        cin.get();
        cin.getline(str,100);
        cout<<"输入密钥:";
        cin>>key;
        str=deciphering(str,key);
        cout<<"解密后的字符串:"<<str<<endl;
        break;
        case 3:
        delete [] str;
        return 0;
        default:
        cout<<"没有这个选项,请重新选择!"<<endl;
        goto go;
    }
goto go;
}

阅读更多
上一篇Visual FoxPro数据库加密算法的研究与实现
下一篇硬盘(U盘)有价数据无价——数据恢复我也行
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭