public static String FromByteArray(Byte[] b, Byte version)
 {
     SHA256 sha256 = new SHA256Managed();
     b = (new Byte[] { version }).Concat(b).ToArray();
     Byte[] hash = sha256.ComputeHash(sha256.ComputeHash(b)).Take(4).ToArray();
     return Base58String.FromByteArray(b.Concat(hash).ToArray());
 }
        public void DeleteFile(List<string> names)
        {
            TcpClient tcpClient = null;
            NetworkStream stream = null;
            try
            {
                tcpClient = new TcpClient(ip, port);

                stream = tcpClient.GetStream();

                Byte[] dataToSend = new Byte[1];
                dataToSend[0] = (byte)TcpPacketType.Delete;
                
                StringBuilder sb = new StringBuilder();
                if (names.Count > 0)
                {
                    for (int i = 0; i < names.Count - 1; i++)
                    {
                        sb.Append(names[i] + "#");
                    }
                    sb.Append(names[names.Count - 1]);
                }
                dataToSend = dataToSend.Concat(System.Text.Encoding.UTF8.GetBytes(sb.ToString())).ToArray();
                stream.Write(dataToSend, 0, dataToSend.Length);
                stream.Flush();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                stream.Close();
                tcpClient.Close();
            }
        }
        public bool UploadFile()
        {
            TcpClient tcpClient = null;
            NetworkStream stream = null;
            try
            {
                tcpClient = new TcpClient(ip,port);

                stream = tcpClient.GetStream();
                Byte[] name = System.Text.Encoding.UTF8.GetBytes(System.IO.Path.GetFileName(file));
                Byte[] dataToSend = new Byte[name.Length + 5];
                dataToSend[0] = (byte)TcpPacketType.Upload;
                Buffer.BlockCopy(BitConverter.GetBytes(name.Length), 0, dataToSend, 1, 4);
                Buffer.BlockCopy(name, 0, dataToSend, 5, name.Length);


                using (System.IO.FileStream fs = System.IO.File.Open(file, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
                {
                    int numBytesToRead = Convert.ToInt32(fs.Length);
                    byte[] oFileBytes = new byte[(numBytesToRead)];
                    fs.Read(oFileBytes, 0, numBytesToRead);
                    dataToSend = dataToSend.Concat(oFileBytes).ToArray();
                    stream.Write(dataToSend, 0, dataToSend.Length);
                    stream.Flush();
                }

                //byte[] backData = new byte[4];
                //stream.Read(backData, 0, 4);
                //int length = BitConverter.ToInt32(backData, 0);
                //if (length == dataToSend.Length)
                //{
                    return true;
                //}
                //else
                //{
                //    return false;
                //}

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return false;
            }

            finally
            {
                stream.Close();
                tcpClient.Close();
            }
           
        }
Esempio n. 4
0
        public Byte[] Prepare(Byte[] m)
        {
            Byte[] endByteArray = new Byte[c];

            //Füllt EndArray
            for (int i = 0; i < c; i++)
            {
                endByteArray[i] = 255;
            }

            while (m.Length % c != 0)
            {
                m = m.Concat(new Byte[] { 255 }).ToArray();
            }

            return m.Concat(endByteArray).ToArray();
        }
Esempio n. 5
0
 /// <summary>
 ///     DES加密
 /// </summary>
 /// <param name="plain">字符串表示的16进制明文</param>
 /// <param name="key"></param>
 /// <returns></returns>
 public static string Encrypt(string plain, Byte[] key)
 {
     //密钥不足8byte,补足
     if (key.Length < 8)
     {
         key = key.Concat(new Byte[8 - key.Length]).ToArray();
     }
     List<Byte> pBytes = new List<byte>(plain.HexStringToBytes());
     //明文的长度不是8的倍数,补足
     while (pBytes.Count%8 != 0)
     {
         pBytes.Add(new byte());
     }
     var bytes = pBytes.ToArray();
     byte[] p = new byte[8];
     StringBuilder sb = new StringBuilder();
     //每8Byte进行一次DES加密,结果拼接
     for (int i = 0; i < bytes.Length; i += 8)
     {
         Array.ConstrainedCopy(bytes, i, p, 0, 8);
         sb.Append(DES.Encrypt(p, key).PrintInHex());
     }
     return sb.ToString();
 }
Esempio n. 6
0
        /// <summary>
        ///     DES加密
        /// </summary>
        /// <param name="plain">8Byte字节数组明文</param>
        /// <param name="key">8Byte字节数组密文</param>
        /// <returns></returns>
        public static byte[] Encrypt(Byte[] plain, Byte[] key)
        {
            if (plain.Length > 8 || key.Length > 8)
            {
                throw new ArgumentException("Plain text and key should be 8 bytes.");
            }
            //不足8字节,补0
            if (plain.Length < 8)
            {
                plain = plain.Concat(new Byte[8 - plain.Length]).ToArray();
            }
            if (key.Length < 8)
            {
                key = key.Concat(new Byte[8 - key.Length]).ToArray();
            }

            //转为位数组 处理小端->大端
            BitArray input = new BitArray(plain.Reverse().ToArray()).Reverse();
            BitArray inputKey = new BitArray(key.Reverse().ToArray()).Reverse();
            Debug.WriteLine("[PLAIN]" + input.PrintInBinary());
            Debug.WriteLine("[KEY]" + inputKey.PrintInBinary());
            //初始置换
            input = FirstSwap(input);
            //Debug.WriteLine(input.PrintInHex());
            BitArray[] keys = new BitArray[16];
            //设置L R
            RoundPackage rounds = new RoundPackage();
            rounds.L.SetRange(0, 32, input);
            rounds.R.SetRange(0, 32, input, 32);
            //生成16轮用子密钥
            keys = GenerateKeys(inputKey);

            //16轮加密
            for (int i = 0; i < 16; i++)
            {
                rounds = Round(rounds, keys[i]);
                //Debug.WriteLine("i:{3}, L:{0},R:{1},Ki:{2}", rounds.L.PrintInBinary(), rounds.R.PrintInBinary(), keys[i].PrintInBinary(),i+1);
            }
            //Debug.WriteLine("L:{0},R:{1}", rounds.L.PrintInBinary(), rounds.R.PrintInBinary());

            BitArray output = new BitArray(64);
            //拼接:R+L
            output = rounds.R.Append(rounds.L);
            //Debug.WriteLine(output.PrintInBinary());
            //逆初始置换
            output = ReverseFirstSwap(output);
            Debug.WriteLine("[ENCRYPT]" + output.PrintInBinary());
            return output.ToByteArray();
        }
Esempio n. 7
0
 /// <summary>
 ///     DES解密
 /// </summary>
 /// <param name="input"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 public static string Decrypt(string input, Byte[] key)
 {
     if (key.Length < 8)
     {
         key = key.Concat(new Byte[8 - key.Length]).ToArray();
     }
     List<Byte> pBytes = new List<byte>(input.HexStringToBytes());
     while (pBytes.Count%8 != 0)
     {
         pBytes.Add(new byte());
     }
     var bytes = pBytes.ToArray();
     byte[] p = new byte[8];
     StringBuilder sb = new StringBuilder();
     for (int i = 0; i < bytes.Length; i += 8)
     {
         Array.ConstrainedCopy(bytes, i, p, 0, 8);
         sb.Append(DES.Decrypt(p, key).PrintInHex());
     }
     return sb.ToString();
 }
Esempio n. 8
0
        public static byte[] Decrypt(Byte[] inputBytes, Byte[] key)
        {
            if (inputBytes.Length > 8 || key.Length > 8)
            {
                throw new ArgumentException("Encrypted text and key should be 8 bytes.");
            }
            if (inputBytes.Length < 8)
            {
                inputBytes = inputBytes.Concat(new Byte[8 - inputBytes.Length]).ToArray();
            }
            if (key.Length < 8)
            {
                key = key.Concat(new Byte[8 - key.Length]).ToArray();
            }
            //BitArray input = new BitArray(inputBytes);
            //BitArray inputKey = new BitArray(key);
            //处理小端->大端
            BitArray input = new BitArray(inputBytes.Reverse().ToArray()).Reverse();
            BitArray inputKey = new BitArray(key.Reverse().ToArray()).Reverse();
            Debug.WriteLine("[ENCRYPTED]" + input.PrintInBinary());
            Debug.WriteLine("[KEY]" + inputKey.PrintInBinary());
            input = FirstSwap(input);
            BitArray[] keys = new BitArray[16];
            RoundPackage rounds = new RoundPackage();
            rounds.L.SetRange(0, 32, input);
            rounds.R.SetRange(0, 32, input, 32);

            keys = GenerateKeys(inputKey);
            //Debug.WriteLine("L:{0},R:{1},Ki:{2}", rounds.L.PrintInHex(), rounds.R.PrintInHex(), keys[15].PrintInHex());

            for (int i = 15; i >= 0; i--)
            {
                rounds = Round(rounds, keys[i]);
                //Debug.WriteLine("L:{0},R:{1},Ki:{2}", rounds.L.PrintInHex(), rounds.R.PrintInHex(), keys[i].PrintInHex());
            }
            BitArray output = new BitArray(64);
            output = rounds.R.Append(rounds.L);
            output = ReverseFirstSwap(output);
            Debug.WriteLine("[DECRYPT]" + output.PrintInBinary());
            return output.ToByteArray();
        }
Esempio n. 9
-1
        private MessageId GenerateMessageId(EMail Mail, String DomainPart = null)
        {
            if (DomainPart.IsNullOrEmpty())
                DomainPart = base.RemoteHost;

            var RandomBytes  = new Byte[16];
            _Random.NextBytes(RandomBytes);

            var HashedBytes = _SHAHasher.ComputeHash(RandomBytes.
                                                     Concat(Mail.From.   ToString().ToUTF8Bytes()).
                                                     Concat(Mail.Subject.           ToUTF8Bytes()).
                                                     Concat(Mail.Date.   ToString().ToUTF8Bytes()).
                                                     ToArray());

            return MessageId.Parse(HashedBytes.ToHexString().Substring(0, 24) + "@" + DomainPart);
        }