Application of RC2 Encryption Algorithm in CSHARP

xiaoxiao2021-03-06  13

Using system.seircurity.cryptography; using system.text; using system.windows.form;

Namespace CURLON {Public Class CRYPT {///

/// Newly creates a file with a size of 10261b to write encrypted data to a fixed size file. /// /// file saved address, contains the file name 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, MessageBoxicon.Error);}} ///

/// Write a file after encrypted text data, of which This file is built with initbinfile. This file will be divided into ten pieces, // is used to save 10 groups of different data, the first Byte bit is retained, and the second bits to the 21st bits are used to store each block. The length of the data, but /// one value of the value is 0-127, so use two Byte to store one length. /// /// Text to encrypt /// The file to be written /// < Param Name = "DataIndex"> Write the first few pieces, takes 1--10 /// Whether to operate successfully 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) ;

Byte [] index = new byte [10261]; // write the read content to the BYTE array TmpFileStream.read (index, 0,10261); tmpfilestream.close (); // Define basic encryption conversion operation 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 /// to decrypt Decryption /// /// Decrypted text 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) BTE [] index = new byte [10261]; tmpfilestream.read (index, 0,10261); int startex = 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 = TextconvertR.getstring (Decrypted, 0, Decrypted.Length); TMPFileStream.close (); return r;} ///

/// Save a piece of text to a file /// // / Text data to encrypted /// The file you want to save /// is encrypted successfully 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 (); System.Security.Cryptography.CryptoStream csEncrypt = new CryptoStream (msEncrypt, Encryptor, CryptoStreamMode.Write) ; byte [] tmp = textConverter.GetBytes (toEncryptText); csEncrypt.Write (tmp, 0, tmp.Length); csEncrypt.FlushFinalBlock (); encrypted = msEncrypt.ToArray (); tmpFileStream.Write (encrypted, 0, encrypted. Length); tmpfilestream.flush (); r = true; tmpfilestream.close (); return r;} ///

///> Decryption //// //// < Param name = "filepath"> Deciplive file /// Decrypted text public static string decryptfromfile (string filepath) {string 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 rc2CS P = 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 [] tmp = new byte [tmpFileStream.Length]; tmpFileStream.Read (tmp, 0, tmp.Length); csDecrypt.Write (tmp, 0, tmp.Length); csDecrypt.FlushFinalBlock (); decrypted = msDecrypt.ToArray (); R = textconverter.getstring (Decrypted, 0, Decrypted.Length); TMPFileStream.close (); Return R;}}} See: RC2 encryption algorithm in C # application ---- Improvement Edition

转载请注明原文地址:https://www.9cbs.com/read-47218.html

New Post(0)