Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
How do i decrypt without having special character in it. I'm only able to do it with encryption but i don't know how to do it with decrpytion. Need help please!
(Added rest of the code)



C#
 static void Main(string[] args)
        {


            string UserInput = "";
            int shift;

            Shift OBSHIFT = new Shift();
            Console.WriteLine("Welcome to Ceasar Shift Program:");
            Console.WriteLine("********************************");

            Console.WriteLine("type a string to decrypt:");
            UserInput = Console.ReadLine();


            Console.WriteLine("How many chars would you like to shift?: :");
            shift = int.Parse(Console.ReadLine());
            Console.Write("Your decrypted string is: ");
            Console.WriteLine(OBSHIFT.Decrypt(UserInput, shift));
            Console.Read();
        }
class Shift
       {
           public string Encrypt(string originalString, int shift)
           {
               string userOutput = "";
               char[] a = originalString.ToCharArray();
               for (int i = 0; i < originalString.Length; i++)
               {
                   char c = a[i];
                   int temp;
                   if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
                   {
                       temp = (int)(a[i] + shift);
                       if ((c >= 'A' && c <= 'Z' && temp > 'Z') || (c >= 'a' && c <= 'z' && temp > 'z'))
                           temp = temp - 26;
                       else
                           temp = (int)(a[i] + (shift));
                   }
                   else
                       temp = c;
                   userOutput += (char)temp;
               }
               return userOutput;
           }

           public string Decrypt(string cipherString, int shift)
           {
               string userOutput = "";
               char[] a = cipherString.ToCharArray();
               for (int i = 0; i < cipherString.Length; i++)
               {
                   char c = a[i];
                   int temp;
                   if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
                   {
                       temp = (int)(a[i] + shift);
                       if ((c >= 'A' && c <= 'Z' && temp > 'Z') || (c >= 'a' && c <= 'z' && temp > 'z'))
                           temp = temp + 26;
                       else
                           temp = (int)(a[i] + (shift));
                   }
                   else
                       temp = c;
                   userOutput += (char)temp;
               }

               return Encrypt(cipherString, -shift);
           }

       }
   }
Posted
Updated 24-Nov-15 6:53am
v3
Comments
phil.o 24-Nov-15 12:40pm    
Not clear. What special character? Prease improve your question with an example.
Jovan Yong 24-Nov-15 12:43pm    
i only want A-Z, i do not want symbols like !@#$%^&*()_++{}:"<>?
phil.o 24-Nov-15 12:57pm    
But you should get in the decrypted value the same value as the original one, the unencrypted value. So if you don't want them in decrypted text, and you do not have them in the unencrypted text, it means that both values are different, so you have a much bigger problem.
Please give AN EXAMPLE of unencrypted string, cyphered one and decrypted one, so that we can see where the problem lies.
Suvendu Shekhar Giri 24-Nov-15 12:46pm    
If you are able to encrypt text excluding special characters then decryption the same should result a value without any special character which is the same one you have encrypted.

Please explain with an example.
Jovan Yong 24-Nov-15 12:51pm    
for encryption, when i type fox, the outcome is ira. I let it jump 3 characters forward.
But for decryption when i type aaa, the outcome is ^^^. I also let it jump backwards 3 character, but its with special character instead of alphabets

Suggestions,
either check for shift being in the range 0 to 25 (or 0 to -25) and requery the user for a valid shift value, or take the shift as modulo 26:
C#
shift %= 26;

After that, then the shifted letters are no more than 26 out of range, so check for in range and add or subtract 26 as appropriate.
===
This isn't a complete solution on its own, it builds on my comments to the question.
 
Share this answer
 
v2
Comments
BillWoodruff 24-Nov-15 21:30pm    
+5 good work here, Matt, in both your answer and your dialogue with the OP, and for encouraging the OP do their homework, rather than writing their code for them !
Matt T Heffron 25-Nov-15 13:14pm    
Thanks!
Encryption has nothing to do with characters at all, forget about "special" ones. You need to do encryption only on one kind of object: array of bytes. If you want to perform encryption on strings, you have to do something like that:
C#
byte[] Encrypt(string data) {
   byte[] rawData = System.Text.Encoding.UTF8.GetBytes(data);
   return YourRealEncryptionMethod(rawData);
}

string Decrypt(string data) {
    byte[] rawData = YourReadDecryptionMethod(data);
    return new string(System.Text.Encoding.UTF8.GetChars(rawData));
}

You can use any encoding, as soon you use the same one in encryption and decryption, and this is one of the UTFs, to cover Unicode.
I have no idea why do you need your own encryption method, but, as a minimum, you have to rewrite it. Or look at what .NET FCL offers:
https://msdn.microsoft.com/en-us/library/system.security.cryptography.symmetricalgorithm%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.security.cryptography.asymmetricalgorithm%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.security.cryptography%28v=vs.110%29.aspx[^];

see also:
https://en.wikipedia.org/wiki/Public-key_cryptography[^],
https://en.wikipedia.org/wiki/Symmetric-key_algorithm[^],
https://en.wikipedia.org/wiki/Cryptography[^].

—SA
 
Share this answer
 
Comments
Matt T Heffron 24-Nov-15 13:25pm    
From the Main method in the original question, this is clearly supposed to be a Caesar Cypher, so only the alphabetic characters are being considered. Also, this is obviously an exercise/homework, so the constraint is valid for him.
Sergey Alexandrovich Kryukov 24-Nov-15 14:23pm    
All right, thank you, Matt. But even with Caesar Cypher, I would suggest going with bytes instead of characters, which is quite possible with the same algorithms. It's very important to understand that characters are not bytes anymore, not with .NET and other modern platforms.
—SA
Matt T Heffron 24-Nov-15 14:28pm    
Of course, with EBCDIC encoding it would be implemented very differently since the alphabetic characters are not contiguous!!!
Sergey Alexandrovich Kryukov 24-Nov-15 14:35pm    
I would say, it would be a bad idea to implement only encryption for alphabetic characters, but this is not deep in the nature of Caesar Cypher, which can easily be generalized. The whole idea to encrypt characters are pretty bad. Anyway, Caesar Cypher is nothing serious, could be considered as a sheer exercises, but I would prefer more reasonable exercises of the nearly the same complexity. It's bad to set bad conceptual models of design work, even where poor coding is taught.
—SA
Matt T Heffron 24-Nov-15 14:45pm    
The exception to your last statement would be a course on encryption where one could start with this bad model, do the simple analysis of why it is bad, and then move on to better techniques.
As pointed out in the comments above Decrypt function must be adjusted to check for values below 'A'/'a'. In addition the final call to Encrypt does not make sense.

C#
public string Decrypt(string cipherString, int shift)
        {
            string userOutput = "";
            char[] a = cipherString.ToCharArray();
            for (int i = 0; i < cipherString.Length; i++)
            {
                char c = a[i];
                int temp;
                if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
                {
                    //temp = (int)(a[i] + shift);
                    temp = (int)(a[i] - shift);
                    //if ((c >= 'A' && c <= 'Z' && temp > 'Z') || (c >= 'a' && c <= 'z' && temp > 'z'))
                   if ((c >= 'A' && c <= 'Z' && temp < 'A') || (c >= 'a' && c <= 'z' && temp < 'a'))
                        temp = temp + 26;
                    //else
                    //    temp = (int)(a[i] + (shift));
                }
                else
                    temp = c;
                userOutput += (char)temp;
            }

            //return Encrypt(cipherString, -shift);
            return userOutput;
        }  

[Edit: just fixed markdown mis-formatting that shouldn't have happened at all]
 
Share this answer
 
v3
Comments
Matt T Heffron 24-Nov-15 14:35pm    
You said: "the final call to Encrypt does not make sense"
On the contrary, since this is clearly a symmetrical cypher, decryption (deciphering) should be just applying the encryption with the inverse operation, so the most straightforward implementation would be to call the Encrypt with the negative of the original shift value.
Matt and Henrik have the right idea.

Write a shift function to do all the magic.

C#
public class Shift
{
static char shift(char letter, int distance)
{
    if (letter >= 'A' && letter <= 'Z')
    {
        // letter is zero to 25
        letter -= 'A';
        letter += distance;
        // wrap around for values above 25.
        letter %= 26;
        // letter is back in range of A to Z.
        letter += 'A';
    }
    else if (letter >= 'a' && letter <= 'z')
    {
        // letter is zero to 25
        letter -= 'a';
        letter += distance;
        // wrap around for values above 25.
        letter %= 26;
        // letter is back in range of a to z.
        letter += 'a';
    }
    return letter;
}

public static string Encrypt(string text, int distance)
{
    StringBuilder sb = new StringBuilder(text);
    for (int i = 0; i < sb.Length; i++)
        sb[i] = shift(sb[i], distance);
    return sb.ToString();
}

public static string Decrypt(string text, int distance)
{
    distance %= 26;
    return Encrypt(text, 26 - distance);
}

}


When using Encrypt with Decrypt, the same shift value must be used. The shift value is assumed to be positive.

Bonus link:
https://en.wikipedia.org/wiki/ROT13[^]
 
Share this answer
 
v2
Comments
Matt T Heffron 24-Nov-15 14:50pm    
The main reason I didn't give complete code is that this problem is pretty clearly homework, so the OP needs to do their own work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900