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

📄 mainform.cs

📁 通过bmp图片格式进行数据加密
💻 CS
📖 第 1 页 / 共 2 页
字号:
				{
					MessageBox.Show("此文件为非加密文件,请重新选择!");
					r.Close();
					fsi.Close();
					return;
				}

				byte[] bDecode = new byte[bh.reserved];

				bTmp = new byte[4];
				for ( int nIndex = 0; nIndex < bDecode.Length; nIndex ++)
				{
					if ((i = r.Read(bTmp, 0, 4))> 0)
						bDecode[nIndex] = bTmp[3];
				}

				CodeText.Text = Encoding.UTF8.GetString(bDecode);

				r.Close();
				fsi.Close();

				MessageBox.Show("解密完成");
			}
			catch (Exception ep) 
			{
				// Let the user know what went wrong.
				logs("The file could not be read:");
				logs(ep.Message);
			}
		}

		private void EncodeBmp_Click(object sender, System.EventArgs e)
		{
			//ExtendBmp("test.bmp", "test_ext.bmp");
			//return;

			try 
			{
				string FILE_NAME = tbBmp.Text;
				string FILE_NAME2 = tbBmp.Text.Insert(tbBmp.Text.LastIndexOf(".bmp"), "_en");

				if (!File.Exists(FILE_NAME)) 
				{
					MessageBox.Show(String.Format("{0} file not exists!", FILE_NAME));
					return;
				}

				FileStream fsi = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
				BinaryReader r = new BinaryReader(fsi);
				// Read data from Test.bmp.

				int i;
				byte [] bTmp = new byte[14];

				if ((i=r.Read(bTmp, 0, 14)) != 14)
				{
					MessageBox.Show("读取文件头错误");
					return;
				}

				bmpHeader bh = new bmpHeader(bTmp);

				bTmp = new byte[40];

				if ((i=r.Read(bTmp, 0, 40)) != 40)
				{
					MessageBox.Show("读取文件头错误");
					return;
				}
				bmpInfoHeader bih = new bmpInfoHeader(bTmp);

				//加密流程
				byte[] bEncode = Encoding.UTF8.GetBytes(CodeText.Text);

				//检查加密文件长度
				if (((bh.FileSize - 54) / 3) < bEncode.Length)
				{	
					MessageBox.Show("要加密文件过长,请重新选择文件载体(bmp文件)!");
					r.Close();
					fsi.Close();
					return;
				}
				//检查文件是否存在
				if (File.Exists(FILE_NAME2)) 
				{
					if (DialogResult.Yes == MessageBox.Show(this, String.Format("{0} file is exists!\ndelete it?", FILE_NAME2), "警告", MessageBoxButtons.YesNo))
						File.Delete(FILE_NAME2);
					else
					{
						r.Close();
						fsi.Close();
						return;
					}
				}
				//r.BaseStream.Length
				if (bih.BitCount == 24)
				{
					FileStream fso = new FileStream(FILE_NAME2, FileMode.Create, FileAccess.Write);
					BinaryWriter w = new BinaryWriter(fso);
					bh.FileSize = bh.FileSize + ((bh.FileSize - 54) / 3);
					bh.reserved = (uint)bEncode.Length;
					bih.BitCount = 32;
					//bih.ColorsImportant = 32;
					w.Write(bh.ToArray());
					w.Write(bih.ToArray());
					bTmp = new byte[4];
					int nIndex = 0;
					
					while ((i = r.Read(bTmp, 0, 3))> 0)
					{
						if ( i == 3 && bEncode.Length > nIndex )
							bTmp[3] = bEncode[nIndex];
						else
							bTmp[3] = 0;
						w.Write(bTmp);
						nIndex ++;
					}
					w.Close();
					fso.Close();
				}
				else if (bih.BitCount == 32)
				{
					FileStream fso = new FileStream(FILE_NAME2, FileMode.Create, FileAccess.Write);
					BinaryWriter w = new BinaryWriter(fso);
					//bh.FileSize = bh.FileSize + ((bh.FileSize - 54) / 3);
					bh.reserved = (uint)bEncode.Length;
					bih.BitCount = 32;
					//bih.ColorsImportant = 32;
					w.Write(bh.ToArray());
					w.Write(bih.ToArray());
					bTmp = new byte[4];
					int nIndex = 0;
					
					while ((i = r.Read(bTmp, 0, 4))> 0)
					{
						if ( i == 4 && bEncode.Length > nIndex )
							bTmp[3] = bEncode[nIndex];
						else
							bTmp[3] = 0;
						w.Write(bTmp);
						nIndex ++;
					}
					w.Close();
					fso.Close();
				
				}
				r.Close();
				fsi.Close();

				MessageBox.Show("加密完成");
			}
			catch (Exception ep) 
			{
				// Let the user know what went wrong.
				logs("The file could not be read:");
				logs(ep.Message);
			}


		
		}

		/// <summary>
		/// 函数定义
		/// </summary>
		public static byte [] b2b(byte [] Value, int index, int len)
		{
			byte [] bTmp = new byte[len];
			for (int i = 0; i < len; i++)
				bTmp[i] = Value[i+index];
			return bTmp;
		}

		private void OpenBmp_Click(object sender, System.EventArgs e)
		{
			tbBmp.Text = "";
			EncodeBmp.Enabled = false;
			DecodeBmp.Enabled = false;
			if (DialogResult.OK == oFDialogBmp.ShowDialog())
			{
				if (oFDialogBmp.FileName == "" || oFDialogBmp.FileName == null)
				{
					MessageBox.Show("请选定文件");
					return;
				}
				tbBmp.Text = oFDialogBmp.FileName;
				EncodeBmp.Enabled = true;
				DecodeBmp.Enabled = true;



			}
		}
	}


	class bmpHeader
	{
		public bmpHeader(byte [] Value)
		{
			if (Value.Length != 14)
			{
				MessageBox.Show("bmp header format error!");
				return;
			}
			Buffer.BlockCopy(Value, 0, Signature, 0, 2);
			FileSize = BitConverter.ToUInt32(MainForm.b2b(Value, 2, 4), 0);
			reserved = BitConverter.ToUInt32(MainForm.b2b(Value, 6, 4), 0);
			DataOffset = BitConverter.ToUInt32(MainForm.b2b(Value, 10, 4), 0);
		}
		public byte [] Signature = new byte[2];	// 2 bytes 'BM' 
		public uint FileSize;	// 4 bytes File size in bytes 
		public uint reserved;	// 4 bytes unused (=0) 
		public uint DataOffset;	// 4 bytes File offset to Raster Data 

		public byte [] ToArray()
		{
			byte [] bTmp = new byte[14];
			Buffer.BlockCopy(Signature, 0, bTmp, 0, 2);
			Buffer.BlockCopy(BitConverter.GetBytes(FileSize), 0, bTmp, 2, 4);
			Buffer.BlockCopy(BitConverter.GetBytes(reserved), 0, bTmp, 6, 4);
			Buffer.BlockCopy(BitConverter.GetBytes(DataOffset), 0, bTmp, 10, 4);
			return bTmp;
		}
	}


	class bmpInfoHeader
	{
		public bmpInfoHeader(byte [] Value)
		{
			if (Value.Length != 40)
			{
				MessageBox.Show("bmp infoheader format error!");
				return;
			}
			Size = BitConverter.ToUInt32(MainForm.b2b(Value, 0, 4), 0);
			Width = BitConverter.ToUInt32(MainForm.b2b(Value, 4, 4), 0);
			Height = BitConverter.ToUInt32(MainForm.b2b(Value, 8, 4), 0);
			Planes = BitConverter.ToUInt16(MainForm.b2b(Value, 12, 2), 0);
			BitCount = BitConverter.ToUInt16(MainForm.b2b(Value, 14, 2), 0);
			Compression = BitConverter.ToUInt32(MainForm.b2b(Value, 16, 4), 0);
			ImageSize = BitConverter.ToUInt32(MainForm.b2b(Value, 20, 4), 0);
			XpixelsPerM = BitConverter.ToUInt32(MainForm.b2b(Value, 24, 4), 0);
			YpixelsPerM = BitConverter.ToUInt32(MainForm.b2b(Value, 28, 4), 0);
			ColorsUsed = BitConverter.ToUInt32(MainForm.b2b(Value, 32, 4), 0);
			ColorsImportant = BitConverter.ToUInt32(MainForm.b2b(Value, 36, 4), 0);
		}
		public uint Size;		// 4 bytes Size of InfoHeader =40  
		public uint Width;		// 4 bytes Bitmap Width 
		public uint Height;		// 4 bytes Bitmap Height 
		public ushort Planes;	// 2 bytes Number of Planes (=1) 
		public ushort BitCount;	// 2 bytes Bits per Pixel   
								//1 = monochrome palette. NumColors = 1   
								//4 = 4bit palletized. NumColors = 16   
								//8 = 8bit palletized. NumColors = 256  
								//16 = 16bit RGB. NumColors = 65536 (?)  
								//24 = 24bit RGB. NumColors = 16M 
		public uint Compression;	// 4 bytes Type of Compression   
								// 0 = BI_RGB   no compression   
								// 1 = BI_RLE8 8bit RLE encoding   
								// 2 = BI_RLE4 4bit RLE encoding 
		public uint ImageSize;		// 4 bytes (compressed) Size of Image  
									// It is valid to set this =0 if Compression = 0 
		public uint XpixelsPerM;	// 4 bytes horizontal resolution: Pixels/meter 
		public uint YpixelsPerM;	// 4 bytes vertical resolution: Pixels/meter 
		public uint ColorsUsed;		// 4 bytes Number of actually used colors 
		public uint ColorsImportant;	 //4 bytes Number of important colors  
										//0 = all 

		public byte [] ToArray()
		{
			byte [] bTmp = new byte[40];
			Buffer.BlockCopy(BitConverter.GetBytes(Size), 0, bTmp, 0, 4);
			Buffer.BlockCopy(BitConverter.GetBytes(Width), 0, bTmp, 4, 4);
			Buffer.BlockCopy(BitConverter.GetBytes(Height), 0, bTmp, 8, 4);
			Buffer.BlockCopy(BitConverter.GetBytes(Planes), 0, bTmp, 12, 2);
			Buffer.BlockCopy(BitConverter.GetBytes(BitCount), 0, bTmp, 14, 2);
			Buffer.BlockCopy(BitConverter.GetBytes(Compression), 0, bTmp, 16, 4);
			Buffer.BlockCopy(BitConverter.GetBytes(ImageSize), 0, bTmp, 20, 4);
			Buffer.BlockCopy(BitConverter.GetBytes(XpixelsPerM), 0, bTmp, 24, 4);
			Buffer.BlockCopy(BitConverter.GetBytes(YpixelsPerM), 0, bTmp, 28, 4);
			Buffer.BlockCopy(BitConverter.GetBytes(ColorsUsed), 0, bTmp, 32, 4);
			Buffer.BlockCopy(BitConverter.GetBytes(ColorsImportant), 0, bTmp, 36, 4);

			return bTmp;
		}

	}

}

⌨️ 快捷键说明

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