Esempio n. 1
0
        public static string Encrypt(string plainText, string teaKey, Encoding encoding = null)
        {
            if (string.IsNullOrEmpty(plainText))
            {
                return(null);
            }
            if (string.IsNullOrEmpty(teaKey))
            {
                return(null);
            }
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            byte[] x = Encoding.UTF8.GetBytes(plainText);
            uint[] v = StrConvert.StrToLongs(x, 0, 0);
            // simply convert first 16 chars of password as key
            x = encoding.GetBytes(teaKey);
            uint[] k = StrConvert.StrToLongs(x, 0, 16);

            byte[] encryptText = EncryptBlock(v, k);

            return(Convert.ToBase64String(encryptText));
        }
Esempio n. 2
0
        public static string Decrypt(string cipherText, string teaKey, Encoding encoding = null)
        {
            if (string.IsNullOrEmpty(cipherText))
            {
                return(null);
            }
            if (string.IsNullOrEmpty(teaKey))
            {
                return(null);
            }
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            byte[] x = Convert.FromBase64String(cipherText);
            uint[] v = StrConvert.StrToLongs(x, 0, 0);
            // simply convert first 16 chars of password as key
            x = encoding.GetBytes(teaKey);
            uint[] k = StrConvert.StrToLongs(x, 0, 16);

            byte[] decryptText = DecryptBlock(v, k);

            return(encoding.GetString(decryptText));
        }
Esempio n. 3
0
        public string Decrypt(string cipherText)
        {
            if (String.IsNullOrEmpty(cipherText))
            {
                return(null);
            }

            byte[] x = Convert.FromBase64String(cipherText);
            uint[] v = StrConvert.StrToLongs(x, 0, 0);

            return(Encoding.UTF8.GetString(DecryptBlock(v, teakeyArr)));
        }
Esempio n. 4
0
        public string Encrypt(string plainText)
        {
            if (String.IsNullOrEmpty(plainText))
            {
                return(null);
            }

            byte[] x = Encoding.UTF8.GetBytes(plainText);
            uint[] v = StrConvert.StrToLongs(x, 0, 0);

            return(Convert.ToBase64String(EncryptBlock(v, teakeyArr)));
        }
Esempio n. 5
0
        public static byte[] DecryptBlock(uint[] v, uint[] k)
        {
            if (v == null || k == null)
            {
                return(null);
            }
            if (k.Length < 4)
            {
                return(null);
            }

            uint n = (uint)v.Length;

            if (n == 0)
            {
                return(null);
            }
            if (n <= 1)
            {
                return new byte[1] {
                           0
                }
            }
            ;                                     // algorithm doesn't work for n<2 so fudge by adding a null

            uint q = (uint)(6 + 52 / n);

            n--;
            uint z = v[n], y = v[0];
            uint mx, e, sum = q * DELTA;
            uint p = 0;

            while (sum != 0)
            {
                e = sum >> 2 & 3;

                for (p = n; p > 0; p--)
                {
                    z  = v[p - 1];
                    mx = (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
                    y  = v[p] -= mx;
                }

                z  = v[n];
                mx = (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
                y  = v[0] -= mx;

                sum -= DELTA;
            }

            return(StrConvert.LongsToStr(v));
        }
Esempio n. 6
0
        public static byte[] EncryptBlock(uint[] v, uint[] k)
        {
            if (v == null || k == null)
            {
                return(null);
            }
            if (k.Length < 4)
            {
                return(null);
            }

            int n = v.Length;

            if (n == 0)
            {
                return(null);
            }
            if (n <= 1)
            {
                return new byte[1] {
                           0
                }
            }
            ;                                     // algorithm doesn't work for n<2 so fudge by adding a null

            uint q = (uint)(6 + 52 / n);

            n--;
            uint z = v[n], y = v[0];
            uint mx, e, sum = 0;

            while (q-- > 0)
            {  // 6 + 52/n operations gives between 6 & 32 mixes on each word
                sum += DELTA;

                e = sum >> 2 & 3;

                for (int p = 0; p < n; p++)
                {
                    y  = v[p + 1];
                    mx = (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
                    z  = v[p] += mx;
                }
                y  = v[0];
                mx = (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[n & 3 ^ e] ^ z);
                z  = v[n] += mx;
            }

            return(StrConvert.LongsToStr(v));
        }
        protected override ICryptoValue DecryptInternal(ArraySegment <byte> cipherBytes, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var cipher = GetBytes(cipherBytes);

            if (cipher.Length == 0)
            {
                return(CreateCryptoValue(cipher, cipher, CryptoMode.Decrypt));
            }
            var v = StrConvert.StrToLongs(cipher, 0, 0);
            var k = StrConvert.StrToLongs(Key.GetKey(), 0, 16);

            var original = DecryptBlock(v, k);

            return(CreateCryptoValue(original, cipher, CryptoMode.Decrypt, o => o.TrimTerminatorWhenDecrypting = true));
        }
Esempio n. 8
0
        public bool SetTeaKey(string teaKey)
        {
            if (String.IsNullOrEmpty(teaKey))
            {
                return(false);
            }
            if (teaKey.Length < 16)
            {
                return(false);
            }

            this.teakey = teaKey;
            byte[] x = Encoding.UTF8.GetBytes(teaKey);
            this.teakeyArr = StrConvert.StrToLongs(x, 0, 16);

            return(true);
        }
        protected override ICryptoValue EncryptInternal(ArraySegment <byte> originalBytes, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var data = GetBytes(originalBytes);

            if (data.Length == 0)
            {
                return(CreateCryptoValue(data, data, CryptoMode.Encrypt));
            }

            var v = StrConvert.StrToLongs(data, 0, 0);
            var k = StrConvert.StrToLongs(Key.GetKey(), 0, 16);

            var cipher = EncryptBlock(v, k);

            return(CreateCryptoValue(data, cipher, CryptoMode.Encrypt));
        }
Esempio n. 10
0
        public string SaveRecord()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(totalProfit);
            sb.Append(",");
            sb.Append(totalLoss);
            sb.Append(",");
            sb.Append(profitablePositions);
            sb.Append(",");
            sb.Append(loosingPositions);
            sb.Append(",");
            foreach (int pos in recordPosition)
            {
                sb.Append(pos);
                sb.Append(",");
            }

            return(StrConvert.EncodeTo64(sb.ToString()));
        }
Esempio n. 11
0
        public void LoadRecord(string data)
        {
            string dataDecoded = StrConvert.DecodeFrom64(data);

            string[] parts = dataDecoded.Split(',');

            totalProfit         = int.Parse(parts[0]);
            totalLoss           = int.Parse(parts[1]);
            profitablePositions = int.Parse(parts[2]);
            loosingPositions    = int.Parse(parts[3]);

            for (int i = 4; i < parts.Length; i++)
            {
                if (parts[i].Length == 0)
                {
                    continue;
                }

                recordPosition[i - 4] = int.Parse(parts[i]);
            }
        }
Esempio n. 12
0
 public void SetTeaKey(string teaKey)
 {
     this.teakey = teaKey;
     byte[] x = Encoding.UTF8.GetBytes(teaKey);
     this.teakeyArr = StrConvert.StrToLongs(x, 0, 16);
 }