Пример #1
0
 public ImageEncryptDecrypt(Bitmap current)
 {
     this.current = Convert(current);
     currentLock  = new LockBitmap(this.current);
 }
Пример #2
0
        public void Encrypt(string[] encFiles, string saveLocation)
        {
            try
            {
                currentLock.LockBits();

                List <BitArray> files = new List <BitArray>();
                foreach (string path in encFiles)
                {
                    BitArray fileArray = GetFileBits(path);
                    files.Add(fileArray);
                }

                int  requiredBytes = TotalBits(files);
                bool changeSize    = MaxBitContent < requiredBytes;
                if (changeSize)
                {
                    Size requiredSize = GetRequiredImageSize(requiredBytes);
                    current     = ResizeImage(requiredSize.Width, requiredSize.Height);
                    currentLock = new LockBitmap(current);
                }

                //Add parity and size bits
                int position = 0;
                Write(startSequenceArr, ref position);
                Write(BitConverter.GetBytes(files.Count), ref position);

                //Fill pixels with file data
                int pos = 0;
                foreach (BitArray file in files)
                {
                    int    lastDot   = encFiles[pos].LastIndexOf(".");
                    string extention = encFiles[pos].Substring(lastDot + 1).PadRight(3);
                    if (extention.Length > 3)
                    {
                        extention = extention.Substring(0, 3);
                    }
                    Write(Encoding.ASCII.GetBytes(extention), ref position);
                    Write(BitConverter.GetBytes(file.Length), ref position);
                    Write(file, ref position);
                }

                Bitmap     newImg     = new Bitmap(current.Size.Width, current.Size.Height, PixelFormat.Format32bppArgb);
                LockBitmap newImgLock = new LockBitmap(newImg);
                newImgLock.LockBits();
                for (int x = 0; x < current.Size.Width; x++)
                {
                    for (int y = 0; y < current.Size.Height; y++)
                    {
                        newImgLock.SetPixel(x, y, currentLock.GetPixel(x, y));
                    }
                }
                newImgLock.UnlockBits();
                newImg.Save(saveLocation);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                currentLock.UnlockBits();
            }
        }