Exemplo n.º 1
0
        int ReadMessageLength(Stream stream)
        {
            // Asignamos el tamaño del mensaje
            switch (_HeaderLength)
            {
            case 1: return(stream.ReadByte());

            case 2:
            {
                byte[] bxf = new byte[_HeaderLength];
                ReadFull(stream, bxf, 0, _HeaderLength);
                return(BitConverterHelper.ToUInt16(bxf, 0));
            }

            case 3:
            {
                byte[] bxf = new byte[_HeaderLength];
                ReadFull(stream, bxf, 0, _HeaderLength);
                return((int)BitConverterHelper.ToUInt24(bxf, 0));
            }

            case 4:
            {
                byte[] bxf = new byte[_HeaderLength];
                ReadFull(stream, bxf, 0, _HeaderLength);
                return(BitConverterHelper.ToInt32(bxf, 0));
            }

            default: throw (new ProtocolException());
            }
        }
Exemplo n.º 2
0
        bool parse(string line, int filedIdLength, int packetNumLength, out string fileId, out packet packet)
        {
            fileId = "";
            packet = null;

            if (string.IsNullOrEmpty(line))
            {
                return(false);
            }

            try
            {
                Match m = Regex.Match(line, RegexData);
                if (m == null || !m.Success)
                {
                    return(false);
                }

                string dom;
                string data = m.Value.Trim();
                StringHelper.Split(data, '.', out data, out dom);

                if (filedIdLength > 0)
                {
                    fileId = data.Substring(0, filedIdLength);
                    if (string.IsNullOrEmpty(fileId))
                    {
                        return(false);
                    }
                }

                int packetNum = 0;
                if (packetNumLength > 0)
                {
                    packetNum = BitConverterHelper.ToInt32(HexHelper.FromHexString(data.Substring(filedIdLength, packetNumLength)), 0);
                }

                packet = new packet()
                {
                    Data = HexHelper.FromHexString(data.Remove(0, filedIdLength + packetNumLength)), Order = packetNum
                };
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Exemplo n.º 3
0
        public override bool Run()
        {
            if (!LocalFileRead.Exists)
            {
                return(false);
            }

            AESHelper aes = AESHelper.Create(this);

            if (aes != null)
            {
                WriteInfo("Using AES Encryption");
            }
            else
            {
                WriteError("Read/Write in RawMode (without any Encryption)");
            }

            using (Bitmap img = (Bitmap)Image.FromFile(LocalFileRead.FullName))
            {
                switch (Mode)
                {
                case EMode.Write:
                {
                    if (!LocalFileWrite.Exists)
                    {
                        WriteError("In this mode LocalFileWrite must exists, and will be replaced with the result image");
                        return(false);
                    }
                    WriteInfo("Start reading file");

                    byte[] data = File.ReadAllBytes(LocalFileWrite.FullName);
                    if (aes != null)
                    {
                        data = aes.Encrypt(data);
                    }

                    byte[] header    = BitConverterHelper.GetBytesInt32(data.Length);
                    int    totalSize = data.Length + header.Length;

                    int av = CalculateMaxLength(img.Width, img.Height);

                    WriteInfo("Bytes to encode", GetSize(totalSize), ConsoleColor.Green);
                    WriteInfo("Bytes available", GetSize(av), ConsoleColor.DarkCyan);

                    if (totalSize <= av)
                    {
                        WriteInfo("Its viable!");
                    }
                    else
                    {
                        WriteError("You need a image more larger or a message more shorter ... sorry :(");
                        return(false);
                    }

                    // crear array binario
                    StringBuilder binary = new StringBuilder();

                    for (int x = 0, m = header.Length; x < m; x++)
                    {
                        binary.Append(Convert.ToString(header[x], 2).PadLeft(8, '0'));
                    }

                    for (int x = 0, m = data.Length; x < m; x++)
                    {
                        binary.Append(Convert.ToString(data[x], 2).PadLeft(8, '0'));
                    }

                    char[] sb  = binary.ToString().ToCharArray();
                    int    sbl = sb.Length;
                    binary.Clear();

                    // Cadena binaria creada
                    int  width   = img.Width;
                    int  height  = img.Height;
                    bool toLower = true;

                    WriteInfo("Start writing image");
                    StartProgress(width * height);

                    byte r, g, b;
                    int  index = 0, current = 0;
                    for (int x = 0; x < width; x++)
                    {
                        for (int y = 0; y < height; y++)
                        {
                            r = GetBinary(sb, ref index, sbl);
                            g = GetBinary(sb, ref index, sbl);
                            b = GetBinary(sb, ref index, sbl);

                            Color clr = img.GetPixel(x, y);

                            clr = SetColor(clr, r, g, b, toLower);
                            img.SetPixel(x, y, clr);

                            current++;
                            WriteProgress(current);
                        }
                    }

                    EndProgress();

                    WriteInfo("Writing output");
                    img.Save(LocalFileWrite.FullName, ImageFormat.Png);
                    break;
                }

                case EMode.Read:
                {
                    WriteInfo("Start reading image");

                    int width  = img.Width;
                    int height = img.Height;
                    int av     = CalculateMaxLength(width, height);

                    StartProgress(width * height);

                    byte[] data   = null;
                    byte[] header = new byte[4];

                    string binary = "";

                    int  dataLength   = 0;
                    int  dataIndex    = 0;
                    int  headerReaded = 0;
                    byte b;
                    int  current = 0;
                    for (int x = 0; x < width; x++)
                    {
                        for (int y = 0; y < height; y++)
                        {
                            Color clr = img.GetPixel(x, y);

                            if (Append(ref binary, out b, clr.R % 2 == 0, clr.G % 2 == 0, clr.B % 2 == 0))
                            {
                                if (headerReaded < 4)
                                {
                                    header[headerReaded] = b;
                                    headerReaded++;

                                    if (headerReaded == 4)
                                    {
                                        dataLength = BitConverterHelper.ToInt32(header, 0);

                                        if (dataLength > av)
                                        {
                                            EndProgress();

                                            WriteInfo("Image maybe contains", GetSize(dataLength), ConsoleColor.Green);
                                            WriteError("Max bytes available " + GetSize(av));
                                            return(false);
                                        }

                                        data = new byte[dataLength];
                                    }
                                }
                                else
                                {
                                    data[dataIndex] = b;
                                    dataIndex++;
                                    if (dataIndex >= dataLength)
                                    {
                                        x = width + 1;
                                        break;
                                    }
                                }
                            }

                            current++;
                            WriteProgress(current);
                        }
                    }

                    EndProgress();

                    if (aes != null)
                    {
                        WriteInfo("Start decrypting file", GetSize(data.Length), ConsoleColor.Green);
                        data = aes.Decrypt(data);
                    }

                    if (data == null)
                    {
                        WriteInfo("Error decrypting file");
                        return(false);
                    }

                    WriteInfo("Writing output", GetSize(data.Length), ConsoleColor.Green);
                    File.WriteAllBytes(LocalFileWrite.FullName, data);

                    break;
                }
                }
            }

            return(true);
        }