locked
encrypt and decryption program RRS feed

  • Question

  • I been looking around and I think this is the proper place to post my question,if not i'm sorry, please don't get upset just send me to the spot where I should post this question.

    Hi Im writing a encryption/decrption program.

    I have the basics down I think.  Its using the Console Application.

    What I have so far is when you launch the Application (Start without debugging mode)

    It asks me do I want to encrypt or decrypt. 

    as show in the code below when encrypting a=x b=y c=z which basically writhes it backwards
    decrypt its vise versia where x=a y=b z=c.

    X represents on what i'm typing in.

    The problem as you may figured out is that you can only put in one character. How would I make it so it reads each character and then converts them correctly.

    Example: abc=xyz;

    I cant do(Below) because I have to do every possible thing a person can type and that impossible.

    case "abc": Console.WriteLine("xyz"); break;

    string x, chose;
    
                Console.WriteLine("encrypt/decrypt");
                chose = Console.ReadLine();
    
                if (chose == "encrypt")
                {
                    x=Console.ReadLine();
                    switch (x)
                    {
                        case "a": Console.WriteLine("x"); break;
                        case "b": Console.WriteLine("y"); break;
                        case "c": Console.WriteLine("z"); break;
                    }
                }
                if (chose == "decrypt")
                {
                    x=Console.ReadLine();
                    switch (x)
                    {
                        case "x": Console.WriteLine("a"); break;
                        case "y": Console.WriteLine("b"); break;
                        case "z": Console.WriteLine("c"); break;
                    }
                }

    Thanks

    Wednesday, July 11, 2012 2:51 AM

Answers

  • Ok this might seem like alot of code, but there is just a good bit of commenting. I wasn't sure exactly how much experiance you have with C# and object oriented programming, so I wanted to be extra careful to cover everything. Sorry if I went overboard.

    using System;
    namespace example.MyCrypter
    {
        class Program
        {
            static void Main()
            {
                //First we will need to initialize our class we built called MyCrypter. 
                //You do this almost exactly the same way you would declare and initialize a string variable.
                MyCrypter MyCrypterClassInstance = new MyCrypter();
                //This says make an object "MyCrypter" called "MyCrypterClassInstance", which equals a brand new object of type "MyCrypter" AKA: = new MyCrypter()
     
                //With that done, we can now use the method inside of my MyCrypter Class object called ProcessMessage. 
                //We can call that method and pass it a string and type of process like so:
                Console.WriteLine(MyCrypterClassInstance.ProcessMessage("ab", MyCrypter.MyCrypterProcessTypes.Encrypt));
                Console.ReadLine();
                //Notice we call it for now inside a Console.WriteLine() method, we can do that because we told our ProcessMessage() method to return a string type when run.
                //We then call Console.Readline() to make the commandprompt pause until someone pressed enter.
                //Note: in the ProcessMessage method we pass first the string to be processed, and then the type of process.
                //You may have noticed the weird syntax involved here, and that is because we coded our enum AKA option list, inside the MyCrypter class.
                //Because of that we needed to reference the whole list from MyCrypter.
            }
            
        }
        
        public class MyCrypter
        {
            
            string _processedMessage;
            //This is the method you will call initially from your main method above "static void Main()", as you see below it has 2 parameters
            //  the first 1) a parameter called "MessageToProcess" and it is a string type.
            //  the second 2) a parameter called "typeOfProcess" it is a MyCrypterProcessTypes type. This is related to the "enum" found at the bottom of this code example.
            //  The enum will allow us to give it the string we want to handle, and how we want to handle it: either "MyCrypterProcessTypes.Decrypt", or "MyCrypterProcessTypes.Encrypt"
            public string ProcessMessage(string MessageToProcess, MyCrypterProcessTypes typeOfProcess)
            {
                
                //Here we say, Ok for each Character Object inside our MessageToProcess string, do something...
                //We can do this because a string variable is simply an array of characters.
                foreach (char CharacterObject in MessageToProcess)
                {
                    if (typeOfProcess == MyCrypterProcessTypes.Encrypt) 
                    {
                        _processedMessage = _processedMessage + encrypt(CharacterObject);
                    }
                    if (typeOfProcess == MyCrypterProcessTypes.Decrypt)
                    {
                        _processedMessage = _processedMessage + decrypt(CharacterObject);
                    }
                }
                
                //We are all done looping through the message, now lets return the string.
                return _processedMessage;
            }
            //Method to encrypt a character and pass back the new character value as defined in the switch case inside this method
            private char encrypt(char CharBeingPassed)
            {
                /*Note: there is a much better way to do this part, like srithar was using in his example.
                But for simplicity lets just go with the switch case... */
                switch (CharBeingPassed)
                {
                    //return a spaced character if there is no character recognized in the case list
                    default: return ' ';
                    case 'a': return 'z';
                    case 'b': return 'y';
                    
                    //And so on...
                }
                
            }
            //Method to decrypt a character and pass back the new character value as defined in the switch case inside this method
            private char decrypt(char CharBeingPassed)
            {
                /*Note: there is a much better way to do this part, like srithar was using in his example.
                But for simplicity lets just go with the switch case... */
                switch (CharBeingPassed)
                {
                    //return a spaced character if there is no character recognized in the case list
                    default: return ' ';
                    case 'z': return 'a';
                    case 'y': return 'b';
                        
                    //And so on...
                }
            }
            //an "enum" is like making a list of available options to select from. We use this in the If statements in our ProcessMessage() method.
            public enum MyCrypterProcessTypes
            {
                Encrypt,
                Decrypt
            }
        }
    }

    let me know how that works for you, I just copied the whole file into the code block.


    May the Force be with you young Jedi...

    Console.WriteLine("-Warren");
    If you find my post helpful please click the green "up arrow" to the left and vote!
    Did my post Answer your question? Propose it as an Answer!
    I Really Appreciate it =)

    Thursday, July 12, 2012 2:20 AM

All replies

  • Hey X301!

    Well in your specific example I would create a method that returns a char, with a parameter list consisting of 1) a char to be modified and 2) what type of modification will be applied (encryption or decryption). Then utilizing that method you could loop through each character in your String x, passing each character one at a time. Towards the end of each loop you can take the returned character and add it into a new String variable forming the modified (encrypted or decryped) string.

    Have you through about utilizing a built in method for actual cryptography, such as an AES type encryption?

    If you need further help I'm happy to assist, and or if you would like to know more about the latter option check out the following link:

    C# RijndaelManaged Class

    Happy coding!

    Console.WriteLine("-Warren");

    • Proposed as answer by DarthBerg82 Wednesday, July 11, 2012 4:09 AM
    • Unproposed as answer by DarthBerg82 Thursday, July 12, 2012 2:44 AM
    Wednesday, July 11, 2012 4:06 AM
  • It can be done using a Dictionary like this
         Dictionary<string, string> encrypt = new Dictionary<string, string>();
                Dictionary<string, string> decrypt = new Dictionary<string, string>();
                string encryptString = "";
                string decryptString = "";
                encrypt.Add("a", "x");
                encrypt.Add("b", "y");
                encrypt.Add("c", "z");
                decrypt.Add("x", "a");
                decrypt.Add("y", "b");
                decrypt.Add("z", "c");
                string input = "abc";
                //Add this code to your Encrypt Case
                char[] EncryptinputArray = input.ToCharArray();
                for (int i = 0; i < EncryptinputArray.Length; i++)
                {
                    encryptString = encryptString + encrypt[EncryptinputArray[i].ToString()];  
                }
                Console.WriteLine(encryptString);
                //You will get the output xyz
    
                //Add this code to your Decrypt Case
                char[] DecryptinputArray = encryptString.ToCharArray();
                for (int i = 0; i < DecryptinputArray.Length; i++)
                {
                    decryptString = decryptString + decrypt[DecryptinputArray[i].ToString()];
                }
                Console.WriteLine(decryptString);
                //You will get output abc

    • Proposed as answer by Srithar Wednesday, July 11, 2012 6:14 AM
    • Unproposed as answer by Srithar Thursday, July 12, 2012 4:10 AM
    Wednesday, July 11, 2012 6:14 AM
  • I don't want to use a built in encryption because I want to make my own. I want to add the whole  ALPHABET when I get the basics done. I have used char before and I was thicken that could help. I also know how to use arrays a little, while loop, for loop and converting variables such as strings to doubles so DarthBerg82 I look at what you had to say and I think its a great idea.

    Can you help me a little. How would   pass each character one at a time. Would I use an array. I like your first option better because you can make your own encryption, and that is what I was aiming for.


    Thanks for the reply DarthBerg82 and Sirithar thanks for the reply.


    Wednesday, July 11, 2012 3:40 PM
  • Sure happy to help, you will want to look into using a foreach loop; this will easily pull each char in the string out. Example:

    foreach (char x in msg.ToCharArray())
                {
                }

    If you need/want I can supply the full example utilizing those concepts your more familiar with.


    Console.WriteLine("-Warren"); If you find my post helpful and or the answer to your question please Vote/Propose as Answer! I Greatly Appreciate it =)

    Wednesday, July 11, 2012 8:22 PM
  • Im getting an error for msg, is it a variable or what. Also I just upgraded to visual studio C# 2012 rc for the metro app option will this be a problem in the future.

    Error 1 The name 'msg' does not exist in the current context c:\users\nick\documents\visual studio 2012\Projects\Test\Test\Program.cs 15 32 Test

    If its not to much trouble can you just write the full example of the code and explain the parts that may be difficult to understand. I been learning from this book I bought off of ebay and It just gives me the code and then has pages explaining it. I'm used to this method and that is how I learn. Sorry if this is a problem

    Thursday, July 12, 2012 1:10 AM
  • Sure I can do that. While I write this up why don't you head on over to csharp-station.com . They have some really good tutorials for C#, and they walk you through several different examples of each element within a program.

    May the Force be with you young Jedi...

    Console.WriteLine("-Warren");
    If you find my post helpful please click the green "up arrow" to the left and vote!
    Did my post Answer your question? Propose it as an Answer!
    I Really Appreciate it =)

    Thursday, July 12, 2012 1:32 AM
  • Ok this might seem like alot of code, but there is just a good bit of commenting. I wasn't sure exactly how much experiance you have with C# and object oriented programming, so I wanted to be extra careful to cover everything. Sorry if I went overboard.

    using System;
    namespace example.MyCrypter
    {
        class Program
        {
            static void Main()
            {
                //First we will need to initialize our class we built called MyCrypter. 
                //You do this almost exactly the same way you would declare and initialize a string variable.
                MyCrypter MyCrypterClassInstance = new MyCrypter();
                //This says make an object "MyCrypter" called "MyCrypterClassInstance", which equals a brand new object of type "MyCrypter" AKA: = new MyCrypter()
     
                //With that done, we can now use the method inside of my MyCrypter Class object called ProcessMessage. 
                //We can call that method and pass it a string and type of process like so:
                Console.WriteLine(MyCrypterClassInstance.ProcessMessage("ab", MyCrypter.MyCrypterProcessTypes.Encrypt));
                Console.ReadLine();
                //Notice we call it for now inside a Console.WriteLine() method, we can do that because we told our ProcessMessage() method to return a string type when run.
                //We then call Console.Readline() to make the commandprompt pause until someone pressed enter.
                //Note: in the ProcessMessage method we pass first the string to be processed, and then the type of process.
                //You may have noticed the weird syntax involved here, and that is because we coded our enum AKA option list, inside the MyCrypter class.
                //Because of that we needed to reference the whole list from MyCrypter.
            }
            
        }
        
        public class MyCrypter
        {
            
            string _processedMessage;
            //This is the method you will call initially from your main method above "static void Main()", as you see below it has 2 parameters
            //  the first 1) a parameter called "MessageToProcess" and it is a string type.
            //  the second 2) a parameter called "typeOfProcess" it is a MyCrypterProcessTypes type. This is related to the "enum" found at the bottom of this code example.
            //  The enum will allow us to give it the string we want to handle, and how we want to handle it: either "MyCrypterProcessTypes.Decrypt", or "MyCrypterProcessTypes.Encrypt"
            public string ProcessMessage(string MessageToProcess, MyCrypterProcessTypes typeOfProcess)
            {
                
                //Here we say, Ok for each Character Object inside our MessageToProcess string, do something...
                //We can do this because a string variable is simply an array of characters.
                foreach (char CharacterObject in MessageToProcess)
                {
                    if (typeOfProcess == MyCrypterProcessTypes.Encrypt) 
                    {
                        _processedMessage = _processedMessage + encrypt(CharacterObject);
                    }
                    if (typeOfProcess == MyCrypterProcessTypes.Decrypt)
                    {
                        _processedMessage = _processedMessage + decrypt(CharacterObject);
                    }
                }
                
                //We are all done looping through the message, now lets return the string.
                return _processedMessage;
            }
            //Method to encrypt a character and pass back the new character value as defined in the switch case inside this method
            private char encrypt(char CharBeingPassed)
            {
                /*Note: there is a much better way to do this part, like srithar was using in his example.
                But for simplicity lets just go with the switch case... */
                switch (CharBeingPassed)
                {
                    //return a spaced character if there is no character recognized in the case list
                    default: return ' ';
                    case 'a': return 'z';
                    case 'b': return 'y';
                    
                    //And so on...
                }
                
            }
            //Method to decrypt a character and pass back the new character value as defined in the switch case inside this method
            private char decrypt(char CharBeingPassed)
            {
                /*Note: there is a much better way to do this part, like srithar was using in his example.
                But for simplicity lets just go with the switch case... */
                switch (CharBeingPassed)
                {
                    //return a spaced character if there is no character recognized in the case list
                    default: return ' ';
                    case 'z': return 'a';
                    case 'y': return 'b';
                        
                    //And so on...
                }
            }
            //an "enum" is like making a list of available options to select from. We use this in the If statements in our ProcessMessage() method.
            public enum MyCrypterProcessTypes
            {
                Encrypt,
                Decrypt
            }
        }
    }

    let me know how that works for you, I just copied the whole file into the code block.


    May the Force be with you young Jedi...

    Console.WriteLine("-Warren");
    If you find my post helpful please click the green "up arrow" to the left and vote!
    Did my post Answer your question? Propose it as an Answer!
    I Really Appreciate it =)

    Thursday, July 12, 2012 2:20 AM
  • Thanks Ill look over it now. I did go to that website and It was more useful then my book so far. It had a bigger list of datatypes and variable types then my book So thank you. Ill continue to use that website.

    Ill reply back when I finish looking over this code. Thanks again.

    Thursday, July 12, 2012 2:33 AM
  • Good deal! Also I just started a blog DarthBerg82's Blog, most of the stuff I'll be talking about is more advanced but I just posted a set of flash cards to go with the lessons found on that site. I have only made a set for lesson 01 thus far, but I will be working on more. Once you get the basics down you may be interested in some of the future posts I'm going to make on software development principles.

    May the Force be with you young Jedi...

    Console.WriteLine("-Warren");
    If you find my post helpful please click the green "up arrow" to the left and vote!
    Did my post Answer your question? Propose it as an Answer!
    I Really Appreciate it =)

    Thursday, July 12, 2012 2:49 AM
  • Ok It works and you did a great job explaining. If I were better then I was now and you wrote this and I knew most of this stuff I wouldn't  care because you put your time to make Shure I would understand it.

    One more thing. I tried to put in a new variable where called

    string x;

    What this variable will hold what I type in and then encrypt it. Example

    x=Console.ReadLine();

    Console.WriteLine(MyCrypterClassInstance.ProcessMessage(x, MyCrypter.MyCrypterProcessTypes.Encrypt));

    I replaced the ab with the variable so it whould just decrypt what I wrote but I got an error saying

    Error 1 Use of unassigned local variable 'x' c:\users\nick\documents\visual studio 2012\Projects\TEST\TEST\Program.cs 17 69 TEST

    How whould I make it so I input what I want to encrypt.

    Thank you so much.


     

    Thursday, July 12, 2012 2:55 AM
  • Could you copy the main() method portion of the code where you added the "string x;" and passed it? That's saying your using a variable that has no assignment.

    May the Force be with you young Jedi...

    Console.WriteLine("-Warren");
    If you find my post helpful please click the green "up arrow" to the left and vote!
    Did my post Answer your question? Propose it as an Answer!
    I Really Appreciate it =)

    Thursday, July 12, 2012 3:01 AM
  • I Got It I just tried again and it worked. I must have done something wrong. Rereading your code and looking at my comment above made me realize I must have done something wrong and I tried again.

    Thank YOU I will vote your comments and mark your second to last comment as Answer. Thanks again so much.


    Thursday, July 12, 2012 3:06 AM
  • I appreciate that! Glad you figured it out =)

    May the Force be with you young Jedi...

    Console.WriteLine("-Warren");
    If you find my post helpful please click the green "up arrow" to the left and vote!
    Did my post Answer your question? Propose it as an Answer!
    I Really Appreciate it =)

    Thursday, July 12, 2012 3:09 AM
  • Is there a way to add you to a friends list of something. It would be an honor since your part of the empire.
    Thursday, July 12, 2012 3:13 AM
  • I find your commitment to the empire most pleasing... But to be honest I'm not sure. I'm new to the whole MSDN forums system; so that is an answer i do not have =(

    I can tell if you goto my blog you can sign up to "follow" it. =)


    May the Force be with you young Jedi...

    Console.WriteLine("-Warren");
    If you find my post helpful please click the green "up arrow" to the left and vote!
    Did my post Answer your question? Propose it as an Answer!
    I Really Appreciate it =)

    Thursday, July 12, 2012 3:22 AM
  • Ok Ill do that.
    Thursday, July 12, 2012 3:23 AM
  • Ok Im following your blog.
    Thursday, July 12, 2012 3:29 AM
  • Awesome, thanks!

    May the Force be with you young Jedi...

    Console.WriteLine("-Warren");
    If you find my post helpful please click the green "up arrow" to the left and vote!
    Did my post Answer your question? Propose it as an Answer!
    I Really Appreciate it =)

    Thursday, July 12, 2012 3:31 AM
  • Hello,

    Most of the methods that are discussed here are pretty primitive and are not really related to cryptography, it might be harsh of me to say that but it's really a joke.

    I understand that you want to learn the language and that's your pet project but I don't think that it's the right way to learn this especially when you name your methods incorrectly and think that just because you get to replace characters and reverse them using ancient wisdom you can learn something.

    If you want to learn the language that's fine keep this going, it's a good practice but if you really want to learn how to encrypt and decrypt something this goes way beyond just replacing characters especially in modern cryptography.


    Regards,

    Eyal Shilony

    Thursday, July 12, 2012 3:36 AM
  • Well I don't know if I would say its a joke. Cryptography started several thousand years ago, and one of the first types was substitution. Of course its nothing that would stand up to modern security measures, but i would say a pretty good place to start for a new developer. Like you said practice is good. I'm sure if X301 sticks with it he will be diving into modern cryptography in the near future... or would that be future cryptography in the near future? =)

    You know one day the stuff we call modern cryptography will be a "joke". I can see it now... Like O.M.G! I can't believe people called 512bit encryption, encryption! I could so break that in 0.02ms with my calculator.

    Take care yall


    May the Force be with you young Jedi...

    Console.WriteLine("-Warren");
    If you find my post helpful please click the green "up arrow" to the left and vote!
    Did my post Answer your question? Propose it as an Answer!
    I Really Appreciate it =)

    Thursday, July 12, 2012 3:53 AM
  • The reason I said it's a joke is exactly this you can't encrypt something without having a key, when it comes to encryption you don't just substitute blocks from one place of the text to another you substitute it with something else that to reverse it you must have the key; otherwise, it's not an encryption but simply a basic obfuscation method. :)

    EDIT: The differences between obfuscation and encryption is exactly that in obfuscation the algorithm uses the text itself to make the output whereas in encryption the algorithm uses the key to process the output and just for clarification encoding is in short dealing with the format of the output.


    Regards,

    Eyal Shilony

    • Edited by Eyal Solnik Thursday, July 12, 2012 5:12 AM
    Thursday, July 12, 2012 4:54 AM