Пример #1
0
 public NFSAttributes(int cdateTime, int adateTime, int mdateTime, NFSItemTypes type, NFSPermission mode, long size, Byte[] handle)
 {
     this._cdateTime = new System.DateTime(1970, 1, 1).AddSeconds(cdateTime);
     this._adateTime = new System.DateTime(1970, 1, 1).AddSeconds(adateTime);
     this._mdateTime = new System.DateTime(1970, 1, 1).AddSeconds(mdateTime);
     this._type = type;
     this._size = size;
     this._mode = mode;
     this._handle = (Byte[])handle.Clone();
 }
Пример #2
0
 /// <summary>
 /// Pads the bytes with zero byte padding scheme.
 /// </summary>
 public static Byte[] Pad(Byte[] bytes, Int32 size)
 {
     Byte[] c = (Byte[])bytes.Clone();
     while (c.Length % size != 0)
     {
         Array.Resize(ref c, c.Length + 1);
         c[c.Length - 1] = (Byte)0x00;
     }
     return c;
 }
 public void Distort(Byte[] plainText, out Byte[] distortedText)
 {
     int length = plainText.Length;
     distortedText = (Byte[])plainText.Clone();
     UInt32[] posDistortionSeq = m_GenerateDistortionSequence(length);
     for (int i = 0; i < length; ++i)
     {
         Utils.Swap(ref distortedText[i], ref distortedText[posDistortionSeq[i] % length]);
     }
 }
Пример #4
0
 /// <summary>
 /// Unpads the bytes with zero byte padding scheme.
 /// </summary>
 public static Byte[] Unpad(Byte[] bytes)
 {
     Byte[] c = (Byte[])bytes.Clone();
     Byte s = (Byte)c[c.Length - 1];
     while (s == (Byte)0x00)
     {
         s = (Byte)c[c.Length - 2];
         Array.Resize(ref c, c.Length - 1);
     }
     return c;
 }
Пример #5
0
 /// <summary>
 /// Pads the bytes with PKCS#7 padding scheme.
 /// </summary>
 public static Byte[] Pad(Byte[] bytes, Int32 size)
 {
     Byte[] c = (Byte[])bytes.Clone();
     Int32 s = size - c.Length % size;
     for (Int32 i = 0; i < s; i++)
     {
         Array.Resize(ref c, c.Length + 1);
         c[c.Length - 1] = (Byte)s;
     }
     return c;
 }
Пример #6
0
 /// <summary>
 /// Unpads the bytes with PKCS#7 padding scheme.
 /// </summary>
 public static Byte[] Unpad(Byte[] bytes)
 {
     Byte[] c = (Byte[])bytes.Clone();
     Byte s = (Byte)c[c.Length - 1];
     for (Int32 i = s; i > 0; i--)
     {
         Int32 v = c[c.Length - 1];
         Array.Resize(ref c, c.Length - 1);
         if (s != v)
         {
             String msg = String.Format(ERROR_VALUE, v, s);
             throw new Exception(msg);
         }
     }
     return c;
 }
Пример #7
0
        private void LoadImage(ref System.Byte[,] image, string fileName)
        {
            System.Int64 fileSize = new System.IO.FileInfo(fileName).Length;
            this.inH = this.inW = (int)Math.Sqrt(fileSize);

            image = new System.Byte[inH, inW];

            using (BinaryReader br = new BinaryReader(File.Open(fileName, FileMode.Open)))
            {
                for (int i = 0; i < inH; i++)
                {
                    for (int k = 0; k < inW; k++)
                    {
                        System.Byte pixel = br.ReadByte();
                        image[k, i] = pixel;
                    }
                }
            }

            outImage = (System.Byte[, ])image.Clone();
        }
Пример #8
0
 public override void Write(Byte[] buffer, Int32 offset, Int32 count)
 {
     _sslHandler.WriteBuffer(IoBuffer.Wrap((Byte[])buffer.Clone(), offset, count));
 }
Пример #9
0
        private int RemoveWord(ref Byte[] bts, string templateWord, int stopByte)
        {
            int startByte = Encoding.ASCII.GetString(bts, 0, bts.Length).IndexOf(templateWord, 0, stopByte);
            if (startByte < 0) return 0;

            int wordLength = templateWord.Length;
            var buffer = (byte[])bts.Clone();
            Array.Clear(bts, 0, bts.Length);
            Array.Copy(buffer, 0, bts, 0, startByte);
            Array.Copy(buffer, startByte + wordLength, bts, startByte, _pakSize - wordLength - startByte);
            return wordLength;
        }
        /// <summary>
        /// adjust the Saturation from input 'source' and output a Byte array
        /// </summary>
        public Byte[] adjustSaturationByteLevel(int value, Byte[] source)
        {
            //if (value == m_currentSaturation)
                //return source;
            double adjustment = (double)(value) / (100.0 * 2);
            Byte[] result = (Byte[])source.Clone();
            double min, max, delta;
            double r, g, b, h, s, v;
            int i;
            double f, p, q, t;

            for (int x = 0; x < ImageWidth; x++)
            {
                for (int y = 0; y < ImageHeight; y++)
                {
                            // change from RGB to HSV
                            r = source[4 * x + y * (4 * m_imageWidth) + 2] / 255.0;
                            g = source[4 * x + y * (4 * m_imageWidth) + 1] / 255.0;
                            b = source[4 * x + y * (4 * m_imageWidth) + 0] / 255.0;

                            min = Utils.min(r, g, b);
                            max = Utils.max(r, g, b);
                            v = max;				// v

                            delta = max - min;

                            if (max != 0)
                            {
                                s = delta / max;		// s
                                if (r == max)
                                    h = (g - b) / delta;		// between yellow & magenta
                                else if (g == max)
                                    h = 2 + (b - r) / delta;	// between cyan & yellow
                                else
                                    h = 4 + (r - g) / delta;	// between magenta & cyan
                                h *= 60;				// degrees
                                if (h < 0)
                                    h += 360;
                            }
                            else
                            {
                                // r = g = b = 0		// s = 0, v is undefined
                                s = 0;
                                h = -1;
                            }

                            // adjust saturation:
                            s += adjustment;
                            if (s > 1.0)
                                s = 1.0;
                            else if (s < 0.0)
                                s = 0.0;

                            // change back from HSV to RGB:
                            r = g = b = 0;

                            if (s == 0)
                            {
                                // achromatic (grey)
                                r = g = b = v;

                            }
                            else
                            {

                                h /= 60.0;			// sector 0 to 5
                                i = (int)h;
                                f = h - i;			// factorial part of h
                                p = v * (1 - s);
                                q = v * (1 - s * f);
                                t = v * (1 - s * (1 - f));

                                switch (i)
                                {
                                    case 0:
                                        r = v;
                                        g = t;
                                        b = p;
                                        break;
                                    case 1:
                                        r = q;
                                        g = v;
                                        b = p;
                                        break;
                                    case 2:
                                        r = p;
                                        g = v;
                                        b = t;
                                        break;
                                    case 3:
                                        r = p;
                                        g = q;
                                        b = v;
                                        break;
                                    case 4:
                                        r = t;
                                        g = p;
                                        b = v;
                                        break;
                                    case 5:		// case 5:
                                        r = v;
                                        g = p;
                                        b = q;
                                        break;
                                    case 6:
                                        r = v;
                                        g = p;
                                        b = q;
                                        break;
                                }
                            }

                            result[4 * x + y * (4 * m_imageWidth) + 2] = (byte)(r * 255.0);
                            result[4 * x + y * (4 * m_imageWidth) + 1] = (byte)(g * 255.0);
                            result[4 * x + y * (4 * m_imageWidth) + 0] = (byte)(b * 255.0);

                }
            }
            m_currentSaturation = value;
            return result;
        }
Пример #11
0
 /// <summary>
 /// Создает экземпляр класса
 /// </summary>
 /// <param name="salt">Синхропосылка</param>
 /// <param name="hash">Хэш пароля</param>
 /// <param name="hashAlgorithm">Алгоритм хэширования паролей</param>
 public Password(Byte[] salt, Byte[] hash, HashAlgorithm hashAlgorithm)
     : this(hashAlgorithm)
 {
     _salt = (Byte[])salt.Clone();
     _hash = (Byte[])hash.Clone();
 }
Пример #12
0
        ////////////////////////////////////////////////////////////////////
        // Create owner key
        ////////////////////////////////////////////////////////////////////
        private Byte[] CreateOwnerKey(
			Byte[] UserBinaryPassword,
			Byte[] OwnerBinaryPassword
			)
        {
            // create hash array for owner password
            Byte[] OwnerHash = MD5.ComputeHash(OwnerBinaryPassword);

            // loop 50 times creating hash of a hash
            for(Int32 Index = 0; Index < 50; Index++) OwnerHash = MD5.ComputeHash(OwnerHash);

            Byte[] ownerKey = (Byte[]) UserBinaryPassword.Clone();
            Byte[] TempKey = new Byte[16];
            for(Int32 Index = 0; Index < 20; Index++)
            {
            for(Int32 Tindex = 0; Tindex < 16 ; Tindex++) TempKey[Tindex] = (Byte)(OwnerHash[Tindex] ^ Index);
            EncryptRC4(TempKey, ownerKey);
            }

            // return encryption key
            return(ownerKey);
        }
Пример #13
0
        ////////////////////////////////////////////////////////////////////
        // Encrypt byte array
        ////////////////////////////////////////////////////////////////////
        internal Byte[] EncryptByteArray(
			Int32	ObjectNumber,
			Byte[]	PlainText
			)
        {
            // create encryption key
            Byte[] EncryptionKey = CreateEncryptionKey(ObjectNumber);
            Byte[] CipherText;

            if(EncryptionType == EncryptionType.Aes128)
            {
            MemoryStream OutputStream = null;
            CryptoStream CryptoStream = null;

            // generate new initialization vector IV
            AES.GenerateIV();

            // create cipher text buffer including initialization vector
            Int32 CipherTextLen = (PlainText.Length & 0x7ffffff0) + 16;
            CipherText = new Byte[CipherTextLen + 16];
            Array.Copy(AES.IV, 0, CipherText, 0, 16);

            // set encryption key and key length
            AES.Key = EncryptionKey;

            // Create the streams used for encryption.
            OutputStream = new MemoryStream();
            CryptoStream = new CryptoStream(OutputStream, AES.CreateEncryptor(), CryptoStreamMode.Write);

            // write plain text byte array
            CryptoStream.Write(PlainText, 0, PlainText.Length);

            // encrypt plain text to cipher text
            CryptoStream.FlushFinalBlock();

            // get the result
            OutputStream.Seek(0, SeekOrigin.Begin);
            OutputStream.Read(CipherText, 16, CipherTextLen);

            // release resources
            CryptoStream.Clear();
            OutputStream.Close();
            }
            else
            {
            CipherText = (Byte[]) PlainText.Clone();
            EncryptRC4(EncryptionKey, CipherText);
            }

            // return result
            return(CipherText);
        }
Пример #14
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="PacketBuffer"></param>

        private bool PacketCallBackFromReader( int readerIndex, Byte[ ] PacketBuffer )
        {
            uint myHandle = this._theReaderID._handle;
            Debug.Assert( readerIndex == myHandle );

            String errorMessage = null;
            PacketData.PacketBase packet = null;

            //!! TODO: fix output SysLogger.WriteToLog = ation != Library;

            PacketData.PacketType type = PacketData.ParsePacket( PacketBuffer, out packet, out errorMessage );
            SysLogger.WriteToLog = true;

            PacketData.PacketWrapper envelope = null;
            long elapsedSesionTime = ElapsedMilliseconds;

            if ( packet == null || type == PacketData.PacketType.U_N_D_F_I_N_E_D || errorMessage != null )
            {
                BadPacket badPacket = new BadPacket( );
#pragma warning disable 420 // reference to a volatile field is valid for Interlocked call
                badPacket.badPacketSequence = System.Threading.Interlocked.Increment( ref _commonBadIndex );
#pragma warning restore 420
                badPacket.packetTime = DateTime.UtcNow;

                badPacket.rawPacketData = PacketBuffer.Clone( ) as byte[ ];
                badPacket.errorMessage = errorMessage;
                using ( MemoryStream data = new MemoryStream( 256 ) )
                {
                    badPacket.WriteTo( data );
                    envelope = new PacketData.PacketWrapper( new PacketData.CommandPsuedoPacket( "BadPacket", data.ToArray( ) ), PacketData.PacketType.U_N_D_F_I_N_E_D );
                }
                envelope.IsPseudoPacket = true;
                envelope.PacketNumber = badPacket.PacketSequence;
                envelope.Timestamp = badPacket.packetTime;
                envelope.ReaderIndex = ( int ) _theReaderID.Handle;
                envelope.ReaderName = _theReaderID.Name;
                envelope.CommandNumber = _processedInventoryIndex;
                envelope.ElapsedTimeMs = elapsedSesionTime;
            }
            else
            {
                envelope = new PacketData.PacketWrapper( packet, type, PacketBuffer, _commonRequestIndex, elapsedSesionTime, readerIndex, Name );
                Debug.Assert( envelope.PacketType == type );
            }

            lock ( PacketQueue )
            {
                PacketQueue.Enqueue( envelope );
            }

#pragma warning disable 420 // reference to a volatile field is valid for Interlocked call
            int queueSize = Interlocked.Increment( ref _queueCount );
#pragma warning restore 420

            _maxQueueSize = queueSize > _maxQueueSize ? queueSize : _maxQueueSize;
            //if ( queueSize > MAX_QUEUE_SIZE )
            //{
            //    int loopCount = 0;
            //    while ( queueSize > TARGET_QUEUE_SIZE && loopCount < 1000 )
            //    {
            //        loopCount++;
            //        QueueEvent.Set( );

            //        Thread.Sleep( QUEUE_SLEEP_MS );
            //        queueSize = _queueCount;
            //    }
            //    // Write an informational entry to the event log.    
            //    SysLogger.LogMessage( String.Format( "Queue Size = {0}\nMax Queue Size = {1}\nSleep Count = {2}\nPacket Count = {3}", queueSize, _maxQueueSize, loopCount, ProcessedPacketCount ) );
            //}
            return FunctionController.GetActionRequest( ) != FunctionControl.RequestedAction.Abort;
        }
Пример #15
0
        public Int32 MyCallback
        (
            UInt32 bufferLength,
            IntPtr pBuffer,
            IntPtr context
        )
        {
            Byte[ ] packetBuffer = new Byte[ bufferLength ];

            Marshal.Copy( pBuffer, packetBuffer, 0, ( Int32 ) bufferLength );

            String errorMessage = null;
            PacketData.PacketBase packet = null;

            PacketData.PacketType type = PacketData.ParsePacket
            (
                packetBuffer,
                out packet,
                out errorMessage
            );

            SysLogger.WriteToLog = true;

            PacketData.PacketWrapper envelope = null;
            long elapsedSesionTime = ElapsedMilliseconds;

            if ( packet == null || type == PacketData.PacketType.U_N_D_F_I_N_E_D || errorMessage != null )
            {
                BadPacket badPacket = new BadPacket( );
#pragma warning disable 420 // reference to a volatile field is valid for Interlocked call
                badPacket.badPacketSequence = System.Threading.Interlocked.Increment( ref _commonBadIndex );
#pragma warning restore 420
                badPacket.packetTime = DateTime.UtcNow;

                badPacket.rawPacketData = packetBuffer.Clone( ) as byte[ ];
                badPacket.errorMessage = errorMessage;

                using ( MemoryStream data = new MemoryStream( 256 ) )
                {
                    badPacket.WriteTo( data );
                    envelope = new PacketData.PacketWrapper( new PacketData.CommandPsuedoPacket( "BadPacket", data.ToArray( ) ), PacketData.PacketType.U_N_D_F_I_N_E_D );
                }

                envelope.IsPseudoPacket = true;
                envelope.PacketNumber = badPacket.PacketSequence;
                envelope.Timestamp = badPacket.packetTime;
                envelope.ReaderIndex = ( int ) _theReaderID.Handle;
                envelope.ReaderName = _theReaderID.Name;
                envelope.CommandNumber = _processedInventoryIndex;
                envelope.ElapsedTimeMs = elapsedSesionTime;
            }
            else
            {
                envelope = new PacketData.PacketWrapper
                (
                    packet,
                    type,
                    packetBuffer,
                    _commonRequestIndex,
                    elapsedSesionTime,
                    ( int ) this.ReaderHandle,
                    Name
                );
                Debug.Assert( envelope.PacketType == type );
            }

            if ( VirtualReaderQueue != null )
            {
                lock ( VirtualReaderQueue )
                {
                    VirtualReaderQueue.Enqueue( envelope );
                }
            }

            lock ( PacketQueue )
            {
                PacketQueue.Enqueue( envelope );
            }

#pragma warning disable 420 // reference to a volatile field is valid for Interlocked call
            int queueSize = Interlocked.Increment( ref _queueCount );
#pragma warning restore 420

           
            _maxQueueSize = queueSize > _maxQueueSize ? queueSize : _maxQueueSize;
            if (queueSize > MAX_QUEUE_SIZE)
            {
                int loopCount = 0;
                while (queueSize > TARGET_QUEUE_SIZE && loopCount < 1000)
                {
                    loopCount++;
                    QueueEvent.Set();

                    Thread.Sleep(QUEUE_SLEEP_MS);
                    queueSize = _queueCount;
                }
                // Write an informational entry to the event log.    
                SysLogger.LogMessage(String.Format("Queue Size = {0}\nMax Queue Size = {1}\nSleep Count = {2}\nPacket Count = {3}", queueSize, _maxQueueSize, loopCount, ProcessedPacketCount));
            }
            return FunctionController.GetActionRequest( ) != FunctionControl.RequestedAction.Abort ? 0 : 1;
        }
Пример #16
0
        //*************************************************************************
        //  Method: BytesToPrintableAscii
        //
        /// <summary>
        /// Converts an array of bytes to a printable ASCII string.
        /// </summary>
        ///
        /// <param name="abtBytes">
        /// Array of bytes to convert.  Can't be null.
        /// </param>
        ///
        /// <param name="cReplacementCharacter">
        /// Character to replace non-printable characters with.  Must have a value
        /// less than or equal to 127.
        /// </param>
        ///
        /// <remarks>
        /// This method replaces any bytes greater than 127 with <paramref
        /// name="cReplacementCharacter" />, then replaces any non-printable ASCII
        /// characters with the same replacement character.
        /// </remarks>
        //*************************************************************************
        public static String BytesToPrintableAscii(
            Byte [] abtBytes,
            Char cReplacementCharacter
            )
        {
            Debug.Assert(abtBytes != null);
            Debug.Assert( (Int32)cReplacementCharacter < 128 );

            // Make a copy of the byte array.

            Byte [] abtClone = ( Byte [] )abtBytes.Clone();

            // Replace any bytes > 127.

            for (Int32 i = 0; i < abtClone.Length; i++)
            if (abtClone[i] > 127)
                abtClone[i] = (Byte)cReplacementCharacter;

            // Use the ASCIIEncoding to get a string.  This leaves all bytes
            // between 0 and 127 unmodified.

            String sString = Encoding.ASCII.GetString(abtClone);

            // Replace the non-printable characters.

            return ( ReplaceNonPrintableAsciiCharacters(
            sString, cReplacementCharacter) );
        }
        /// <summary>
        /// adjust the Brightness from input 'source' and output a Byte array
        /// </summary>
        public Byte[] adjustBrightnessByteLevel(int value, Byte[] source)
        {
            //  if (value == m_currentBrightness)
              //  return source;

            double adjustment = (double)(value) / (100.0 * 3);
            adjustment *= (double)255;
            int r, g, b;
            //Byte[] result = new Byte[source.Length];
            Byte[] result = (Byte[]) source.Clone();
            for (int y = 0; y < m_imageHeight; y++)
            {
                for (int x = 0; x < m_imageWidth; x++)
                {

                    // int outputIndex = 4 * (ImageWidth - 1 - x) + 4 * (ImageHeight - 1 - y) * ImageWidth;
                    //int inputIndex = 4 * (x) + (y) * (4 * ImageWidth);
                    b = source[4 * x + y * (4 * m_imageWidth) + 0];
                    g = source[4 * x + y * (4 * m_imageWidth) + 1];
                    r = source[4 * x + y * (4 * m_imageWidth) + 2];

                    b += (int)adjustment;
                    g += (int)adjustment;
                    r += (int)adjustment;

                    if (value > 0) // lightening
                    {

                        if (r > 255)
                            r = 255;
                        if (g > 255)
                            g = 255;
                        if (b > 255)
                            b = 255;
                    }
                    else
                    {
                        if (r < 0)
                            r = 0;
                        if (g < 0)
                            g = 0;
                        if (b < 0)
                            b = 0;
                    }
                    result[4 * x + y * (4 * m_imageWidth) + 0] = (byte)b;
                    result[4 * x + y * (4 * m_imageWidth) + 1] = (byte)g;
                    result[4 * x + y * (4 * m_imageWidth) + 2] = (byte)r;
                }
            }

            m_currentBrightness = value;
            return result;
        }
        /// <summary>
        /// adjust the Contrast from input 'source' and output a Byte array
        /// </summary>
        public Byte[] adjustContrastByteLevel(int value, Byte[] source)
        {
            double r, g, b;
             //   if (value == m_currentContrast)
              //  return source;
            Byte[] result = (Byte[])source.Clone();
            double contrast = (double)Math.Pow((100 + value) / 100.0, 2);

            for (int y = 0; y < m_imageHeight; y++)
            {
                for (int x = 0; x < m_imageWidth; x++)
                {
                    r = source[4 * x + y * (4 * m_imageWidth) + 2] / 255.0;
                    g = source[4 * x + y * (4 * m_imageWidth) + 1] / 255.0;
                    b = source[4 * x + y * (4 * m_imageWidth) + 0] / 255.0;
                    //Blue
                    //Converts Blue value to a value between 0 and 1
                    //Centers that value over 0 (value will be between -.5 and .5)
                    b -= 0.5f;
                    //Adjust the value by contrast (value will be between -127.5 and 127.5)
                    //(Value will usually be between -1 and 1)
                    b *= contrast;
                    //Value will be between -127 and 128
                    b += 0.5f;
                    //Clamp value
                    if (b > 1)
                        b = 1;
                    else if (b < 0)
                        b = 0;

                    //Green
                    g -= 0.5f;
                    g *= contrast;
                    g += 0.5f;
                    if (g > 1)
                        g = 1;
                    else if (g < 0)
                        g = 0;

                    //Red
                    r -= 0.5f;
                    r *= contrast;
                    r += 0.5f;
                    if (r > 1)
                        r = 1;
                    else if (r < 0)
                        r = 0;

                    result[4 * x + y * (4 * m_imageWidth) + 2] = (byte)(r * 255);
                    result[4 * x + y * (4 * m_imageWidth) + 1] = (byte)(g * 255);
                    result[4 * x + y * (4 * m_imageWidth) + 0] = (byte)(b * 255);
                }
            }
            m_currentContrast = value;
            return result;
        }
Пример #19
0
 /// <summary>
 /// KD = KE + sum(Each byte of plainText);
 /// Decoding Key is also controling sequence.
 /// </summary>
 /// <param name="encodingKey">Encoding Key in bytes. 32bytes</param>
 /// <returns>Decoding Key in bytes</returns>
 public static Byte[] GenerateDecodingKey(Byte[] encodingKey, Byte[] plainText)
 {
     if (encodingKey.Length != 32)
         throw new Exception("Encoding Key length must be 32bytes. Current one is " + encodingKey.Length + "bytes");
     int itr_K = 0;
     int length = encodingKey.Length;
     Byte[] decodingKey = (Byte[])encodingKey.Clone(); // The length is the same.
     foreach(Byte b in plainText)
     {
         decodingKey[(itr_K++) % length] += b;
     }
     return decodingKey;
 }