⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aea.cs

📁 此工程实现了对文件
💻 CS
字号:
using System;
using System.Security.Cryptography;
using  System.IO;
using System.Text;

using System.Management;
using System.Runtime.InteropServices;

using System.Data; 

namespace WindowsApplication1.cipher
{
	/// <summary>
	/// Class1 的摘要说明。
	/// </summary>
	public class Aes
	{
		public enum KeySize { Bits128, Bits192, Bits256 }; //keySize
		private SymmetricAlgorithm sa;
		//private byte[] cipherBytes;
		public Aes()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
			sa = Rijndael.Create();
		}

		public byte[] GetKey(String key)
		{
			String sTemp;
			int keyLength = key.Length;
			if (keyLength <= 32) //check if keySize is valid 
			{
				if (sa.LegalKeySizes.Length > 0)
				{
					int lessSize = 0;
					int moreSize = sa.LegalKeySizes[0].MinSize;
					// key sizes are in bits
					while (key.Length * 8 > moreSize)
					{
						lessSize = moreSize;
						moreSize += sa.LegalKeySizes[0].SkipSize;
					}
					sTemp = key.PadRight(moreSize / 8, ' ');
				}
				else
					sTemp = key;
				// convert the secret key to byte array
				return ASCIIEncoding.ASCII.GetBytes(sTemp);
			}
			return null;
		}




		public String EncryptText(String key, String text) 
		{
			byte[] plainBytes = 
				Encoding.UTF8.GetBytes(text);
			byte[] cipherBytes = this.EncryptBytes(key, plainBytes);
			return Encoding.Unicode.GetString(cipherBytes);
		}
		public byte[] EncryptTextByte(String key, String text) 
		{
			byte[] plainBytes = 
				Encoding.UTF8.GetBytes(text);
			byte[] cipherBytes = this.EncryptBytes(key, plainBytes);
			return cipherBytes;
		}


		public String DecryptText(String key, String text)
		{
			byte[] cipherBytes = Encoding.Unicode.GetBytes(text);
			byte[] plainBytes = new Byte[cipherBytes.Length];
            plainBytes = this.DecryptBytes(key, cipherBytes);
			return Encoding.UTF8.GetString(plainBytes);
		}
		public byte[] DecryptTextByte(String key, String text)
		{
			byte[] cipherBytes = Encoding.Unicode.GetBytes(text);
			byte[] plainBytes = new Byte[cipherBytes.Length];
			plainBytes = this.DecryptBytes(key, cipherBytes);
			return plainBytes;
		}



		public void EncryptFile(byte[] Key, String input, String output) 
		{
			FileStream fsInput = new FileStream(input,
				FileMode.Open,
				FileAccess.Read);

			FileStream fsEncrypted = new FileStream(output,
				FileMode.Create,
				FileAccess.Write);

			byte []toEncryptBytes = new byte[fsInput.Length];

			fsInput.Read(toEncryptBytes, 0, (int)fsInput.Length);
			fsInput.Close();

			byte[] result = EncryptBytes(Key, toEncryptBytes); 
 
			//write file
			BinaryWriter w = new BinaryWriter(fsEncrypted);
			w.Write(result);
			w.Flush();
			w.Close();
		}
		public byte[] EncryptFile(byte[] Key, String input) 
		{
			FileStream fsInput = new FileStream(input,
				FileMode.Open,
				FileAccess.Read);

			byte []toEncryptBytes = new byte[fsInput.Length];

			fsInput.Read(toEncryptBytes, 0, (int)fsInput.Length);
			fsInput.Close();

			byte[] result = EncryptBytes(Key, toEncryptBytes); 
			return result;
		}

		public void DecryptFile(byte[] Key, String input, String output)
		{		
			FileStream fsread = new FileStream(input,
				FileMode.Open,
				FileAccess.Read);
			FileStream outStream = new FileStream(output,
				FileMode.Create,
				FileAccess.Write);

            byte []toDecryptBytes = new byte[fsread.Length];

			fsread.Read(toDecryptBytes, 0, (int)fsread.Length);
			fsread.Close();

			//decrypt
            byte[] result = DecryptBytes(Key, toDecryptBytes);       

			//write file
			BinaryWriter w = new BinaryWriter(outStream);
			w.Write(result);
			w.Flush();
			w.Close();			

		}
		public byte[] DecryptFile(byte[] Key, String input)
		{		
			FileStream fsread = new FileStream(input,
				FileMode.Open,
				FileAccess.Read);

			byte []toDecryptBytes = new byte[fsread.Length];

			fsread.Read(toDecryptBytes, 0, (int)fsread.Length);
			fsread.Close();

			//decrypt
			byte[] result = DecryptBytes(Key, toDecryptBytes);       
			return result;

		}

		//important
		public byte[] EncryptBytes(String key, byte[] toEncryptBytes) 
		{
			byte[] keyBytes = this.GetKey(key);
			if (key == null) return null;
			return this.EncryptBytes(keyBytes, toEncryptBytes);
		}
		public byte[] DecryptBytes(String key, byte[] toDecryptBytes) 
		{
			byte[] keyBytes = this.GetKey(key);
			if (key == null) return null;
			return this.DecryptBytes(keyBytes, toDecryptBytes);
		}


		public byte[] EncryptBytes(byte[] keyBytes, byte[] toEncryptBytes) 
		{
			if (!sa.ValidKeySize(keyBytes.Length * 8)) return null;

			this.sa.Key = keyBytes;
			this.sa.IV = keyBytes;
			MemoryStream ms = new MemoryStream();
			CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
			cs.Write(toEncryptBytes,0,toEncryptBytes.Length);			
			cs.Close();
			byte[] cipherBytes = ms.ToArray();
			return cipherBytes;
		}
		public byte[] DecryptBytes(byte[] keyBytes, byte[] toDecryptBytes) 
		{
			if (toDecryptBytes.Length == 0)
				return null;
			if (!sa.ValidKeySize(keyBytes.Length * 8)) return null;

			this.sa.Key = keyBytes;
			this.sa.IV = keyBytes;

			MemoryStream ms = new MemoryStream(toDecryptBytes);			
			CryptoStream cs = new CryptoStream(ms, sa.CreateDecryptor(), CryptoStreamMode.Read);
			byte[] plainBytes = new Byte[toDecryptBytes.Length];
			cs.Read(plainBytes,0,plainBytes.Length);
			ms.Close();
			cs.Close();
			return plainBytes;
		}
	}



	public class RSFormator 
	{
		String key;
		public RSFormator() 
		{

		}
		public String GeneratKey() 
		{
			return GetCpuID() + GetRandomNum(16);
		}
		public String GetCpuID() 
		{
			try
			{
				ManagementClass mc = new ManagementClass("Win32_Processor");
				ManagementObjectCollection moc = mc.GetInstances();
      
				String strCpuID = null ;
				foreach( ManagementObject mo in moc ) 
				{
					strCpuID = mo.Properties["ProcessorId"].Value.ToString();
					break; 
				}
				return strCpuID;
			}
			catch
			{
				return null;
			}
		}
		public String GetRandomNum(int length) 
		{
			if (length <= 0) return null;
			StringBuilder sb = new StringBuilder();
			DateTime dt = new DateTime(); 
			dt = DateTime.Now;
            Random rnd = new Random(dt.Millisecond);
			for (int i = 0;i < length;i++) 
			{
				sb.Append(rnd.Next(0,9).ToString());
			}
			return sb.ToString();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -