Using USB Device as Key for Authentication System

By | February 14, 2018

Using a USB Device as Key for Authentication System
Good day, In this post I would like to present you an idea which came to my mind the other day. I was thinking of a cool and convenient way for an authentication system which does not require typing anything into an entry field. Getting to the point: I thought about using a USB device, USB storage / flash drive or mobile phone etc. as the key for an authentication system. This means whenever you have to authenticate in a system all you need to do is plugging in your USB device. The authentication system will scan it and determine whether it is white listed as a valid key for the authentication or not.

So I decided to go on researches on the internet to find out how I will be able to access the USB devices of my Windows 7 Computer and how I will be able to determine its current state, means knowing if it is plugged in at the current moment or not.
I found out I could use Marshal, which is a class located in the System.Runtime.InteropServices of .NET 4.0 Namespace to achieve this. A short bit later I was able to find a public class which would let me do exactly what I need. I decided not to reinvent wheels and use the public class I have found on CodeProject.

Here is a preview of what I have achieved at the end

So how did I do this? Well I am simply creating a List from a method in that class

List<string> listDevices = USBClass.GetListOfUSBDevices();

Then I am iterating through my list of devices comparing it to my target device

if (listDevices.Contains(myDevice)) {
    return true;
} else {
    return false;
}

And this is basically all that is needed for this to be working. Now when a device is connected it will be recognized by the software and added to the list. If that particular device is the key device to pass the USB authentication system then the listDevices.Contains(myDevice) function will return a true.

Tutorial Video:

Here are the two functions I have used in my video

private static List<string> HashListMembers(List<string> list) {
    List<string> hashes = new List<string>();

    for (int i = 0; i < list.Count; i++) {
        hashes.Add(Hash.SHA256(list[i]));
    }

    return hashes;
}

private static bool DeviceConnected(string deviceHash) {
    return HashListMembers(USBClass.GetListOfUSBDevices()).Contains(deviceHash);
}

I hope this post helped you, please let me know of flaws and issues in the comments. I will appreciate any constructing feedback in the comment section.

One thought on “Using USB Device as Key for Authentication System

Leave a Reply

Your email address will not be published. Required fields are marked *