C # encryption and decryption

Source: Internet
Author: User

Simple Encryption

Code
Using System;
Using System. Collections. Generic;

Namespace MD
{
Class Mainclass
{
Public   Interface Ibindesh
{
String Encode ( String Str );
String Decode ( String Str );
}

Public   Class Encryptiondecryption: ibindesh
{
Public   String Encode ( String Str)
{
String Htext =   "" ;
For ( Int I = 0 ; I < Str. length; I ++ )
{
Htext = Htext + ( Char ) (STR [I] +   10   -   1   *   2 );
}
Return Htext;
}
Public   String Decode ( String Str)
{
String Dtext =   "" ;
For ( Int I =   0 ; I < Str. length; I ++ )
{
Dtext = Dtext + ( Char ) (STR [I] -   10   +   1   *   2 );
}
Return Dtext;
}

Public   Static   Void Main ( String [] ARGs)
{
Encryptiondecryption inter =   New Encryptiondecryption ();
String Str = (Ibindesh) Inter). encode ( " Admin " );
Console. writeline (STR );
Console. writeline (ibindesh) Inter). Decode (STR ));
Console. Read ();
}
}
}
}

 

Use the classes provided in. Net for DES encryption

Code
Using System;
Using System. Data;
Using System. configuration;
Using System. collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. Web. UI. htmlcontrols;
Using System. IO;
Using System. Security. cryptography;
Using System. text;

Public   Partial   Class Encryption and decryption: system. Web. UI. Page
{
Protected   Void Page_load ( Object Sender, eventargs E)
{
String Encryptdata =   " ABCD " ; // String to be encrypted
String Encryptkey =   " 12345678 " ; // Key, which must be 8 bits
Stringbuilder sb =   New Stringbuilder ();
SB. append ( " The data to be encrypted is: " + Encryptdata + " <Br/> " );
SB. append ( " The encrypted data is: " + Encrypt (encryptdata, encryptkey) + " <Br/> " );
SB. append ( " The decrypted data is: " + Decrypt (encrypt (encryptdata, encryptkey), encryptkey) + " <Br/> " );

Response. Write (sb. tostring ());
}
# Region
Private   String Encrypt ( String Strdata, String Strkey)
{
Descryptoserviceprovider des =   New Descryptoserviceprovider ();
Byte [] Bytedata = Encoding. utf8.getbytes (strdata );
// Foreach (byte B in bytedata)
// {
// Response. Write (B + "<br/> ");
// }
// Response. Write ("<HR/> ");

Des. Key = Asciiencoding. ASCII. getbytes (strkey ); // ASCII Value
Des. iv = Asciiencoding. ASCII. getbytes (strkey );
// Foreach (byte C in Des. Key)
// {
// Response. Write (C + "<br/> ");
// }
// Response. Write ("<HR/> ");

Memorystream MS =   New Memorystream ();
Using (Cryptostream CS =   New Cryptostream (MS, Des. createencryptor (), cryptostreammode. Write ))
{
CS. Write (bytedata, 0 , Bytedata. Length );
CS. flushfinalblock ();
CS. Close ();
}
// Foreach (byte D in ms. toarray ())
// {
// Response. Write (D + "<br/> ");
// }
String Str = Convert. tobase64string (Ms. toarray ());
Ms. Close ();
Des. Clear ();
Return STR;
}
Private   String Decrypt ( String Strdata, String Strkey)
{
Byte [] Bytedata = Convert. frombase64string (strdata );
Using (Descryptoserviceprovider des =   New Descryptoserviceprovider ())
{
Des. Key = Asciiencoding. ASCII. getbytes (strkey );
Des. iv = Asciiencoding. ASCII. getbytes (strkey );
Memorystream MS =   New Memorystream ();
Using (Cryptostream CS =   New Cryptostream (MS, Des. createdecryptor (), cryptostreammode. Write ))
{
CS. Write (bytedata, 0 , Bytedata. Length );
CS. flushfinalblock ();
CS. Close ();
}
String Str = Encoding. utf8.getstring (Ms. toarray ());
Ms. Close ();
Return STR;
}
}
# Endregion
}

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.