Elsewhere there are things that we all miss, yet it takes just one to notice...

Notes on encryption and security

An important part of communicating over the internet is having some security in place. For my own project this will have the server setup so that it only accepts connections from known devices. It will also encrypt data before it is sent and decrypted when the server receives it.

Encryption:

There are many ways to encrypt data:

  1. Inverting all bits in the data being sent. This was handy in the very old days of 8-bit cpu’s to hide data in memory to stop people spying on code and data in memory.
  2. Apply a key to the data. A good method of securing data which could be a password protected to unlock on the receiving side. However, it doesn’t take long for key to be found.
  3. Apply a key and add salt. Skewing the data like in method 2 is good, but when adding salt, it makes it almost impossible to decrypt without having access to the code.

I’ll be using a system which adds a key and pours in some salt. Now how this is done is using a key to skew the data, and then by splitting the bits of the data up randomly and adding random bits in between, the bitstream will be larger but impossible to decrypt. 2 things will be needed to decrypt the data, the code itself and the key.

Security:

Most of the time, security means logging in with a username and password. The final project will require the device to first log in which will have a timeout feature on it. This is just like session handling in GWT for the web. The login data will still be sent encrypted.

Fortunately, I’ve worked with encryption since 8-bit cpu’s, so this shouldn’t pose a problem for me at all. I’ll have some generic classes written that can be used on both client and server for de/encryption. Apart from the usual login credentials, the client and server will have some hardcoded (still encrypted) transport keys, which can be updated when software is updated. The server can also handle software updates which includes data security updates. This will mean that software that isn’t updated will no-longer work unless it gets an update.

Obviously, this means a lot of boilerplate code, but it will be worth it. And hiding it from a web interface by using TCP, it will be more secure.

One last thing to note about security is server/port attacks. On each socket connection, the server will log the number of requests from an IP address. It it gets too many then it will ignore that IP address for 30 minutes or another set time. This will reduce DoS attacks on the servers port/s.

When the application is transferring data back and forth to the server, most of the communication will be done in one connection. Too many connection attempts will simply be denied.

TCP home server test client

With the previous post setup I’ve written a very basic example of how to test the server. At the moment, there are no back and forth communications, just a simple send of a 4 byte header.

Run the server program first, and then run the test. These can actually be run in the same order from Eclipse.

The good news is, this also works over the internet. The ‘Global’ in the code hold a string of my web server address and also the port that my router has open.

So, all working as expected.

[snippet id=”11″]

The output from the server will be:

WLGfx Home Server - Starting up
Shutdown hook attached
TCP Server thread started...
TCP Incoming socket handler
Got header: HEAD

Java server test 1 [UPDATED]

My first test for Java as a console daemon. There’s not much here at the moment and the project is in Eclipse IDE.

From Eclipse, right click on the project and select export and then export as a runnable jar. Then in the terminal just type:

java -jar myexportedjar.jar

The JVM will continue to run as long as there are non daemon threads still running as in the below code. Also note that the hook handler allows catching of CTRL+C and other errors.

UPDATE:

I’ve now added the actually tcp server code which has a timeout on the server socket to allow for the thread to shut down safely. And a thread to handle each incoming connection.

[snippet id=”9″]

(I’m currently not well at the moment but I’m plodding on with some studies.)

Testing out TCP server over the web (my IoT stuff)

So I have this idea where I can take photos on my phone or save images from the web and then send them directly to my PC.

I’m currently using a Sky router in which I have enabled a port redirect to my PC. That’s all working. And I’ve got an external web address which re-routes to my PC also. That’s all working as I can host a website on my PC and access from outside.

The first pitfall was I started a console application in Qt to be a TCP server, but, deploying Qt applications to anywhere can be a huge pain in the butt.

So now I’ve decided to pick between a Java server or a C/C++ server. The issue I will have with a standard C/C++ server is image handling when it comes to working with them in the future, whereas Java has built in Bitmap libraries. Although I could add a 3rd party library to get around this, Java has lots of other libraries.

The other issue is speed. C/C++ has raw speed and that it. But because its a server, speed shouldn’t really be an issue for most things.

I’ve already successfully got the address of my PC over the internet and in the past had a basic TCP communication test working. This time it’s for real.

Okay, I’ll try out a Java project and see how it goes.

My current stack development

Linux headless server

Once everything is deployed, the linux distribution will have  the desktop stripped and left to run headless.

UDP server

General broadcasting of the servers location on the network along with the current time 4 times a second as the end point devices have no internet connection and no time.

TCP server

Used for communication between the server and devices. Content is managed through TCP instead of FTP as it is more secure.

C/C++

The software manages the servers UDP and TCP communication and also handles other commands when necessary from the web server. UDP is used for streaming DVB-T data across the network. Continually running daemon in linux.

Apache Tomcat 7

The Java based web server.

Google Web Toolkit (GWT)

The web framework for both front and back end web server. Used to manage user logins and session authentication. Manages the devices deployment. Mainly in Java. The web server is a large project.

Android Studio and Qt 5.7

Used to write the software for the end point devices.

GWT IFrame DialogBox example

In GWT, being able to load external pages comes in extremely handy when you want to pass on a lot of the design work to another team. This can be achieved by loading an external page elsewhere on the web server into an IFrame and wrapping the existing widgets.

Most GWT widgets can be wrapped, buttons, DIV’s, check boxes, etc.

Unfortunately I’ve not found a way of wrapping a HTML table. The only way around that is to have the table on the existing HTML page in a DIV, clear the DIV and programmatically add the new table which can be styled using the pages CSS. However, another problem arises with CSS which is you cannot use table header and footers CSS.

Anyway, below is a quick and simple way of loading an external page into a DialogBox using an IFrame and wrapping the buttons. You might notice that I’ve cleared the DialogBox CSS style.

[snippet id=”7″]

More stuff August 2016

Very rarely do I update my website. That’s because I work all day and get very little time at home to actually work on my own stuff. However, I can update on a few things.

  1. My knowledge of Java has improved vastly that now it’s rare I use google to solve my problems for me. Personally I wouldn’t use it for my own projects, except for the web server I will eventually start work on. Lots of people still say that there is now speed difference between Java and C++, but there is when you are running constant loops in code.
  2. Google Web Toolkit or GWT. I’ve used this extensively for over a year now and I do actually like it more and more as the months go by. More recently, I’ve figured out how to manage logged in users and session management. I usually have a tomcat user assigned in Linux which the back-end can use, away from the web server to do it’s storage and data crunching. LibGDX also uses GWT and with the experience I’ve had, I’ve got a few things up my sleeve.
  3. Qt… What more can I say. Every new version that comes out it gets more and more support. Recently I have figured how to call Java functions from Qt’s C++ on the Android platform which can be used for lots of missing features from native.
  4. I still haven’t got round to playing around more with Python which I really want to do. As a scripting language it looks great. And for it’s mega bloated support for everything and anything (API’s etc), there’s no end to what it can do. Ideally once I get to getting good at it I want to be able to quickly prototype stuff to be redone in C++ or Java.

For the time being that’s it.

My plan for the very near future is to get some more stuff done on my website using the GWT back-end.

Scroll to top