My characters and file encryption classes, 128-bit encryption

xiaoxiao2021-03-06  80

Using system.semburity; using system.security.cryptography; using system.ipe; using system.text;

Namespace MySecurity {public class mysecurity {public mysecurity () {// // Todo: Add constructor logic //} private string key = "1234567890"; // Default key

Private Byte [] SKEY; Private Byte [] SIV;

///

/// encryption string /// /// Enter string /// password, may be a "" /// after outputs the encrypted string static public string SEncryptString (string inputStr, string keyStr) {MySecurity ws = new MySecurity (); return ws.EncryptString (inputStr, Keystr);

///

/// encryption string /// /// Enter string /// password, It may be a "" /// after outputs the encrypted string public string encryptString (string inputStr, string keyStr) {DESCryptoServiceProvider des = new DESCryptoServiceProvider (); if (keyStr == "") keyStr = key; byte [] inputByteArray = Encoding.Default.GetBytes (inputStr); byte [] keyByteArray = Encoding.Default.GetBytes (keyStr); SHA1 ha = new SHA1Managed (); byte [] hb = ha.ComputeHash (keyByteArray ); SKey = new byte [8]; SIV = new byte [8]; for (int i = 0; i <8; i ) SKEY [I] = HB [I]; for (int i = 8; i < 16; I ) SIV [I-8] = HB [I]; des.key = SKEY; DES.IV = SIV; MemoryStream Ms = new memoryStream (); cryptostream cs = new cryptostream (ms, des.createEncryptor (), Cryptostreammode.write; cs.write (InputByteaRray, 0, InputByteaRray.Length); cs.flushfinalblock (); stringbuilder return = new string BUILDER (); BYTE B IN ms.toarray ()) {ret.Appendformat ("{0: x2}", b);} cs.close (); ms.close (); return ret.tostring () }

///

/// encryption file /// /// Enter file path /// After encryption, output File path /// password, can be "" /// public bool encryptfile (String Filepath, String SavePath, String Keystr) {DESCryptoServiceProvider des = new DESCryptoServiceProvider (); if (keyStr == "") keyStr = key; FileStream fs = File.OpenRead (filePath); byte [] inputByteArray = new byte [fs.Length]; fs.Read (inputByteArray, 0, (int) fs.length); fs.close (); byte [] keybyteaRray = encoding.default.getBytes (keyStr); sha1 ha = new sha1managed (); byte [] hb = ha.computehash (keybyteaRray); SKEY = New Byte [8]; SIV = New Byte [8]; for (int i = 0; i <8; i ) SKEY [I] = HB [I]; for (int i = 8; i <16; I ) SIV [I-8] = HB [I]; des.key = SKEY; DES.IV = SIV; MemoryStream Ms = New MemoryStream (); Cryptostream Cs = New CryptostReam (MS, DES.CREATEENCRYPTOR (), Cryptostre ammode.write; cs.write (InputByteArray, 0, InputBytearray.Length); cs.flushfinalblock (); fs = file.openwrite (savePath); foreach (byte b in ms.toarray ()) {fs.writebyte (B ); Fs.close (); cs.close (); ms.close (); return true;}

///

/// Decryption string /// /// String /// key /// the result of the decryption public string DecryptString (string inputStr, string keyStr) {DESCryptoServiceProvider des = new DESCryptoServiceProvider (); if (keyStr == "") keyStr = key

///

/// Decryption file /// /// Enter file path /// Decryption output File path /// password, can be "" /// public bool decryptfile (String Filepath, String SavePath, String Keystr) {DESCryptoServiceProvider des = new DESCryptoServiceProvider (); if (keyStr == "") keyStr = key; FileStream fs = File.OpenRead (filePath); byte [] inputByteArray = new byte [fs.Length]; fs.Read (inputByteArray, 0, (int) fs.length); fs.close (); byte [] keybyteaRray = encoding.default.getBytes (keyStr); sha1 ha = new sha1managed (); byte [] hb = ha.computehash (keybyteaRray); SKEY = New Byte [8]; SIV = New Byte [8]; for (int i = 0; i <8; i ) SKEY [I] = HB [I]; for (int i = 8; i <16; I ) SIV [I-8] = HB [I]; des.key = SKEY; DES.IV = SIV; MemoryStream MS = New MemoryStream (); Cryptostream Cs = New CryptostReam (MS, DES.CREATEDECRYPTOR (), Cryptost REAMMODE.WRITE); CS.WRITE (INPUTBYTEARRAY, 0, INPUTBYTEARRAY.LENGTH); cs.flushfinalblock (); fs = file.openwrite (SavePath); foreach (byte b in ms.toarray ()) {fs.writebyte (B ); Fs.close (); cs.close (); ms.close (); return true;}

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

New Post(0)