C # Encryption Algorithm

Source: Internet
Author: User

Built-in MD5, DSA, and RSA encryption. You need to introduce using system. Security. cryptography;

// 32-bit MD5 function public static string md532 (string Str) {string Cl = STR; string Pwd = ""; MD5 MD5 = md5.create (); byte [] S = md5.computehash (encoding. utf8.getbytes (CL); For (INT I = 0; I <S. length; I ++) {Pwd = PWD + s [I]. tostring ("X") ;}return PWD ;}// 16-bit MD5 function public static string md516 (string convertstring) {md5cryptoserviceprovider MD5 = new md5cryptoserviceprovider (); string t2 = bitconverter. tostring (md5.computehash (utf8encoding. default. getbytes (convertstring), 4, 8); t2 = t2.replace ("-", ""); t2 = t2.tolower (); Return T2 ;} public static string dsaencrypt (string Str) {byte [] bytes = encoding. ASCII. getbytes (STR); // select the signature method, including RSA and DSA dsacryptoserviceprovider dsac = new dsacryptoserviceprovider (); byte [] Sign = dsac. signdata (bytes); string strresult = convert. tostring (sign); Return strresult ;}

Des encryption

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. security. cryptography; using system. io; namespace deshelper {class desjob {public static string encryptkey = "oyea "; // define the key # region encrypted string /// <summary> /// encrypted string /// </Summary> /// <Param name = "str"> to encrypt string </param> // <returns> encrypted string </returns> Public static string encrypt (string Str) {descryptoserviceprovider descsp = new descryptoserviceprovider (); // instantiate the encryption/Decryption Class Object byte [] Key = encoding. unicode. getbytes (encryptkey); // defines the byte array to store the key byte [] DATA = encoding. unicode. getbytes (STR); // defines the byte array to store the string memorystream mstream = new memorystream (); // instantiate the memory stream object // use the memory stream to instantiate the encrypted Stream object cryptostream cstream = new cryptostream (mstream, descsp. createencryptor (key, key), cryptostreammode. write); cstream. write (data, 0, Data. length); // write data to the encrypted stream. flushfinalblock (); // release the encrypted stream return convert. tobase64string (mstream. toarray ()); // return the encrypted string} # endregion # region decryption string // <summary> // decrypt the string /// </Summary> // <Param name =" str "> the string to be decrypted </param> // <returns> the decrypted string </returns> Public static string decrypt (string Str) {descryptoserviceprovider descsp = new descryptoserviceprovider (); // instantiate the encryption/Decryption Class Object byte [] Key = encoding. unicode. getbytes (encryptkey); // defines the byte array to store the key byte [] DATA = convert. frombase64string (STR); // defines a byte array to store the string memorystream mstream = new memorystream (); // instantiate the memory stream object // use the memory stream to instantiate and decrypt the stream object cryptostream cstream = new cryptostream (mstream, descsp. createdecryptor (key, key), cryptostreammode. write); cstream. write (data, 0, Data. length); // write data into the decryption stream. flushfinalblock (); // release the decryption stream return encoding. unicode. getstring (mstream. toarray (); // returns the decrypted string} # 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.