WinSocket Programming notes (c)

Achieve two .Kerberos agreement

I put each part of the code apart to write:

1. The random session key generated

#include<iostream>
#include<stdlib.h>
using namespace std;

int main()
{   srand((unsigned)time(NULL));
    int key=(rand()%9000+1000);
    cout << key << endl;
    return 0;
}

 

2. Encryption Algorithm

#include <iostream>
#include <cstring>
using namespace std;

int main()
{   string plain="ada54ff89m";
    string cipher="";
    int key=89696;
    int offset = key % 26;
    for(int i = 0; i < plain.length(); i++)
    {   char c = plain[i];
        if(isalpha(c))
        {
            if(c >= 'a' && c <= 'z')
            {
                c = 'a' + ((c - 'a' + offset) % 26);
                cipher += c;
        }
        else
        {
                c = 'A' + ((c - 'A' + offset) % 26);
                cipher += c;
        }}

        else if(isdigit(c))
            {
                c = '0' + ((c - '0' + offset) % 10);
                cipher += c;
            }
            else
            {
                cipher += c;
            }
    }
    cout<<cipher<<endl;
    }

3. decryption algorithm

#include <iostream>
#include<cstring>
using namespace std;

int main()
{   int key=89659;
    string plain ="";
    string cipher="wsw0lmm";
    int offset = key % 26;
    for(int i = 0; i < cipher.length(); i++)
    {
        char c = cipher[i];
        if(isalpha(c))
        {
            if(c >= 'a' && c <= 'z')
            {
                c = 'z' - (('z' - c + offset) % 26);
                plain += c;
            }
            else
            {
                c = 'Z' - (('Z' - c + offset) % 26);
                plain += c;
            }
        }
        else if(isdigit(c))
            {
                c = '9' - (('9' - c + offset) % 10);
                plain += c;
            }
            else
            {
                plain += c;
            }
    }
    cout<<plain<<endl;
}

4.map reading the content property (corresponding to the master key)

#include <fstream>
#include <string>
#include <iostream>
#include<map>
using namespace std;

int main()
{
    map<string,int> userHash;
    userHash["ALICE"]=12345;
    userHash["BOB"]=23456;
    userHash["AS"]=34567;
    userHash["TGS"]=45678;
    int TGSkey = userHash.find("TGS")->second;
    cout<<TGSkey<<endl;
    return 0;
}

Before then is the combination of server code, modify the merger between the good times have to do type conversion, or an error occurs

Server-side the main code:

    IF (A == * * Secret) // storage spatially different positions, not directly comparable 
    {COUT << " the verification is successful " << endl;
         // the AS ALICE master key encrypted random session key and user name
         // int turn string , KSkey -> KKSkey 
        char T [ 256 ];
         String KKSkey; 
        sprintf (T, " % D " , KSkey); 
        KKSkey = T; 
        COUT << " KSkey: " << KKSkey << endl;
         String Plain = name + KKSkey; 
        cout << "Plain text:"<<plain<<endl;
        string cipher="";
        int offset = 34567 % 26;
        for(int i = 0; i < plain.length(); i++)
        {   char c = plain[i];
            if(isalpha(c))
            {
                if(c >= 'a' && c <= 'z')
                {
                    c = 'a' + ((c - 'a' + offset) % 26);
                    cipher += c;
            }
            else
            {
                    c = 'A' + ((c - 'A' + offset) % 26);
                    cipher += c;
            }}

            else if(isdigit(c))
                {
                    c = '0' + ((c - '0' + offset) % 10);
                    cipher += c;
                }
                else
                {
                    cipher += c;
                }
        }
        //string转char,cipher -> p
        char p[100];
        int i;
        for( i=0;i<cipher.length();i++)
        {
            p[i] = cipher[i];
        }
        p[i] = '\0';
        cout<<"密文TGT:"<<p<<endl;
         // ALICE master key encrypted random session key 
        String plain2 = KKSkey;
         String cipherr = "" ;
         int OFFSET2 = 12345 % 26 is ;
         for ( int I = 0 ; I <plain2.length (); I ++ ) 
        {    char = C2 plain2 [I];
             IF (the isalpha (C2)) 
            { 
                IF (C2> = ' A ' && C2 <= ' Z ' ) 
                { 
                    C2= 'a' + ((c2 - 'a' + offset2) % 26);
                    cipherr += c2;
            }
            else
            {
                    c2 = 'A' + ((c2 - 'A' + offset2) % 26);
                    cipherr += c2;
            }}

            else if(isdigit(c2))
                {
                    c2 = '0' + ((c2 - '0' + offset2) % 10);
                    cipherr += c2;
                }
                else
                {
                    cipherr += c2;
                }
        }
        //string转char,cipherr -> p2
        char p2[100];
        int i2;
        for( i2=0;i2<cipherr.length();i2++)
        {
            p2[i2] = cipherr[i2];
        }
        p2[i2] = '\0'; 
        COUT << " encrypted master key ALICE KSkey: " << P2 << endl;
         // ALICE master key to encrypt the TGT 
        String plain3 = cipher;
         String cipherrr = "" ;
         int OFFSET3 = 12345 % 26 is ;
         for ( int = I3 0 ; I3 <plain3.length (); I3 ++ ) 
        {    char C3 = plain3 [I3];
             IF (the isalpha (C3)) 
            { 
                IF (C3> = ' A ' && c3 <= 'z')
                {
                    c3 = 'a' + ((c3 - 'a' + offset3) % 26);
                    cipherrr += c3;
            }
            else
            {
                    c3 = 'A' + ((c3 - 'A' + offset3) % 26);
                    cipherrr += c3;
            }}

            else if(isdigit(c3))
                {
                    c3 = '0' + ((c3 - '0' + offset3) % 10);
                    cipherrr += c3;
                }
                else
                {
                    cipherrr += c3;
                }
        }
        //string转char,cipherrr -> p3
        char p3[100];
        int i4;
        for( i4=0;i4<cipherrr.length();i4++)
        {
            p3[i4] =cipherrr [I4]; 
        } 
        P3 [I4] = ' \ 0 ' ; 
        COUT << " after the TGT master key encryption ALICE: " << P3 << endl; 
    }

Substantially no change in the client code, here is not labeled

 

Operating results (because it is not the same run shots, so the results are not the same):

Service-Terminal:

Client:

Receive information:

 

(to be continued)

 

Guess you like

Origin www.cnblogs.com/echoDetected/p/12666945.html