C # RC4 algorithm, cryptographic decryption class

Source: Internet
Author: User
Tags ranges

RC4.

1 /*2 * Created by SharpDevelop. 3 * User: Yish4 * Date: 04/04/20155 * Time: 03:016  * 7 * To change this template click Tools | options | code Writing | Edit Standard header file8  */9 usingSystem;Ten  One namespaceLibraries A { -     /// <summary> -     ///Description of cryptography. the     /// </summary> -      Public classrc4crypt:idisposable{ -         byte[] S; -         byte[] T; +         byte[] K; -         byte[] k; +          PublicRc4crypt () {} A          PublicRc4crypt (byte[] key) { at              This. k=key; -         } -          Public byte[] Key -         { -             Get -             { in                 returnK; -             } to             Set +             { -K =value; the             } *         } $         //initializes the state vector s and temporary vector T for Keystream method invocationPanax Notoginseng         voidInitial () { -             if(S = =NULL|| T = =NULL) the             { +S =New byte[ the]; AT =New byte[ the]; the             } +              for(inti =0; I < the; ++i) { -S[i]= (byte) I; $T[i] = k[i%K.length]; $             } -         } -         //initial permutation state vector s, for Keystream method invocation the         voidranges () { -             intj=0;Wuyi              for(inti =0; I < the; ++i) { theJ= (J+s[i]+t[i]) &0xFF; -S[i]= (byte) ((S[i]+s[j]) &0xFF); WuS[j]= (byte) ((S[i]-s[j]) &0xFF); -S[i]= (byte) ((S[i]-s[j]) &0xFF); About             } $         } -         //generate a key stream -         //len: Len bytes for clear text -         voidKeystream (intLen) { A initial (); + ranges (); the             intI=0, j=0, t=0; -k=New byte[Len]; $              for(intR =0; R < Len; r++) { theI= (i+1) &0xFF; theJ= (J+s[i]) &0xFF; the                  theS[i]= (byte) ((S[i]+s[j]) &0xFF); -S[j]= (byte) ((S[i]-s[j]) &0xFF); inS[i]= (byte) ((S[i]-s[j]) &0xFF); the                  theT= (S[i]+s[j]) &0xFF; Aboutk[r]=S[t]; the             } the         } the          +          Public byte[] Encryptbyte (byte[] data) { -             //Production Key Stream the keystream (data. Length);Bayi              for(inti =0; I < data. Length; i++) { theK[i]= (byte) (data[i]^k[i]); the             } -             returnK; -         } the  the          Public byte[] Decryptbyte (byte[] data) { the             returnencryptbyte (data); the         } -  the         //is the collection complete ? the         BOOL_disposed; the          Public voidDispose ()94         { theDispose (true); theGc. SuppressFinalize ( This); the         }98~Rc4crypt () About         { -Dispose (false);101         }102         //The parameters here indicate whether the managed objects that implement the IDisposable interface need to be freed103         protected Virtual voidDispose (BOOLdisposing)104         { the             if(_disposed)return;//If it has been recycled, the execution is interrupted.106             if(disposing)107             {108                 //TODO: Releasing managed objects that implement the IDisposable interface109  the             }111             //TODO: Dispose of unmanaged resources, set object to null theS =NULL;113T =NULL; theK =NULL; theK =NULL; the_disposed =true;117         }118     }119}

C # RC4 algorithm, cryptographic decryption class

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.