HELLO FRIENDS,
I m creating a CRYPTOGRAPHY WINDOWS FORM APPLICATION.
1-Calculate String Length. (ALREADY DONE)
2-Reverse the String. (ALREADY DONE)
3-Total Length of the message will be save and each character of the message will be replaced from it. (HELP REQUIRED)
Example:
You wrote a message " A CAT"
so STEP 1: calculate Length : 5
STEP 2: reverse : TAC A
STEP 3 : String length is added: so 5 is added to each corresponding character
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Therefore,
A is at 1 , when 5 is added it will become F which is 6,
C is at 3, when 5 is added it will become H, which is 8,
A is again replaced with F
T is at 20, when 5 is added it will become Y, which is 25
Therefore, A message "A CAT" will become " F HFY"
So far i done that:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string UIT_CRYPTOGRAPHY()
{
string userString = richTextBox1.Text;
char[] textArray = userString.ToCharArray();
int stringLength = userString.Length;
Array.Reverse(textArray);
label1.Text = new string(textArray);
return label1.Text;
}
private void button3_Click(object sender, EventArgs e)
{
/* string[] stringArray = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z" };*/
string ReverseString = UIT_CRYPTOGRAPHY();
label1.Text = ReverseString;
}
PLEASE HELP ME
THANKS IN ADVANCE.
Loading
prince shilsPosted Mar 31, 2012, 12:35 AM
prince shilsPosted Mar 31, 2012, 12:34 AM
Sam HobbsPosted Mar 30, 2012, 5:25 PM
VulpesPosted Mar 30, 2012, 2:53 PM
1. You need to wrap around if k is less than 65 rather than if k is more than 90.
2. You need to reverse the string after the above manipulation rather than before it.
You can do all this in the Decode button Click handler alone:
prince shilsPosted Mar 30, 2012, 1:16 PM
For example:
when i decode WONDERFULL result returns =ONDE8F;LL which is wrong.
here is a code:
//DECODE METHOD
public string CRYPTOGRAPHY_REVERSE()
{
label1.Text = label1.Text.ToUpper(); // convert to upper case
string userString = label1.Text;
char[] decodeArray = userString.ToCharArray();
int stringLength = userString.Length;
Array.Reverse(decodeArray);
label2.Text = new string(decodeArray);
return label2.Text;
}
//DECODE BUTTON CODE
string DecodeString = CRYPTOGRAPHY_REVERSE();
int len = DecodeString.Length;
char[] encrypted = new char[len]; // create an array to hold the encrypted characters
for (int i = 0; i < len; i++)
{
int j = DecodeString[i]; // char is implicitly convertible to int, so A -> 65, B -> 66 etc.
if (j >= 65 && j <= 90) // if between A and Z
{
int k = DecodeString[i] - len; // add 5
if (k > 90) k += 26; // wrap round if more than 90
encrypted[i] = (char)k; // convert back to a character
}
else
{
encrypted[i] = DecodeString[i]; // leave unchanged
}
}
label2.Text = new string(encrypted); // create new string and assign to label
//THANKYOU
prince shilsPosted Mar 28, 2012, 11:05 PM
VulpesPosted Mar 28, 2012, 2:37 PM
You can then use the fact that the letters A to Z have ASCII values of 65 to 90 inclusive to add 5 to them and convert back to a character. The last 5 letters V, W, X, Y and Z will need to 'wrap round' so they are mapped to A, B, C, D and E respectively.
Here's some revised code: