Use C # to encrypt and decrypt strings

Source: Internet
Author: User

Haha, I just started to learn C # recently. It feels good.

The following sectionCodeIs implemented in C #, mainly to encrypt and decrypt strings.


//Source code
// Encrypt and decrypt a string

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace study_test4
{
Class Program
{
Static void main (string [] ARGs)
{

String s; // defines storage input string variables
Int L; // defines the variable that stores the string length.


/* Encrypt the input string */
Console. writeline ("Please input a string :");
S = console. Readline (); // input string

L = S. length; // calculate the string length
Char [] arr = new char [l]; // defines an array of characters to store encrypted strings.
Arr [0] = s [L-1]; // stores the end character of the input string to the first position of the character array

For (INT I = 1; I <L; I ++)
Arr [I] = (char) (INT) (s [I-1]) + 3);/* from the second character of the input string to the second-to-last character of the input string,

* Add three characters in sequence, and store the remaining positions in the character array */

Console. Write ("encrypted :");
 
For (INT I = 0; I <L; I ++)
Console. Write ("{0}", arr [I]); // output encrypted characters

/* Decrypt the encrypted string */
Char [] arr_decode = new char [l];
Arr_decode [L-1] = arr [0];
For (INT I = 1; I <L; I ++)
Arr_decode [I-1] = (char) (INT) (ARR [I])-3 );

Console. Write ("decrypted :");

For (INT I = 0; I <L; I ++)
Console. Write ("{0}", arr_decode [I]); // output encrypted characters

Console. Readline (); // stop the console on the display page.
}
}
}

In the process of learning C #, I am deeply aware that a good piece of code must be annotated, because the code written is not just for myself. So in order to develop the habit of writing comments, although the Code is very simple, I also commented out them one by one.

I hope you can help us with the correction. Thank you, O (∩ _ ∩) O.

Related Article

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.