C++简单数据加密算法实现加密和解密的代码

工作闲暇时间,把写内容过程中较好的内容珍藏起来,下面的资料是关于C++简单数据加密算法实现加密和解密的内容,应该是对小伙伴有用。

  This is a program for Encryption and Decryption
  This program uses the Simple Data Encryption Standard (SDES) Algorithm.
  This Algo takes 8-bits of plaintext at a time and produces 8-bits of ciphertext.
  It uses 10-bits of key for Encryption and Decryption.

  Developed by : Vivek Kumar ([email protected])

   Created on : 31 March 2005
   Last Modified on : 10 April 2005

   Any sort of suggetions/comments are most welcome at [email protected]

#include
#include
#include
#include
#include
#include
void menuEn();
void menuDe();
int DoEnDe(int);

class SDES
{
private:
    char KEY[11],K1[9],K2[9],IPOutput[9],InvIPOutput[9];
    char F1Output[9],F2Output[9];
    char INPUT_BIT[9],OUTPUT_BIT[9];

public:
    unsigned char INPUT,OUTPUT;

    ~SDES();
    void GenerateKeys();
    void conv_to_bits(unsigned char );
    void DES_Encryption(unsigned char );
    void DES_Decryption(unsigned char );
    void SDES::GetChar();
};
{
    int i;
    {
        printf("nInValid Key-Length %s %d",key,strlen(key));
        getch();
        exit(1);
    }
    {
        KEY[i]=key[i];
    }
    KEY[10]='0';

}

void SDES::GenerateKeys()
{
    int i;

    for (i=0;i<10;i++)
        P10_OP[i]=KEY[P10[i]-1];

    P10_OP[10]='0';

    for (i=0;i<5;i++)
    {
        P10LEFT[i]=P10_OP[i];
        P10RIGHT[i]=P10_OP[i+5];
    }
    P10LEFT[5]='0';
    P10RIGHT[5]='0';

    pl=new char[6];
    pr=new char[6];

    pl=Left_Shift(P10LEFT,1);
    pr=Left_Shift(P10RIGHT,1);

    for (i=0;i<5;i++)
    {
        plpr[i]=pl[i];
        plpr[i+5]=pr[i];
    }
    plpr[10]='0';

    for (i=0;i<8;i++)
        K1[i]=plpr[P8[i]-1];


    pl1=Left_Shift(pl,2);
    pr1=Left_Shift(pr,2);

    for (i=0;i<5;i++)
    {
        plpr[i]=pl1[i];
        plpr[i+5]=pr1[i];
    }
    plpr[10]='0';

    for (i=0;i<8;i++)
    {
        K2[i]=plpr[P8[i]-1];
    }

}

{
    int length=strlen(bs);

    char_ptr = new char[length +1];
    str=new char[length+1];
    char_ptr=bs;

    int i,j;
    for (j=0;j=0;i--)
    {
        bit=ch%2;
        ch=ch/2;

        if (bit!=0)
            INPUT_BIT[i]='1';
        else
            INPUT_BIT[i]='0';
    }

}

{
    int IPArray[8]={2,6,3,1,4,8,5,7};
    int i;
    IPOutput[8]='0';
    for (i=0;i<8;i++)
    {
        IPOutput[i]=input[IPArray[i]-1];
    }
}

{
    int InvIPArray[8]={4,1,3,5,7,2,8,6};
    int i;
    InvIPOutput[8]='0';
    for (i=0;i<8;i++)
    {
        InvIPOutput[i]=input[InvIPArray[i]-1];
    }
}

void SDES::DES_Encryption(unsigned char input)
{
    char LIP[5],RIP[5],L1[5],R1[5];
    int i;

    INPUT=input;

    printf("nEncrpyting.........");

    for (i=0;i<4;i++)
    {
        LIP[i]=IPOutput[i];
        RIP[i]=IPOutput[i+4];
    }
    LIP[4]='0';
    RIP[4]='0';

    Function_F(LIP,RIP,1);

    for (i=0;i<4;i++)
    {
        L1[i]=F1Output[i];
        R1[i]=F1Output[4+i];
    }
    L1[4]='0';
    R1[4]='0';

    Function_F(R1,L1,2);


    GetChar();
}


  Here IP, InvIP, E/P, SBOX1 and SBOX2 are same
  But Function_F first operats on sub-key K2 and
void SDES::DES_Decryption(unsigned char input)
{
    char LIP[5],RIP[5],L1[5],R1[5];
    int i;

    INPUT=input;
    conv_to_bits(INPUT);

    printf("nDecrpyting.........");

    for (i=0;i<4;i++)
    {
        LIP[i]=IPOutput[i];
        RIP[i]=IPOutput[i+4];
    }
    LIP[4]='0';
    RIP[4]='0';

    Function_F(LIP,RIP,2);

    for (i=0;i<4;i++)
    {
        L1[i]=F1Output[i];
        R1[i]=F1Output[4+i];
    }
    L1[4]='0';
    R1[4]='0';

    Function_F(R1,L1,1);
    InvIP(F1Output);
    GetChar();
}

{
    int i;
    char SBOX_Output[5];
    char P4_Output[5];
    char fk_Output[5];
    char Main_Output[9];

    for (i=0;i<8;i++)
    {
        E_POutput[i]=rinput[E_P[i]-1];
    }
    E_POutput[8]='0';

    EXOR_Output=EX_OR(E_POutput,key);

    LEXOR=new char[strlen(EXOR_Output)/2+1];
    REXOR=new char[strlen(EXOR_Output)/2+1];

    for (i=0;i8)
    {
        printf("nWRONG LENGTH STRING");
        exit(0);
    }
    for (i=0;i<8;i++)
    {
        if (bs[i]=='1')
        {
            in=1;
            for (j=1;j<8-i;j++)
            {
            }
            ch=ch+in;
        }
    }
    OUTPUT=ch;
}

SDES::~SDES()
{
}


void main(void)
{
    unsigned char ch,ch1;
    int i,n=10,choice;

    while (1)
    {
        key = new char[11];
        sfname = new char[20];
        tfname = new char[20];
        mainmenu(&choice);
        fflush(stdin);
        switch (choice)
        {
        case 1:
            menuEn();
            DoEnDe(choice);
            break;
        case 2:
            menuDe();
            DoEnDe(choice);
            break;
        case 3:
            exit(0);
        default:
            printf("nWrong Choice Enter againnPress any key to return to Main Menu..");
            getch();
            break;
        }
    }
}

{
    printf("nWhat do you want to do..");
    printf("n1. Encryption");
    printf("n2. Decryption");
    printf("n3. Exit");
    printf("nnEnter the choice? ");
    scanf("%d",c);
}
void menuEn()
{
    sfname=new char[20];
    tfname=new char[20];
    key=new char[11];
    printf("nEncryption Menunn");
    printf("nEnter the filename to be Encrypted: ");
    gets(sfname);
    printf("nEnter the Target file name: ");
    gets(tfname);
    printf("nEnter the 10-bits KEY: ");
    gets(key);
    printf("nnNotedown this key, as same key is used for Decryption");
}

void menuDe()
{
    sfname=new char[20];
    tfname=new char[20];
    key=new char[11];
    printf("nDecryption Menunn");
    printf("nEnter the filename to be Decrypted: ");
    gets(sfname);
    printf("nEnter the Target file name: ");
    gets(tfname);
    printf("nEnter the 10-bits KEY: ");
    gets(key);
}

int DoEnDe(int c)
{
    SDES S(key);
    int i,n;
    unsigned char ch;
    fp=fopen(tfname,"w");
    ft=fopen(sfname,"r");
    if (fp==NULL)
    {
        printf("nTarget File not opened SORRY");
        getch();
        fclose(fp);
        return(0);
    }
    if (ft==NULL)
    {
        printf("nSource File not opened SORRY");
        getch();
        fclose(ft);
        return(0);
    }
    while (fread(&ch,1,1,ft)==1)
    {
        S.OUTPUT=ch;

        for (i=0;i

你可能感兴趣的:(C++简单数据加密算法实现加密和解密的代码)