Ejemplo n.º 1
0
        static bool GetAES(ref byte[] data, BlindSocket socket, out Cryptography.AES256 aes256)
        {
            aes256 = null;

            uint encryptDate = BitConverter.ToUInt32(data, 4);

            byte[] realData = new byte[data.Length - 8];
            Array.Copy(data, 8, realData, 0, realData.Length);
            data = realData;

            Console.WriteLine("Encrypted date : " + encryptDate);
            socket.CryptoSend(BitConverter.GetBytes(encryptDate), PacketType.Info);

            byte[] key = socket.CryptoReceiveMsg();
            if (key == null)
            {
                MessageBox.Show("파일 복호화에 실패했습니다.", "파일 열기");
                return(false);
            }
            Console.WriteLine("Received key {0} bytes", key.Length);

            byte[] iv = socket.CryptoReceiveMsg();
            if (iv == null)
            {
                MessageBox.Show("파일 복호화에 실패했습니다.", "파일 열기");
                return(false);
            }
            Console.WriteLine("Received iv {0} bytes", iv.Length);

            aes256 = new Cryptography.AES256(key, iv);
            return(true);
        }
Ejemplo n.º 2
0
        private byte[] DecryptFile(byte[] data)
        {
            byte[] fileData = new byte[data.Length - 8];
            Array.Copy(data, 8, fileData, 0, fileData.Length);
            uint   encryptDate = BitConverter.ToUInt32(data, 4);
            string command     = "SELECT _key, iv FROM crypto_key WHERE apply_date <= '" + encryptDate +
                                 "' AND expire_date > '" + encryptDate + "'; ";
            MySqlDataAdapter adapter = new MySqlDataAdapter(command, connection);
            DataSet          dataset = new DataSet();

            adapter.Fill(dataset);
            if (dataset.Tables[0].Rows.Count != 1)
            {
                return(null);
            }
            Cryptography.AES256 aes256 = new Cryptography.AES256((byte[])(dataset.Tables[0].Rows[0]["_key"]), (byte[])(dataset.Tables[0].Rows[0]["iv"]));
            try
            {
                return(aes256.Decryption(fileData));
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Ejemplo n.º 3
0
 public bool ConnectWithECDH(string ip = BlindNetConst.ServerIP, int port = BlindNetConst.MAINPORT)
 {
     Connect(ip, port);
     aes = ECDH_Client();
     if (aes == null)
     {
         return(false);
     }
     return(true);
 }
Ejemplo n.º 4
0
        public async Task <bool> ConnectWithECDHAsync(string ip = BlindNetConst.ServerIP, int port = BlindNetConst.MAINPORT)
        {
            await Task.Run(() => Connect(ip, port));

            aes = ECDH_Client();
            if (aes == null)
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 5
0
        private BlindSocket ECDH_Server(Socket socket)
        {
            if (socket == null)
            {
                return(null);
            }

            Cryptography.AES256 aes;
            BlindSocket         clientSock;

            using (ECDiffieHellmanCng dh = new ECDiffieHellmanCng())
            {
                dh.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
                dh.HashAlgorithm         = CngAlgorithm.Sha256;
                byte[] publicKey = dh.PublicKey.ToByteArray();
                socket.Send(publicKey, publicKey.Length, SocketFlags.None);
                byte[] sharekey = new byte[publicKey.Length];
                socket.Receive(sharekey, publicKey.Length, SocketFlags.None);
                byte[] key = dh.DeriveKeyMaterial(CngKey.Import(sharekey, CngKeyBlobFormat.EccPublicBlob));
                aes        = new Cryptography.AES256(key);
                clientSock = new BlindSocket(socket, aes);
            }

            for (int i = 1; ; i++)
            {
                byte[] prevIv = aes.aes.IV;
                aes.aes.GenerateIV();
                byte[] newIv = aes.aes.IV;
                aes.aes.IV = prevIv;

                clientSock.CryptoSend(newIv, PacketType.Info);
                byte[] iv = clientSock.CryptoReceiveMsg();

                if (!newIv.SequenceEqual(iv))
                {
                    if (i < BlindNetConst.MAXRETRY)
                    {
                        clientSock.CryptoSend(null, PacketType.Retry);
                    }
                    else
                    {
                        clientSock.CryptoSend(null, PacketType.Fail);
                        return(null);
                    }
                }
                else
                {
                    clientSock.CryptoSend(null, PacketType.OK);
                    aes.aes.IV = newIv;
                    break;
                }
            }
            return(clientSock);
        }
Ejemplo n.º 6
0
        private BlindSocket ECDH_Server(Socket socket)
        {
            if (socket == null)
            {
                return(null);
            }

            Cryptography.AES256 aes;
            BlindSocket         clientSock;

            using (ECDiffieHellmanCng dh = new ECDiffieHellmanCng())
            {
                dh.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
                dh.HashAlgorithm         = CngAlgorithm.Sha256;
                byte[] publicKey = dh.PublicKey.ToByteArray();
                socket.Send(publicKey, publicKey.Length, SocketFlags.None);
                byte[] sharekey = new byte[publicKey.Length];
                socket.Receive(sharekey, publicKey.Length, SocketFlags.None);
                byte[] key = dh.DeriveKeyMaterial(CngKey.Import(sharekey, CngKeyBlobFormat.EccPublicBlob));
                aes        = new Cryptography.AES256(key);
                clientSock = new BlindSocket(socket, aes);
            }

            for (int i = 1; ; i++)
            {
                string testTxt = BlindNetUtil.GetRandomString(BlindNetConst.MINRNDTXT, BlindNetConst.MAXRNDTXT);
                clientSock.CryptoSend(Encoding.UTF8.GetBytes(testTxt), PacketType.MSG);
                var    pack    = clientSock.CryptoReceive();
                string recvTxt = Encoding.UTF8.GetString(pack.data).TrimEnd('\0');
                if (recvTxt != testTxt)
                {
                    if (i < BlindNetConst.MAXRETRY)
                    {
                        clientSock.CryptoSend(null, PacketType.Retry);
                    }
                    else
                    {
                        clientSock.CryptoSend(null, PacketType.Fail);
                        return(null);
                    }
                }
                else
                {
                    break;
                }
            }
            clientSock.CryptoSend(null, PacketType.OK);
            return(clientSock);
        }
Ejemplo n.º 7
0
        private Cryptography.AES256 ECDH_Client()
        {
            Cryptography.AES256 aes;
            using (ECDiffieHellmanCng dh = new ECDiffieHellmanCng())
            {
                dh.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
                dh.HashAlgorithm         = CngAlgorithm.Sha256;
                byte[] publicKey = dh.PublicKey.ToByteArray();
                byte[] sharekey  = new byte[publicKey.Length];
                socket.Receive(sharekey, publicKey.Length, SocketFlags.None);
                socket.Send(publicKey, publicKey.Length, SocketFlags.None);
                byte[] key = dh.DeriveKeyMaterial(CngKey.Import(sharekey, CngKeyBlobFormat.EccPublicBlob));
                aes = new Cryptography.AES256(key);
            }

            this.aes = aes;
            while (true)
            {
                byte[] iv = CryptoReceiveMsg();
                CryptoSend(iv, PacketType.Response);
                var pack = CryptoReceive();
                if (pack.header == PacketType.Retry)
                {
                    continue;
                }
                else if (pack.header == PacketType.Fail)
                {
                    IPEndPoint iep = (IPEndPoint)(socket.RemoteEndPoint);
                    Console.WriteLine("ERROR [Host " + iep.Address + ":" + iep.Port + "] Connection test with text is failed");
                    this.aes = null;
                    return(null);
                }
                else if (pack.header == PacketType.OK)
                {
                    aes.aes.IV = iv;
                    break;
                }
            }
            return(aes);
        }
Ejemplo n.º 8
0
        static bool GetLatestAES(BlindSocket socket, out Cryptography.AES256 aes256)
        {
            aes256 = null;
            byte[] key = socket.CryptoReceiveMsg();
            if (key == null)
            {
                MessageBox.Show("파일 복호화에 실패했습니다.", "파일 열기");
                return(false);
            }
            Console.WriteLine("Received key {0} bytes", key.Length);

            byte[] iv = socket.CryptoReceiveMsg();
            if (iv == null)
            {
                MessageBox.Show("파일 복호화에 실패했습니다.", "파일 열기");
                return(false);
            }
            Console.WriteLine("Received iv {0} bytes", iv.Length);

            aes256 = new Cryptography.AES256(key, iv);
            return(true);
        }
Ejemplo n.º 9
0
        private byte[] EncryptFile(byte[] data)
        {
            string           command = "SELECT _key, iv FROM crypto_key WHERE apply_date <= NOW() AND expire_date > NOW();";
            MySqlDataAdapter adapter = new MySqlDataAdapter(command, connection);
            DataSet          dataset = new DataSet();

            adapter.Fill(dataset);
            if (dataset.Tables[0].Rows.Count != 1)
            {
                return(null);
            }

            Cryptography.AES256 aes256 = new Cryptography.AES256((byte[])(dataset.Tables[0].Rows[0]["_key"]), (byte[])(dataset.Tables[0].Rows[0]["iv"]));
            try
            {
                uint timestemp = uint.Parse(DateTime.Now.ToString("yyyyMMdd"));
                return(BlindNetUtil.MergeArray(BitConverter.GetBytes(timestemp), aes256.Encryption(data)));
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Ejemplo n.º 10
0
 public BlindSocket(ref BlindSocket blindSocket)
 {
     socket = blindSocket.socket;
     aes    = blindSocket.aes;
 }
Ejemplo n.º 11
0
 public BlindSocket(Socket socket, Cryptography.AES256 aes)
 {
     this.socket = socket;
     this.aes    = aes;
 }
Ejemplo n.º 12
0
 public BlindSocket(Cryptography.AES256 aes)
 {
     socket   = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     this.aes = aes;
 }
Ejemplo n.º 13
0
 public BlindSocket()
 {
     socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     aes    = null;
 }