2013-03-04 172 views
-1

試圖創建一個簡單的替換密碼,我的程序工作得很好,但它的數據不加密或解密。我真的不知道我應該添加到我的代碼,以便它能正常工作...任何想法? 這是我的代碼使用簡單的替換密碼進行加密和解密

namespace yaba 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void btnEncrypt_Click(object sender, EventArgs e) 
     { 
      string encrypt = tboxIO.Text; 
      encrypt.ToLower(); 


      bool tbNull = tboxIO.Text == ""; 

      if (tbNull) 
       MessageBox.Show("There is nothing to encrypt."); 

      else 
      { 
       char[] array = encrypt.ToCharArray(); 

       for (int i = 0; i < array.Length; i++) 
       { 
        int num = (int)array[i]; 
        if (num >= 'a' && num <= 'z') 
        { 
         num += Convert.ToInt32(tbShift.Text); 
         if (num > 'z') 
         { 
          num = num - 26; 
         } 
        } 
        else if (num >= 'A' && num <= 'Z') 
        { 
         num += Convert.ToInt32(tbShift.Text); 
         if (num > 'Z') 
         { 
          num = num - 26; 
         } 
        } 
        array[i] = (char)num; 
       } 
       lblIO.Text = "Encrypted Message"; 
       tboxIO.Text = new string(array).ToLower(); 
      } 

      tboxIO.Copy(); 
     } 

     private void btnDecrypt_Click(object sender, EventArgs e) 
     { 
      string decrypt = tboxIO.Text; 
      decrypt.ToLower(); 

      bool tbNull = tboxIO.Text == ""; 

      if (tbNull) 
       MessageBox.Show("There is nothing to decrypt."); 

      else 
      { 
       char[] array = decrypt.ToCharArray(); 
       for (int i = 0; i < array.Length; i++) 
       { 
        int num = (int)array[i]; 
        if (num >= 'a' && num <= 'z') 
        { 
         num -= Convert.ToInt32(tbShift.Text); 
         if (num > 'z') 
          num = num - 26; 

         if (num < 'a') 
          num = num + 26; 
        } 
        else if (num >= 'A' && num <= 'Z') 
        { 
         num -= Convert.ToInt32(tbShift.Text); 
         if (num > 'Z') 
          num = num - 26; 

         if (num < 'A') 
          num = num + 26; 
        } 
        array[i] = (char)num; 
       } 
       lblIO.Text = "Decrypted Message"; 
       tboxIO.Text = new string(array).ToUpper(); 
      } 

      tboxIO.Copy(); 
     } 

     private void pictureBox1_Click(object sender, EventArgs e) 
     { 
      MessageBox.Show("hehe"); 
     } 

     private void tboxIO_MouseClick(object sender, MouseEventArgs e) 
     { 
      tboxIO.SelectAll(); 
      tboxIO.Copy(); 
     } 

     private void tbShift_MouseClick(object sender, MouseEventArgs e) 
     { 
      tbShift.SelectAll(); 
     } 

     private void label1_Click(object sender, EventArgs e) 
     { 

     } 


    } 
} 

回答

1

那麼,按什麼邏輯你想加密的數字?他們是否也應該轉移Convert.ToInt32(tbShift.Text)次?然後你可以添加另一個else if(num >= '0' && num <= '9')並做同樣的事情(+/- 10而不是+/- 26)。

但是,您需要意識到,您可能會經歷兩次甚至三次,具體取決於您要轉移多少。我希望你會莫名其妙地確信tbShift.Text只能(!)有一個數字不超過26

較高,但需要知道你是怎麼想加密/解密的數字告訴我們,所以我們甚至有機會幫助你。

0

據我所知,你明確地說,你只需要大寫和小寫字母進行加密,所以沒有,數字是不加密的。

另外(不相關的)您對ToLower的調用沒有意義,因爲ToLower是一個函數,您不會將結果賦值給任何東西。

1

我改變了你的代碼。你不應該使用ToLower。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     string myText = "MusLum"; 
     string encrypted = ""; 
     string decrypted = ""; 
     char shift = 'a'; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Encrypt_Click(object sender, EventArgs e) 
     { 
      string encrypt = myText; 

      bool tbNull = myText == ""; 

      if (tbNull) 
       MessageBox.Show("There is nothing to encrypt."); 

      else 
      { 
       char[] array = encrypt.ToCharArray(); 

       for (int i = 0; i < array.Length; i++) 
       { 
        int num = (int)array[i]; 
        if (num >= 'a' && num <= 'z') 
        { 

         num += (Convert.ToInt32(shift.ToString().ToLower()[0]) - Convert.ToInt32('a')+1); 

         if (num > 'z') 
         { 
          num = num - 26; 
         } 
        } 
        else if (num >= 'A' && num <= 'Z') 
        { 
         num += (Convert.ToInt32(shift.ToString().ToUpper()[0]) - Convert.ToInt32('A')+1); 

         if (num > 'Z') 
         { 
          num = num - 26; 
         } 
        } 
        array[i] = (char)num; 
       } 

       encrypted = new string(array); 
      } 


     } 

     private void Decrypt_Click(object sender, EventArgs e) 
     { 
      string decrypt = encrypted; 


      bool tbNull = encrypted == ""; 

      if (tbNull) 
       MessageBox.Show("There is nothing to decrypt."); 

      else 
      { 
       char[] array = encrypted.ToCharArray(); 
       for (int i = 0; i < array.Length; i++) 
       { 
        int num = (int)array[i]; 
        if (num >= 'a' && num <= 'z') 
        { 
         num -= (Convert.ToInt32(shift.ToString().ToLower()[0]) - Convert.ToInt32('a')+1); 

         if (num > 'z') 
          num = num - 26; 

         if (num < 'a') 
          num = num + 26; 
        } 
        else if (num >= 'A' && num <= 'Z') 
        { 
         num -= (Convert.ToInt32(shift.ToString().ToUpper()[0]) - Convert.ToInt32('A')+1); 

         if (num > 'Z') 
          num = num - 26; 

         if (num < 'A') 
          num = num + 26; 
        } 
        array[i] = (char)num; 
       } 

       decrypted = new string(array); 
      } 
     } 
    } 
}