using System; using System.Security.Cryptography; using System.Text; using System.IO; using System.Windows.Forms; namespace Curllion {public class Crypt {/// /// to create a new file size 10261B To write encrypted data to a fixed size file. /// summary> /// file saved address, contains the file name param> public static void initbinfile (string filepath) {byte [] TMP = new byte [10261]; Try // Create a file stream, write its contents all 0 {System.IO.FileStream WriteFileStream = new filestream (filepath, system.io.filemode.create, system.io.filecess.write, system.io.fileshare.none , 512, False; FOR (int i = 0; i <10261; I ) TMP [I] = 0; WriteFileStream.write (TMP, 0, 10261); WriteFileStream.flush (); writeFileStream.close ();} Catch (system.io.ioException) {MessageBox.show ("File Operation Error!", "Error!", MessageBoxButtons.ok, MessageBoxIn.Error);}} /// /// After encryption of text data Write a file, where this file is built with initbinfile, this file will be divided into ten pieces, // is used to save 10 different data, the first Byte bit is retained, the second to 21st The length of each piece of data is used separately, but /// one value of the value is 0-127, so use two Byte to store one length.
/// summary> /// Text to encrypt param> /// The file to be written param> /// < Param Name = "DataIndex"> Write the first few pieces, takes 1--10 param> /// Whether to operate successfully returns> Public Static Bool Encrypttoft (String toEncryptText, String Filepath, Int DataIndex {BOOL R = false; if (DataIndex> 10 && DataInDex <1) {MessageBox.show ("Data index ranges between 1 to 10 Between 1 to 10!", "Error!", MessageBoxButtons.ok, MessageBoxicon.Error ); return r;} byte [] encrypted; // initialization vector byte [] key = {106,51,25,141,157,142,23,111,234,159,187,154,215,34,37,204}; // key byte [] IV = {135,186,133,136,184,149,153,144}; // Create UTF-16 encoded, for between byte [] and the string conversion System.Text.UnicodeEncoding textConverter = new UnicodeEncoding (); // Create RC2 service RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider (); // open the file for writing, Mainly in order to maintain the content of the original file is not lost in System.IO.FileStream (FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.Fileshare.none, 1024, True) ; B Yte [] index = new byte [10261]; // written to the content array TmpFileStream.read (INDEX, 0, 10261); TMPFileStream.close (); // Define Basic Encryption Conversion System.security .Cryptography.ICryptoTransform Encryptor = rc2CSP.CreateEncryptor (key, IV); System.IO.MemoryStream msEncrypt = new MemoryStream (); // this encrypted after conversion stream from CSEncrypt encryption, encrypted, in the result msEncrypt stream. System.Security.cryptography.cryptostream csencrypt = new cryptostream (msencrypt, encryptor, cryptostreammode.write); // The text to be encrypted into UTF-16 encoding, saved in the TMP array.
Byte [] tmp = textconverter.getbytes (toEncryptText); // Enter the TMP CSENCRYPT will be encrypted via Encryptor. CSencrypt.write (TMP, 0, Tmp.Length); // Output to msenctypt csencrypt.flushfinalBlock (); // Transfer stream to Byte [] encrypted = msencrypt.toarray (); if (Encrypted.Length> 1024) {MessageBox .Show ("After encryption, the data length is greater than 1kb, no saving"); Return False;} // Get the size of the data after encryption, the result exists the result.
Index [DataIndex * 2 - 1] = Convert.TOBYTE (Convert.TOString (Encrypted.Length / 128)); Index [DataIndex * 2] = Convert.TOBYTE (Convert.TOString (Encrypted.Length% 128)); // Write the encrypted result into INDEX FOR (INT i = 0; I /// from a file, in which this file is built by initbinfile, And encrypted by EncryPttOfile> Document param> /// to decrypt parameters> Decryption PAC> /// /// Decrypted text returns> public static string decryptfromfile (string filepath, int dataindex) {string r = ""; if (DataIndex> 10 && DataIndex <1) {MessageBox.show "The value of the data index ranges from 1 to 10!", "Error!", MessageBoxButtons.ok, MessageBoxicon.Error; Retu rn r;} byte [] decrypted; byte [] key = {106,51,25,141,157,142,23,111,234,159,187,154,215,34,37,204}; byte [] IV = {135,186,133,136,184,149,153,144}; System.Text.UnicodeEncoding textConverter = new UnicodeEncoding (); RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider (); System.IO.FileStream tmpFileStream = new FileStream (filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None, 1024, true); System. Security.cryptography.icryptotransform Decryptor =
rc2CSP.CreateDecryptor (key, IV); System.IO.MemoryStream msDecrypt = new MemoryStream (); System.Security.Cryptography.CryptoStream csDecrypt = new CryptoStream (msDecrypt, Decryptor, CryptoStreamMode.Write); byte [] index = new byte [ 10261]; TMPFILESTREAM.READ (Index, 0,10261); int StartIndex = 1024 * (DataIndex-1) 21; int count = index [dataindex * 2 - 1] * 128 index [dataindex * 2]; Byte [ ] TMP = new byte [count]; Array.copy (Index, 1024 * (DataIndex-1) 21, TMP, 0, Count; csdecrypt.write (TMP, 0, count); csdecrypt.flushfinalBlock (); Decrypted = msdecrypt.toArray (); r = textconverter.getstring (decrypted, 0, decrypted.Length); TmpFileStream.close (); return r;} /// /// Save a piece of text to a file /// summary> /// Text data to encrypted param> /// File to save param> /// < RETURNS> Whether encrypted success returns> public static bool encrypttofile (string toencrypttext, string filepath) {bool r = false; Byte [] encrypted; byte [] key = {106, 51, 25, 141 , 157,142,23,111,234,159,187,154,215,34,37,204}; byte [] IV = {135,186,133,136,184,149,153,144}; System.Text.UnicodeEncoding textConverter = new UnicodeEncoding (); RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider (); System.IO.FileStream tmpFileStream = new FileStream (filePath , System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.None, 1024, true); System.Security.Cryptography.ICryptoTransform Encryptor = rc2CSP.CreateEncryptor (key, IV); System. IO.MemoryStream msencrypt = new memoryStream ();