Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 MaDichVong
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private string strInput = "";
- private string strOutput = "";
- private int iKey;
- private void btEncrypt_Click(object sender, EventArgs e)
- {
- strInput = tbInput.Text;
- strOutput = "";
- try
- {
- iKey = int.Parse(tbKey.Text.ToString());
- iKey = iKey % 26;
- }
- catch (Exception eikey)
- {
- iKey = 0;
- }
- char[] ch = strInput.ToCharArray();
- for (int i = 0; i < strInput.Length; i++)
- {
- // CHU HOA
- if (ch[i] >= 'A' && ch[i] <= 'Z')
- {
- if (ch[i] + iKey > 'Z')
- {
- strOutput += ((char)(ch[i] + iKey - 26)).ToString();
- }
- else
- {
- strOutput += ((char)(ch[i] + iKey)).ToString();
- }
- }
- // chu thuong
- else if (ch[i] >= 'a' && ch[i] <= 'z')
- {
- if (ch[i] + iKey > 'z')
- {
- strOutput += ((char)(ch[i] + iKey - 26)).ToString();
- }
- else
- {
- strOutput += ((char)(ch[i] + iKey)).ToString();
- }
- }
- else
- {
- strOutput += ((char)ch[i]).ToString();
- }
- }
- tbOutput.Text = strOutput;
- }
- private void btDencrypt_Click(object sender, EventArgs e)
- {
- strInput = tbInput.Text;
- strOutput = "";
- try
- {
- iKey = int.Parse(tbKey.Text.ToString());
- iKey = iKey % 26;
- }
- catch (Exception eikey)
- {
- iKey = 0;
- }
- char[] ch = strInput.ToCharArray();
- for (int i = 0; i < strInput.Length; i++)
- {
- // CHU HOA
- if (ch[i] >= 'A' && ch[i] <= 'Z')
- {
- if (ch[i] - iKey < 'A')
- {
- strOutput += ((char)(ch[i] - iKey + 26)).ToString();
- }
- else
- {
- strOutput += ((char)(ch[i] - iKey)).ToString();
- }
- }
- // chu thuong
- else if (ch[i] >= 'a' && ch[i] <= 'z')
- {
- if (ch[i] - iKey < 'a')
- {
- strOutput += ((char)(ch[i] - iKey + 26)).ToString();
- }
- else
- {
- strOutput += ((char)(ch[i] - iKey)).ToString();
- }
- }
- else
- {
- strOutput += ((char)ch[i]).ToString();
- }
- }
- tbOutput.Text = strOutput;
- }
- private void btClear_Click(object sender, EventArgs e)
- {
- strInput = "";
- strOutput = "";
- tbInput.Text = "";
- tbOutput.Text = "";
- tbKey.Text = "";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment