C # encryption or decryption algorithm cryptography CRC32 MD5 base64

Source: Internet
Author: User
Tags crc32

Encryption or decryption algorithms and operations of cryptography, CRC32, MD5, and base64

Using system; <br/> using system. collections. generic; <br/> using system. text; <br/> using system. security; <br/> using system. security. cryptography; <br/> using system. data; <br/> using system. io; </P> <p> namespace sercuritylib <br/>{</P> <p>/** /// <summary> <br/> // For Cryptography summary. <Br/> // </Summary> <br/> public class cryptography <br/> {<br/> // encryption key <br/> Private Static encryption ricalgorithm key; </P> <p> static Cryptography () <br/>{< br/> key = new descryptoserviceprovider (); <br/>}< br/> Public Cryptography (symmetricalgorithm inkey) <br/>{< br/> key = inkey; <br/>}< br/>/** // <summary> <br/> // hash string <br/> /// </Summary> <br/> // <Param name = "PWD"> </param> <br/> /// <Returns> </returns> <br/> Public static string getpwdhash (string PWD) <br/>{< br/> string ret = ""; <br/> byte [] bpwd = encoding. ASCII. getbytes (PWD. trim (); <br/> byte [] edata; <br/> sha256 Sha = new sha256managed (); <br/> edata = Sha. computehash (bpwd); <br/> ret = convert. tobase64string (edata); <br/> return ret. trim (); <br/>}< br/>/** // <summary> <br/> // symmetric encryption of strings to return encrypted data <br/> /// </sum Mary> <br/> /// <Param name = "original"> </param> <br/> /// <returns> </returns> <br/> Public static string getencrypt (string original) <br/>{< br/> return convert. tobase64string (encrypt (original, key )); <br/>}</P> <p>/** // <summary> <br/> // perform symmetric decryption on the string to return the decrypted data <br/> // </Summary> <br/> // <Param name = "encrypt"> </param> <br/> // <returns> </returns> <br/> Public static string getdecrypt (string Encrypt) <br/>{< br/> return decrypt (convert. frombase64string (encrypt), key); <br/>}</P> <p> Public String bytestostring (byte [] BS) <br/>{< br/> stringbuilder sb = new stringbuilder (); <br/> foreach (byte B in BS) <br/>{< br/> Sb. append (B. tostring ("000"); <br/>}< br/> return sb. tostring (); <br/>}< br/> Public static byte [] stringtobytes (string s) <br/>{< br/> int BL = S. length/3; <br/> Te [] BS = new byte [BL]; <br/> for (INT I = 0; I <BL; I ++) <br/> {<br/> BS [I] = byte. parse (S. substring (3 * I, 3); <br/>}< br/> return BS; <br/>}</P> <p> Private Static byte [] encrypt (string plaintext, symmetricalgorithm key) <br/>{< br/> memorystream MS = new memorystream (); <br/> cryptostream encstream = new cryptostream (MS, key. createencryptor (), cryptostreammode. write); <br/> streamwriter s W = new streamwriter (encstream); <br/> SW. writeline (plaintext); <br/> SW. close (); <br/> encstream. close (); <br/> byte [] buffer = Ms. toarray (); <br/> MS. close (); <br/> return buffer; <br/>}</P> <p> Private Static string decrypt (byte [] cyphertext, symmetricalgorithm key) <br/>{< br/> memorystream MS = new memorystream (cyphertext); <br/> cryptostream encstream = new cryptostream (MS, key. createdecryp Tor (), cryptostreammode. read); <br/> streamreader sr = new streamreader (encstream); <br/> string val = sr. readline (); <br/> Sr. close (); <br/> encstream. close (); <br/> MS. close (); <br/> return val; <br/>}< br/>/** // <summary> <br/> // CRC verification <br/> /// quick Detection Algorithm <br/> /// </Summary> <br/> public class CRC32 <br/> {</P> <p> /**//// <summary> <br/> /// </Summary> <br/> protected UlO NG [] crc32table; </P> <p>/** // <summary> <br/> // structure: initialize the validation table <br/> /// </Summary> <br/> Public CRC32 () <br/>{< br/> const ulong ulpolynomial = 0xedb88320; <br/> ulong dwcrc; <br/> crc32table = new ulong [256]; <br/> int I, j; <br/> for (I = 0; I <256; I ++) <br/>{< br/> dwcrc = (ulong) I; <br/> for (j = 8; j> 0; J --) <br/>{< br/> If (dwcrc & 1) = 1) <br/> dwcrc = (dwcrc> 1) ^ ulpolynomial; <Br/> else <br/> dwcrc >>= 1; <br/>}< br/> crc32table [I] = dwcrc; <br/>}</P> <p>/** // <summary> <br/> // verify the byte array <br/> /// </Summary> <br/> /// <Param name = "buffer"> ref byte array </param> <br/> // <returns> </returns> <br/> Public ulong bytecrc (ref byte [] buffer) <br/> {<br/> ulong ulcrc = 0 xffffffff; <br/> ulong Len; <br/> Len = (ulong) buffer. length; <br/> for (ulong buffptr = 0; buffptr <Len; buffptr ++) <br/>{< br/> ulong tabptr = ulcrc & 0xff; <br/> tabptr = tabptr ^ buffer [buffptr]; <br/> ulcrc = ulcrc> 8; <br/> ulcrc = ulcrc ^ crc32table [tabptr]; <br/>}< br/> return ulcrc ^ 0 xffffffff; <br/>}</P> <p>/** // <summary> <br/> // string verification <br/> // </ summary> <br/> /// <Param name = "sinputstring"> string </param> <br/> /// <returns> </returns> <br/> public ulong stringcrc (string sinpu Tstring) <br/>{< br/> byte [] buffer = encoding. default. getbytes (sinputstring); <br/> return bytecrc (ref buffer ); <br/>}</P> <p>/** // <summary> <br/> // file verification <br/> // </ summary> <br/> /// <Param name = "sinputfilename"> input file </param> <br/> /// <returns> </returns> <br/> Public ulong filecrc (string sinputfilename) <br/>{< br/> filestream infile = new system. io. filestream (sinputfilename, system. io. Filemode. open, system. io. fileaccess. read); <br/> byte [] binput = new byte [infile. length]; <br/> infile. read (binput, 0, binput. length); <br/> infile. close (); </P> <p> return bytecrc (ref binput ); <br/>}</P> <p>/** // <summary> <br/> // MD5 is not reversed. encoding <br/> // obtain the unique feature string, password Encryption <br/> // (cannot be restored) <br/> // </Summary> <br/> public class MD5 <br/> {</P> <p>/** // <summary> <br/> /// </summ Ary> <br/> Public MD5 () <br/>{< br/>}</P> <p>/** // <summary> <br/> /// obtain the character string. <br/> // </Summary> <br/> // <Param name = "sinputstring"> input text </param> <br/> // <returns> </returns> <br/> Public static string hashstring (string sinputstring) <br/>{< br/> system. security. cryptography. MD5 MD5 = system. security. cryptography. md5.create (); <br/> string encoded = bitconverter. tostring (md5.computehas H (encoding. default. getbytes (sinputstring ))). replace ("-", ""); <br/> return encoded; <br/>}</P> <p>/** // <summary> <br/> // obtain the feature string of the file. <br/> // /</Summary> <br/> // <Param name = "sinputfilename"> input file </param> <br/> // <returns> </returns> <br/> Public String hashfile (string sinputfilename) <br/>{< br/> system. security. cryptography. MD5 MD5 = system. security. cryptography. md5.create (); <br/> filestr EAM infile = new system. io. filestream (sinputfilename, system. io. filemode. open, system. io. fileaccess. read); <br/> byte [] binput = new byte [infile. length]; <br/> infile. read (binput, 0, binput. length); <br/> infile. close (); </P> <p> string encoded = bitconverter. tostring (md5.computehash (binput )). replace ("-", ""); <br/> return encoded; <br/>}</P> <p>/** // <summary> <br/> // base64 uuencoded Encoding <br/> // encodes the binary into ASCII text for network transmission <br/> // (recoverable) <br/> // </Summary> <br/> public class base64 <br/> {</P> <p>/** // <summary> <br/> /// </Summary> <br/> Public base64 () <br/>{< br/>}</P> <p>/** // <summary> <br/> // decodes the string. <br/> /// </Summary> <br/> /// <Param name = "sinputstring"> input text </param> <br/> // <returns> </ returns> <br/> Public String decryptstring (string sinputstring) <br />{< Br/> char [] sinput = sinputstring. tochararray (); <br/> try <br/> {<br/> byte [] boutput = system. convert. frombase64string (sinputstring); <br/> return encoding. default. getstring (boutput); <br/>}< br/> catch (system. argumentnullexception) <br/>{< br/> // The 64-character array of base is null <br/> return "; <br/>}< br/> catch (system. formatexception) <br/>{< br/> // The length is incorrect. 4 cannot be divisible. <br/> return ""; <br/>}< br/>} </P> <p>/** // <summary> <br/> // encode the string <br/> /// </Summary> <br/> /// <Param name = "sinputstring"> input text </param> <br/> /// <returns> </returns> <br/> Public String encryptstring (string sinputstring) <br/> {<br/> byte [] binput = encoding. default. getbytes (sinputstring); <br/> try <br/>{< br/> return system. convert. tobase64string (binput, 0, binput. length); <br/>}< br/> catch (system. argumentnullexc Eption) <br/>{< br/> // The binary array is null. <br/> return ""; <br/>}< br/> catch (system. argumentoutofrangeexception) <br/>{< br/> // insufficient length <br/> return ""; <br/>}</P> <p>/** // <summary> <br/> // decode the file. <br/> /// </Summary> <br/> /// <Param name = "sinputfilename"> input file </param> <br/> /// <Param name =" soutputfilename "> output file </param> <br/> Public void decryptfile (string sinputfilename, string soutputfil Ename) <br/>{< br/> system. io. streamreader infile; <br/> char [] base64chararray; </P> <p> try <br/>{< br/> infile = new system. io. streamreader (sinputfilename, <br/> system. text. encoding. ASCII); <br/> base64chararray = new char [infile. basestream. length]; <br/> infile. read (base64chararray, 0, (INT) infile. basestream. length); <br/> infile. close (); <br/>}< br/> catch <br/> {// (system. exception exp) {< Br/> return; <br/>}</P> <p> // convert base64 uuencoded to binary output. <br/> byte [] binarydata; <br/> try <br/> {<br/> binarydata = <br/> system. convert. frombase64chararray (base64chararray, <br/> 0, <br/> base64chararray. length); <br/>}< br/> catch (system. argumentnullexception) <br/>{< br/> // The 64-character array of base is null <br/> return; <br/>}< br/> catch (system. formatexception) <br/>{< br/> // The length is incorrect and cannot be divisible by 4 <br/> return; <Br/>}</P> <p> // write output data <br/> system. io. filestream OUTFILE; <br/> try <br/>{< br/> OUTFILE = new system. io. filestream (soutputfilename, <br/> system. io. filemode. create, <br/> system. io. fileaccess. write); <br/> OUTFILE. write (binarydata, 0, binarydata. length); <br/> OUTFILE. close (); <br/>}< br/> catch <br/> {// (system. exception exp) {<br/> // stream error <br/>}</P> <p>/** // <summary> <br/>/ // Encode the file <br/> /// </Summary> <br/> /// <Param name = "sinputfilename"> input file </param> <br/> /// <Param name = "soutputfilename"> output file </param> <br/> Public void encryptfile (string sinputfilename, string soutputfilename) <br/>{</P> <p> system. io. filestream infile; <br/> byte [] binarydata; </P> <p> try <br/>{< br/> infile = new system. io. filestream (sinputfilename, <br/> system. io. filemode. open, <br/> system. I O. fileaccess. read); <br/> binarydata = new byte [infile. length]; <br/> long bytesread = infile. read (binarydata, 0, <br/> (INT) infile. length); <br/> infile. close (); <br/>}< br/> catch <br/> {// (system. exception exp) {<br/> return; <br/>}</P> <p> // convert the binary input to base64 uuencoded output. <br/> // each 3 bytes is used as 4 bytes in the source data. <br /> long arraylength = (long) (4.0d/3.0d) * binarydata. length); </P> <p> // if it cannot be divisible by 4 <br/> if (Rraylength % 4! = 0) <br/>{< br/> arraylength + = 4-arraylength % 4; <br/>}</P> <p> char [] base64chararray = new char [arraylength]; <br/> try <br/>{< br/> system. convert. tobase64chararray (binarydata, <br/> 0, <br/> binarydata. length, <br/> base64chararray, <br/> 0); <br/>}< br/> catch (system. argumentnullexception) <br/>{< br/> // The binary array is null. <br/> return; <br/>}< br/> catch (system. argumentoutofrangeexception) <br/>{< br/> // insufficient length <br/> return; <br/>}</P> <p> // write uencoded data to the file. <br/> system. io. streamwriter OUTFILE; <br/> try <br/>{< br/> OUTFILE = new system. io. streamwriter (soutputfilename, <br/> false, <br/> system. text. encoding. ASCII); <br/> OUTFILE. write (base64chararray); <br/> OUTFILE. close (); <br/>}< br/> catch <br/> {// (system. exception exp) {<br/> // file stream error <br/>}</P> <p >}< br/>}</P> <p>}

Source: http://blog.csdn.net/beasyer/archive/2007/06/20/1660103.aspx

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.