Ejemplo n.º 1
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));
        }
Ejemplo n.º 2
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));
        }
Ejemplo 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)));
        }
Ejemplo 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)));
        }
Ejemplo n.º 5
0
        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));
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        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));
        }
Ejemplo n.º 8
0
 public void SetTeaKey(string teaKey)
 {
     this.teakey = teaKey;
     byte[] x = Encoding.UTF8.GetBytes(teaKey);
     this.teakeyArr = StrConvert.StrToLongs(x, 0, 16);
 }