Caesar cipher, decrypted by Caesar encryption.

Source: Internet
Author: User
Tags lowercase

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 Caesararithmetic {public partial class Frm_main:form {int ishifting = 5;//default offset 5 Publ
        IC Frm_main () {InitializeComponent ();
            }//Get ASCII public int ASCII (string str) {byte[] array = new byte[1];//for character to create byte array Array = System.Text.Encoding.ASCII.GetBytes (str);//Assignment int asciicode = (short) (array[0) for a byte array;//Get the number of bytes
            The first entry of the group return asciicode;//returns the first item of the byte array} public string Caesar (String str)/Caesar encryption algorithm implementation { Char[] C = str. ToCharArray ();//create character array string strcaesar = "";//define a variable to store the encrypted string for (int i = 0; I < Str. Length; i++)//traversal of each string in the string {string ins = C[i]. ToString ();//the character int to which the record is traversediins = AscII (INS); String outs = "";//define a variable to record the encrypted string bool Ischar = "0123456789abcdefghijklmnopqrstuvwxyz". Contains (Ins. ToLower ())//To determine whether the specified string contains the traversed character bool Istoupperchar = Ischar && (ins. ToUpper () = ins);//Determine if the character being traversed is uppercase ins = Ins. ToLower ();//Converts the traversed character to a lowercase if (ischar)//To determine whether the specified string contains the characters that are traversed to {CONSOLE.WR

                   Iteline ("ins = {0}, a = {1}, Z={2},ascii (INS) ={3}", Ins, ASCII ("a"), ASCII ("Z"), ASCII (INS)); int offset = (ASCII (INS) + Ishifting-ascii ("a"))% (ASCII ("Z")-ASCII ("a") + 1);//Get the AscII code of the character Cons Ole.
                    WriteLine ("({0} + 5-97)% (122-97 + 1)", AscII (INS));

                    Console.WriteLine ("Offset={0}", offset); Outs = Convert.tochar (offset + AscII ("a")). ToString ();//converts to a character and records if (Istoupperchar)//is capitalized {outs = Outs. ToUpper ()//Convert Allis uppercase}} else {outs = ins;//record traversal 
        The character} Strcaesar + + outs;//added to the encrypted string} return strcaesar;//returns the encrypted string } private void Button1_Click (object sender, EventArgs e) {string p_str_code = Textb Ox1. text;//record the password to be encrypted TextBox2.Text = Caesar (P_str_code);//display encrypted string} private void Button2_Click (o Bject sender, EventArgs e) {string p_str_code = textbox2.text;//record password to decrypt TextBox2.Text = Ca ESAR2 (P_str_code),//display decrypted string} public string Caesar2 (String str)/Caesar encryption algorithm implementation {char[ ] C = str. ToCharArray ();//create character array string strcaesar = "";//define a variable to store the encrypted string for (int i = 0; I < Str. Length; i++)//traversal of each string in the string {string ins = C[i]. ToString ();//log traversal to the character string outs = "";//define a variable to recordEncrypted string bool Ischar = "0123456789abcdefghijklmnopqrstuvwxyz". Contains (Ins. ToLower ())//To determine whether the specified string contains the traversed character bool Istoupperchar = Ischar && (ins. ToUpper () = ins);//Determine if the character being traversed is uppercase ins = Ins. ToLower ();//Converts the traversed character to lowercase if (ischar)//Determines whether the specified string contains the character {int iins = AscII (
                 INS);
                 Console.WriteLine ("ins = {0}, a = {1}, z={2}", Iins, ASCII ("a"), ASCII ("Z"));  int offset = (ASCII (INS) + 5-ascii ("a"))% (ASCII ("Z")-ASCII ("a") + 1);//Get the AscII code of the character//int offset = (ASCII (INS) + 5-ascii ("a"))% (ASCII ("Z")-ASCII ("a") + 1);//Get the ASCII int offset = (ASCII ("z") + I of the character)  
                 
                 Shifting-ascii (INS))% (ASCII ("Z")-ASCII ("a") + 1);

                 Console.WriteLine ("offset = {0}", offset); Outs = Convert.tochar (offset + AscII ("a")). ToString ();//convert to character and record outs = Convert.tochar (AscII ("z")-offsET). ToString ();//converts to a character and records if (Istoupperchar)//is capitalized {outs = outs.

              ToUpper ();//all converted to uppercase} Console.WriteLine ("Outs = {0}", outs); else {outs = ins;//log Traversal character} Strcaesar + = Outs
 ;//Add to encrypted string} return strcaesar;//returns encrypted string}}}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.