1

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?

  • Can you explain what was wrong with the libraries you looked at? I don't think AES or DES is simple enough to just paste in an answer. – BrettAM Jul 13 '15 at 22:58
  • 1
    We have tried several online libraries with no success - what online libraries? Your question is very short on detail. – Nick Gammon Jul 13 '15 at 23:00
  • 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? – user1027620 Jul 13 '15 at 23:04
  • 1
    So what you need then is to figure out how to pad and then break a message into the 16 byte chucks specified by AES reversably. – BrettAM Jul 14 '15 at 1:02
  • 1
    Its really simple. Just encrypt the first 16 bytes, then the next 16 bytes and so on. If you are left with, say, 5 bytes, append 11 bytes of zeros and encrypt that. Decryption is the same but instead of appending zeros, you cut off trailing zeros. – BrettAM Jul 14 '15 at 1:30
4

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? Thanks – user1027620 Jul 13 '15 at 22:47
  • So i should md5 the string and then apply AES on it? – user1027620 Jul 14 '15 at 1:19
  • That was my suggestion, yes. – Nick Gammon Jul 14 '15 at 1:26
  • 1
    I 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. – Nick Gammon Jul 14 '15 at 1:38
  • 2
    I think you should start a new question, like "can an encryption key stored on a Uno be discovered?" - this is really a new topic. – Nick Gammon Jul 14 '15 at 2:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.