Thread: Confusion (Encryption of Strings)

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    Confusion (Encryption of Strings)

    Hey all.

    I was gonna write an encryption program, but 'real' encryption algorithms seem really complex. So I thought I'd write one. I had this idea for a simple encryption program, (I'm not sure if it is a good idea, but it is an idea non the less,) where a lookup string is used to locate the characters in the origional string, and uses the number of the character in the lookup to find characters in randomly selected, what I've called, crypt strings. A symbol unique to each crypt string is placed after each character so the crypt string used can be identified later when decrypting. I think you'll get it if I've explained it correctly.

    (At the moment, it is just cycling through the crypt strings instead of randomly selecting them. I'll implement this after.)

    Code:
    #include <iostream>
    #include <string>
    #include <conio.h>
    #include <windows.h>
    
    using namespace std;
    
    int KPR = 0;
    char TST;
    
    string String_to_be_encrypted;
    string Encrypted_string;
    
    bool Encrypt_String (string String_Origional, string String_Final)
    {
        string Lookup = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-=[];'#,./ !£$%^&*()_+{}:@~|<>\?";
        
        string Crypt1 = "zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA?><|~@:}{+_)(*&^%$£! /.,#';][=-098765432\1";
        string Crypt2 = "EJOTYDINSXCHMRWBGLQVAFKPUZejotydinsxchmrwbglqvafkpuz /.,#\';][=-0987654321?><|~@:}{+_)(*&^%$£!";
        string Crypt3 = "?><|~@:}{+_)(*&^%$£! /.,#\';][=-0987654321ejotydinsxchmrwbglqvafkpuzEJOTYDINSXCHMRWBGLQVAFKPUZ";
        string Crypt4 = "1234567890?><|~@:}{+_)(*&^%$£!-=[]'#;,./ \ejotydinsxchmrwbglqvafkpuzZYXWVUTSRQPONMLKJIHGFEDCBA";
        string Crypt5 = "1029384756aZbYcXdWeVfUgThSiRjQkPlOmNnMoLpKqJrIsHtGuFvEwDxCyBzA!£$%^&*()_+{}:@~<>?|/.,#';][=- Ç";
        
        char Temp = '"';
        
        Lookup += Temp;
        
        Crypt1 += Temp;
        Crypt2 += Temp;
        Crypt3 += Temp;
        Crypt4 += Temp;
        Crypt5 += Temp;
        
        int Origional_Size = String_Origional.size();
        int Final_Size = 0;
        
        int Designated_Size = Origional_Size*2;
        
        ////////////////////////////////////////////////////////////////////////////
        // Encryption variables
        ////////////////////////////////////////////////////////////////////////////
        
        int OChar_Number = 1;
        int Char_Number = 0;
        int Crypt_Number = 1;
        
        char Searching_Char;
        char Replacement_Char;
        
        while (Final_Size != Designated_Size)
        {
            while ( Lookup[Char_Number] != String_Origional[OChar_Number] )
            {
                Char_Number++;
            }    
            
            switch (Crypt_Number)
            {
                case 1:
                    Replacement_Char = Crypt1[Char_Number];
                    String_Final += Replacement_Char;
                    String_Final += '¥';
                    Final_Size++;
                    break;
                    
                case 2:
                    Replacement_Char = Crypt2[Char_Number];
                    String_Final += Replacement_Char;
                    String_Final += 'ê';
                    Final_Size++;
                    break;
                    
                case 3:
                    Replacement_Char = Crypt3[Char_Number];
                    String_Final += Replacement_Char;
                    String_Final += 'ñ';
                    Final_Size++;
                    break;
                    
                case 4:
                    Replacement_Char = Crypt4[Char_Number];
                    String_Final += Replacement_Char;
                    String_Final += 'æ';
                    Final_Size++;
                    break;
                    
                case 5:
                    Replacement_Char = Crypt5[Char_Number];
                    String_Final += Replacement_Char;
                    String_Final += 'µ';
                    Final_Size++;
                    break;
                    
                case 6:
                    Crypt_Number = 1;
                    break;
                    
            }
            
            Replacement_Char = '\0';
            OChar_Number++;
        }    
        
        Encrypted_string = String_Final;
        return true;
    }    
    
    ////////////////////////////////////////////////////////////////////////////////
    // Symbol list                                                       [Alt Value]
    ////////////////////////////////////////////////////////////////////////////////
    
    // 1 = ¥                                                                   95645
    // 2 = ê                                                                 9514632
    // 3 = ñ                                                                    2468
    // 4 = æ                                                                 2222225
    // 5 = µ                                                               397152486
    
    ////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    
    int main()
    {
        char Selection;
        
        cout<<"\n\n\n    Welcome to Cypher.exe\n\n\n    This program is an algorithm";
        cout<<"prototype, which both encrypts and decrypts\n    user input using it's own unique algorithm.";
        cout<<"\n\n\n    What do you wish to do?\n\n\n    1. Encrypt a string";
        cout<<"\n    2. Decrypt a string\n\n\n    Number of desired selection:    ";
        Selection = getch();
        
        while (Selection != '1' && Selection != '2')
        {
            Selection = getch();
        }    
        
        cout<<Selection;
        Sleep(2000);
        
        switch (Selection)
        {
            case '1':
                
                while (KPR == 0)
                {
                    String_to_be_encrypted.erase();
                    Encrypted_string.erase();
                    
                    system("cls");
                
                    cout<<"\n\n\n    Enter a string to be encrypted:    ";
                    getline (cin, String_to_be_encrypted);
                
                    Encrypt_String (String_to_be_encrypted, Encrypted_string);
                    
                    cout<<"\n\n    Cypher text:    ";
                    
                    cout<<Encrypted_string;
                    
                    cout<<"\n\n\n    Press any key to continue...";
                    
                    char Key = getch();
                    
                }    
                
                break;
                
            case '2':
                
                //code
                
                break;
                
        }       
    }
    Anyways it's all going horribly wrong. I really don't know what's up with this. It simply dosen't work. It does compile, but every second character is ñ, (but with a capital n,) and the first is definately not what it is supposed to be.

    If anyone can tell me what is wrong or can otherwise help, please do! This is really annoying me.

    Thanks for any replies!
    Necrofear
    Last edited by Necrofear; 05-03-2007 at 01:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings confusion, =/.
    By Warrax in forum C++ Programming
    Replies: 7
    Last Post: 05-15-2007, 12:48 PM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Help with Simple encryption using c-style strings
    By sdevil in forum C++ Programming
    Replies: 1
    Last Post: 02-20-2006, 11:48 PM
  5. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM