RC2 encryption algorithm in CSHARP application ---- Improvement

xiaoxiao2021-03-06  15

Author Blog: http://blog.9cbs.net/curn ----- Learn each other

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

namespace Curllion {public class Crypt {private byte [] key; private byte [] iv; private System.Text.ASCIIEncoding asciiEncoding; private System.Text.UnicodeEncoding textConverter; private RC2CryptoServiceProvider rc2CSP; public Crypt () {InitializeComponent ();} private void InitializeComponent () {key = new byte [] {106,51,25,141,157,142,23,111,234,159,187,154,215,34,37,204}; iv = new byte [] {135,186,133,136,184,149,153,144}; asciiEncoding = new System.Text.ASCIIEncoding (); textConverter = new System .Text.unicodeEncoding (); RC2CSP = new rc2cryptoServiceProvider ();} ///

/// Newly built a file that is 10261b to write encrypted data to fixed-size files. /// /// file saved address, including file name public 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.fileaccess.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 data to encrypted /// File to write /////// Write the first few pieces, takes 1--10 /// Whether to operate successfully Public 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; // Open the file to be written, mainly in order to keep the original file does not lose system.io.filestream TmpFileStream = new 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 (this.key, this.iv); System.IO.MemoryStream msEncrypt = new MemoryStream (); // this stream encryption conversion, 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 string decryptfromfile (string filepath, int dataindex) {string r = ""; if (DataIndex> 10 && DataIndex <1) {messagebox.show (" The value of the data index ranges between 1 to 10! "," Error! ", MessageBoxButtons.ok, MessageBoxicon.Error; Return R; } Byte [] decrypted; 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 (this.key, this.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 (DataIndex-1) 21, TMP, 0, Count; csdecrypt.write; csdecrypt.flushfinalBlock (); decrypted = msdecrypt. ToArray (); r = textconverter.getstring (Decrypted, 0, Decrypted.Length); TmpFileStream.close (); Return R;} ///

/// Save a piece of text After saving to a file /// /// Text data to encrypted /// The file you want to save /// is encryption successful public bool EncryptToFile (string toEncryptText, string filePath) {bool r = false; byte [] encrypted; 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 (this.key, this.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;} ///

/// Decrypts an encrypted file //// //// File /// Decrypted text public string decryptfromfile (string filepath) {string r = ""; byte [] Decrypted; 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 (this.key, this.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;} // ------------------------------------------------------------------------------------------------------------ ----------------------------------------- ///

//// Write a file after encrypting text data, 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 Bits 21 bits are used to store the length of each data, but /// / one value of the value is 0-127, so, use two Byte to store a length. /// /// Text data to encrypted /// File to write /////// Write a few pieces, value 1--10 /// initialization vector ///

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 /// /// Initialization vector /// Decryption key /// Decrypted text public string decryptfromfile (string filepath, int dataindex, byte [] iv, byte [] key) {string r = ""; if (DataIndex> 10 && DataIndex <1) {MessageBox.s How ("data index ranges from 1 to 10! ","error! ", MessageBoxButtons.OK, MessageBoxIcon.Error); return r;} byte [] decrypted; 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 /// To save the file /// initialization vector /// Encrypted key /// Whether to encrypt success public bool encrypttofile (String toEncryptText, String Filepath, Byte [] IV, byte [] Key) {bool r = false; byte [] encrypted; 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;} ///

/// Decrypts an encrypted file //// //// File to decrypt /// Initialization vector /// Decryption key / // Decrypted text public string decryptfromfile (string filepath, byte [] iv, byte [] key) {string r = ""; byte [] decrypted; 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, C Ryptostreammode.write;

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

New Post(0)