thieumao

Mã dịch vòng

Sep 8th, 2013
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace MaDichVong
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private string strInput = "";
  20.         private string strOutput = "";
  21.         private int iKey;
  22.  
  23.         private void btEncrypt_Click(object sender, EventArgs e)
  24.         {
  25.             strInput = tbInput.Text;
  26.             strOutput = "";
  27.             try
  28.             {
  29.                 iKey = int.Parse(tbKey.Text.ToString());
  30.                 iKey = iKey % 26;
  31.             }
  32.             catch (Exception eikey)
  33.             {
  34.                 iKey = 0;
  35.             }
  36.  
  37.             char[] ch = strInput.ToCharArray();
  38.             for (int i = 0; i < strInput.Length; i++)
  39.             {
  40.                 // CHU HOA
  41.                 if (ch[i] >= 'A' && ch[i] <= 'Z')
  42.                 {
  43.                     if (ch[i] + iKey > 'Z')
  44.                     {
  45.                         strOutput += ((char)(ch[i] + iKey - 26)).ToString();
  46.                     }
  47.                     else
  48.                     {
  49.                         strOutput += ((char)(ch[i] + iKey)).ToString();
  50.                     }
  51.                 }
  52.                 // chu thuong
  53.                 else if (ch[i] >= 'a' && ch[i] <= 'z')
  54.                 {
  55.                     if (ch[i] + iKey > 'z')
  56.                     {
  57.                         strOutput += ((char)(ch[i] + iKey - 26)).ToString();
  58.                     }
  59.                     else
  60.                     {
  61.                         strOutput += ((char)(ch[i] + iKey)).ToString();
  62.                     }
  63.                 }
  64.                 else
  65.                 {
  66.                     strOutput += ((char)ch[i]).ToString();
  67.                 }
  68.             }
  69.  
  70.             tbOutput.Text = strOutput;
  71.         }
  72.  
  73.         private void btDencrypt_Click(object sender, EventArgs e)
  74.         {
  75.             strInput = tbInput.Text;
  76.             strOutput = "";
  77.             try
  78.             {
  79.                 iKey = int.Parse(tbKey.Text.ToString());
  80.                 iKey = iKey % 26;
  81.             }
  82.             catch (Exception eikey)
  83.             {
  84.                 iKey = 0;
  85.             }
  86.  
  87.             char[] ch = strInput.ToCharArray();
  88.             for (int i = 0; i < strInput.Length; i++)
  89.             {
  90.                 // CHU HOA
  91.                 if (ch[i] >= 'A' && ch[i] <= 'Z')
  92.                 {
  93.                     if (ch[i] - iKey < 'A')
  94.                     {
  95.                         strOutput += ((char)(ch[i] - iKey + 26)).ToString();
  96.                     }
  97.                     else
  98.                     {
  99.                         strOutput += ((char)(ch[i] - iKey)).ToString();
  100.                     }
  101.                 }
  102.                 // chu thuong
  103.                 else if (ch[i] >= 'a' && ch[i] <= 'z')
  104.                 {
  105.                     if (ch[i] - iKey < 'a')
  106.                     {
  107.                         strOutput += ((char)(ch[i] - iKey + 26)).ToString();
  108.                     }
  109.                     else
  110.                     {
  111.                         strOutput += ((char)(ch[i] - iKey)).ToString();
  112.                     }
  113.                 }
  114.                 else
  115.                 {
  116.                     strOutput += ((char)ch[i]).ToString();
  117.                 }
  118.             }
  119.  
  120.             tbOutput.Text = strOutput;
  121.         }
  122.  
  123.         private void btClear_Click(object sender, EventArgs e)
  124.         {
  125.             strInput = "";
  126.             strOutput = "";
  127.             tbInput.Text = "";
  128.             tbOutput.Text = "";
  129.             tbKey.Text = "";
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment