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 /// summary> /// Enter string param> /// password, may be a "" param> /// after outputs the encrypted string returns> static public string SEncryptString (string inputStr, string keyStr) {MySecurity ws = new MySecurity (); return ws.EncryptString (inputStr, Keystr);
/// /// encryption string /// summary> /// Enter string param> /// password, It may be a "" param> /// after outputs the encrypted string returns> 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 /// summary> /// Enter file path param> /// After encryption, output File path param> /// password, can be "" param> /// returns> 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 /// summary> /// String param> /// key param> /// the result of the decryption returns> public string DecryptString (string inputStr, string keyStr) {DESCryptoServiceProvider des = new DESCryptoServiceProvider (); if (keyStr == "") keyStr = key
/// /// Decryption file /// summary> /// Enter file path param> /// Decryption output File path param> /// password, can be "" param> /// returns> 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;}