We are trying to implement AES or DES encryption using an Arduino Uno.
We have a keypad module attached that will be used to input integers. A String should be generated based on the entered data and encrypted using AES or DES.
We have tried several online libraries with no success due to the constraints put on the "plain text" size.
An example pseudo-code for the above:
string key = "This is a key";
string plain = "This is the keypad input" + keypadInput + "and some other random stuff"; <- This should have any size.
var cipher = AES.encode(plain, key);
print(cipher);
var decoded = AES.decode(cipher, key);
print(decoded);
Could someone please point to the right direction on how to do this?
1 Answer 1
We have no experience with the byte[] objects and whatnot.
Might be time to do a few tutorials. You won't get far programming if you don't learn how to use arrays.
The string class is part of the STL (Standard Template Library) which does not come pre-installed on the Arduino (however you can download it if you want to).
The Standard Template Library (STL) for AVR with C++ streams
Briefly, however you could do something like this:
char key [20] = "This is a key";
char keypadInput [16]; // whatever they entered
char plain[80];
strcpy (plain, "This is the keypad input");
strcat (plain, keypadInput);
strcat (plain, "and some other random stuff");
The example code doesn't have any size checking. You would want to make sure that you don't put more than 79 characters into "plain" (to allow for the terminating 0x00 character). For example, look up strncat.
Can you please elaborate on how DES or AES as described in the question can be implemented?
Have you done much research? Have you Googled Arduino AES encryption
like I just did? I got this as the first hit:
Topic: new AES library - Arduino Forum
I've written an AES (Advanced Encryption Standard) library for Arduino. It supports 128, 192 and 256 bit key sizes. Code space overhead is about 4KB I think, each instance requires 240 bytes RAM for subkeys. Fairly tightly coded and checked against official test vectors for ECB mode. Also CBC mode supported.
It seems that if value of plain is larger than 16 characters the library will encrypt the first 16 characters only... Is this an Arduino Uno limitation?
I don't want to seem unhelpful, but you really should do some research before asking these questions. From Wikipedia - Advanced Encryption Standard (AES):
AES is a variant of Rijndael which has a fixed block size of 128 bits, and a key size of 128, 192, or 256 bits.
A "fixed block size of 128 bits" means you encrypt 128 bits at a time, that is, 128/8 = 16 bytes
.
So no, it is not an Arduino Uno limitation, it is the way AES works.
Also see Wikipedia - DES.
In the case of DES, the block size is 64 bits.
So DES does 8 bytes at a time.
What you can do with a password is to hash it, for example: Secure Hash Algorithm or MD5 (amongst others). These algorithms take a variable length string and return a fixed "hash". In the case of MD5 the output is a 128-bit field.
You could then encrypt that 128-bit hash, if you want to keep it a secret.
Would encrypting the 16 bytes and then append the next 16 bytes and so on result to the same output as if the whole plain text was encrypted in the first place in one go?
The suggestion by @BrettAM was, if you want to encrypt more than 16 bytes, do them 16 bytes at a time, padding if necessary the final block.
eg.
AAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBB CCCCCCCCCC000000
encrypt encrypt encrypt
LLLLLLLLLLLLLLLL MMMMMMMMMMMMMMMM NNNNNNNNNNNNNNNN
decrypt decrypt decrypt
AAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBB CCCCCCCCCC000000
In this example the final block was padded with zeros. Now the receiving end decrypts in 16-byte blocks.
-
Can you please elaborate on how DES or AES as described in the question can be implemented? Or the proper way to do so? Thanksuser1027620– user10276202015年07月13日 22:47:45 +00:00Commented Jul 13, 2015 at 22:47
-
So i should md5 the string and then apply AES on it?user1027620– user10276202015年07月14日 01:19:42 +00:00Commented Jul 14, 2015 at 1:19
-
That was my suggestion, yes.2015年07月14日 01:26:24 +00:00Commented Jul 14, 2015 at 1:26
-
1I agree with @BrettAM however the original question specifically asked about
We are trying to implement AES or DES encryption using an Arduino Uno
. Without knowing more details about this project it is hard to give a definitive answer. A straight hash might fall victim to a dictionary attack, whereas if it was encrypted, and if the key was not known, that might protect it.2015年07月14日 01:38:26 +00:00Commented Jul 14, 2015 at 1:38 -
2I think you should start a new question, like "can an encryption key stored on a Uno be discovered?" - this is really a new topic.2015年07月14日 02:38:39 +00:00Commented Jul 14, 2015 at 2:38
We have tried several online libraries with no success
- what online libraries? Your question is very short on detail.plain
is larger than 16 characters the library will encrypt the first 16 characters only... Is this an Arduino Uno limitation?