Example #1
0
        private void VerifyHeader()
        {
            var buffer = new byte[UnicodeHeaderLength];

            ReadBytes(buffer, 0, 28);
            Validate.Match(buffer.Segment(0, 4), MagicBytes, "Magic value invalid.");
            Validate.Match(buffer.Segment(8, 2), MagicClientBytes, "Magic client value invalid.");
            _fileVersion = BitConverter.ToUInt16(buffer, 10);
            Validate.Any(_fileVersion, SupportedVersions, "Found unsupported version.");
            ReadBytes(buffer, 28, (IsAnsi ? AnsiHeaderLength : UnicodeHeaderLength) - 28);
            var crcPartial = BitConverter.ToUInt32(buffer, 4);

            Validate.Match(crcPartial, Crc32.Calculate(buffer.Segment(8, 471)), "Partial CRC invalid.");

            var nbt = new Bref(
                BitConverter.ToUInt64(buffer, RootOffset + 36),
                BitConverter.ToUInt64(buffer, RootOffset + 44));
            var bbt = new Bref(
                BitConverter.ToUInt64(buffer, RootOffset + 52),
                BitConverter.ToUInt64(buffer, RootOffset + 60));

            _nbtReader = new BTreeReader <NbtEntry>(Stream, (long)nbt.Ib, seg => new NbtEntry(seg));
            _bbtReader = new BTreeReader <BbtEntry>(Stream, (long)bbt.Ib, seg => new BbtEntry(seg));

            _cryptMethod = (CryptMethod)buffer[CryptMethodOffset];
        }
Example #2
0
 public string Crypt(CryptMethod method, string input)
 {
     if (method == CryptMethod.ENCRYPT)
     {
         var toEncryptArray = Encoding.UTF8.GetBytes(input as string);
         resultArray = Crypt(method, toEncryptArray);
         return(Convert.ToBase64String(resultArray, 0, resultArray.Length));
     }
     else
     {
         var toEncryptArray = Convert.FromBase64String(input.Replace(" ", "+"));
         resultArray = Crypt(method, toEncryptArray);
         return(Encoding.UTF8.GetString(resultArray));
     }
 }
Example #3
0
            public object Crypt(CryptMethod _method, CryptClass _class, object _input, string _key)
            {
                SymmetricAlgorithm control;

                switch (_class)
                {
                case CryptClass.AES:
                    control = new AesManaged();
                    break;

                case CryptClass.RC2:
                    control = new RC2CryptoServiceProvider();
                    break;

                case CryptClass.RIJ:
                    control = new RijndaelManaged();
                    break;

                case CryptClass.DES:
                    control = new DESCryptoServiceProvider();
                    break;

                case CryptClass.TDES:
                    control = new TripleDESCryptoServiceProvider();
                    break;

                default:
                    return(false);
                }

                control.Key     = UTF8Encoding.UTF8.GetBytes(_key);
                control.Padding = PaddingMode.PKCS7;
                control.Mode    = CipherMode.ECB;

                ICryptoTransform cTransform = null;

                byte[] resultArray;

                if (_method == CryptMethod.ENCRYPT)
                {
                    cTransform = control.CreateEncryptor();
                }
                else if (_method == CryptMethod.DECRYPT)
                {
                    cTransform = control.CreateDecryptor();
                }

                if (_input is string)
                {
                    byte[] inputArray = UTF32Encoding.UTF8.GetBytes(_input as string);
                    resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
                    control.Clear();
                    return(Convert.ToBase64String(resultArray, 0, resultArray.Length));
                }
                else if (_input is byte[])
                {
                    resultArray = cTransform.TransformFinalBlock((_input as byte[]), 0, (_input as byte[]).Length);
                    control.Clear();
                    return(resultArray);
                }
                return(false);
            }
Example #4
0
        private void VerifyHeader()
        {
            var buffer = new byte[UnicodeHeaderLength];
            ReadBytes(buffer, 0, 28);
            Validate.Match(buffer.Segment(0, 4), MagicBytes, "Magic value invalid.");
            Validate.Match(buffer.Segment(8, 2), MagicClientBytes, "Magic client value invalid.");
            _fileVersion = BitConverter.ToUInt16(buffer, 10);
            Validate.Any(_fileVersion, SupportedVersions, "Found unsupported version.");
            ReadBytes(buffer, 28, (IsAnsi ? AnsiHeaderLength : UnicodeHeaderLength) - 28);
            var crcPartial = BitConverter.ToUInt32(buffer, 4);
            Validate.Match(crcPartial, Crc32.Calculate(buffer.Segment(8, 471)), "Partial CRC invalid.");

            var nbt = new Bref(
                BitConverter.ToUInt64(buffer, RootOffset + 36),
                BitConverter.ToUInt64(buffer, RootOffset + 44));
            var bbt = new Bref(
                BitConverter.ToUInt64(buffer, RootOffset + 52),
                BitConverter.ToUInt64(buffer, RootOffset + 60));

            _nbtReader = new BTreeReader<NbtEntry>(_input, (long)nbt.Ib);
            _bbtReader = new BTreeReader<BbtEntry>(_input, (long)bbt.Ib);

            _cryptMethod = (CryptMethod)buffer[CryptMethodOffset];
        }
Example #5
0
            public enum CryptClass { AEC, DEC, RIJ, TDES }     //Different Cryptographic Algorithms
            public object Crypt(CryptMethod cryptmethod, CryptClass cryptclass, object input, string key, string IV)
            {
                SymmetricAlgorithm control;                  //Base class which all implementations of symmetric algorithms must inherit

                switch (cryptclass)                          //Choosing CryptClass
                {
                case CryptClass.AEC:
                    control = new AesManaged();
                    break;

                case CryptClass.DEC:
                    control = new DESCryptoServiceProvider();
                    break;

                case CryptClass.RIJ:
                    control = new RijndaelManaged();
                    break;

                case CryptClass.TDES:
                    control = new TripleDESCryptoServiceProvider();
                    break;

                default:
                    return(false);

                    break;
                }

                control.Key     = UTF8Encoding.ASCII.GetBytes(key); //Converting Key(string) into Byte[] using UTF8 encoding
                control.IV      = UTF8Encoding.UTF8.GetBytes(IV);   //Converting Initializing Vector(string) into Byte[] using UTF8 encoding
                control.Padding = PaddingMode.PKCS7;                //PaddingMode For Cypher
                control.Mode    = CipherMode.CBC;                   //Chiper Block Chaining

                ICryptoTransform cryptTransform = null;             //Defines the basic operations of cryptographic transformations

                if (cryptmethod == CryptMethod.ENCRYPT)
                {
                    cryptTransform = control.CreateEncryptor(control.Key, control.IV);
                }
                else if (cryptmethod == CryptMethod.DECRYPT)
                {
                    cryptTransform = control.CreateDecryptor(control.Key, control.IV);
                }

                byte[] resultarray;

                /*
                 * NOTE:
                 * 1->While Encrypting Decrypting String data type special attention be paid.
                 * 2->While Encrypting string must be converted to bytes using
                 * desired encoding techniques UTF8 or ASCII
                 * 3->While returning the Encrypted data in the form of string,
                 * use Convert.ToBase64String
                 * 4->While Decrypting the base64 string must be converted to bytes using
                 * Convert.FromBase64String
                 * 5->Finally while returning the Decrypted Data, Convert the byte array into
                 * string using UnicodeEncoding.UTF8.ToString method
                 *
                 */


                if (input is string)
                {
                    byte[] temp;
                    if (cryptmethod == CryptMethod.ENCRYPT)
                    {
                        temp = UTF8Encoding.UTF8.GetBytes((string)input);      //Convert into byte[] via UTF8 encoding
                    }
                    else
                    {
                        temp = Convert.FromBase64String((string)input);        //Convert into byte[] using FromBase64String since input is in base64 format
                    }
                    resultarray = cryptTransform.TransformFinalBlock(temp, 0, temp.Length);
                    control.Clear();
                    cryptTransform.Dispose();
                    if (cryptmethod == CryptMethod.ENCRYPT)
                    {
                        return(Convert.ToBase64String(resultarray));            //Converting byte[] to base64
                    }
                    else
                    {
                        return(UnicodeEncoding.UTF8.GetString(resultarray));     //Finally(Decryption) Converting byte[] into string
                    }
                }
                else if (input is byte[])
                {
                    resultarray = cryptTransform.TransformFinalBlock((byte[])input, 0, ((byte[])input).Length);
                    control.Clear();
                    cryptTransform.Dispose();
                    return(resultarray);
                }
                return(false);
            }
Example #6
0
 public byte[] Crypt(CryptMethod method, byte[] input)
 {
     cTransform  = method == CryptMethod.ENCRYPT ? control.CreateEncryptor() : control.CreateDecryptor();
     resultArray = cTransform.TransformFinalBlock(input, 0, input.Length);
     return(resultArray);
 }