Okay, here's the story. I'm not very familiar with how Arduino works but learning. I have a project that requires AES encryption on the Arduino Uno. As per a previous question we managed to figure out the problem but failed to resolve it fully so we started using another library. (This one: https://github.com/qistoph/ArduinoAES256)
uint8_t key[] = { //
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
};
Now onto the problem itself. Using an online AES encryption tool such as: http://aesencryption.net/ Simply asks for a Key of the encryption
in plain text and the output is base64
as specified by the service here: The result of the encryption will appear in base64 encoded to prevent character encoding problems.
Also, plain text can be used to process the encryption using AES
algorithm.
I've been trying to achieve the exact same result using an Arduino Uno
board with the exact same requirements. The end result is to have a base64
string that can be decrypted on another remote location having the same key
.
Now as for the .ino
file it has this setup:
#include "aes256.h" //Include library files
#define DUMP(str, i, buf, sz) { Serial.println(str); \
for(i=0; i<(sz); ++i) { if(buf[i]<0x10) Serial.print('0'); Serial.print(char(buf[i]), HEX); } \
Serial.println(); } //Help function for printing the Output
aes256_context ctxt;
void setup() {
int i;
Serial.begin(9600);
Serial.println("Initializing AES256... ");
uint8_t key[] = { //
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
};
DUMP("Key: ", i, key, sizeof(key));
aes256_init(&ctxt, key);
uint8_t data[] = { //"asdfasdfasdfasdf"
0x61, 0x73, 0x64, 0x66, 0x61, 0x73, 0x64, 0x66,
0x61, 0x73, 0x64, 0x66, 0x61, 0x73, 0x64, 0x66
};
DUMP("Unencrypted data: ", i, data, sizeof(data));
aes256_encrypt_ecb(&ctxt, data);
DUMP("Encrypted data: ", i, data, sizeof(data));
aes256_decrypt_ecb(&ctxt, data);
DUMP("Back decrypted data: ", i, data, sizeof(data));
aes256_done(&ctxt);
}
void loop() {
}
1) I can't seem to understand what is required to have a plain text
key and plain text
message to encrypt giving an end result of base64
output.
For example if I were to do this using JavaScript
: (And this is just an example).
2) I'd like to achieve something like this:
var key = "32 characters plain text here";
var plain = "Unlimited characters text here that can be used by the library to encrypt this plain message variable";
var base64_output = aes.encrypt256(key, plain);
3) I'd like to have the base64_output as base64 string so that it can be printed on a third-party printer hooked to the Arduino. - Is this impossible to achieve? We've been trying for some time now from endless conversions to splitting up our plain
text into chunks of 16 characters so that we can encrypt the message.
Any help is greatly appreciated. If someone doesn't mind us contacting them over the phone we would also greatly appreciate a phone call to have this issue explained and resolve so we can share our experience with the Arduino community.
Thanks in advance.
edit:
We're getting this error
edit2:
Thank you @Nick Gammon for the quick response.
After removing const from the function 4 places and the key
variable declaration, everything seems to be working fine now.
We modified the code in order to print all bytes for each cipher block. We have this output:
140 145 154 38 228 66 98 128 116 68 245 193 70 84 205 115
Œ‘š&äBb€tDõÁFTÍsŒ‘š&äBb€tDõÁFTÍs
186 174 17 21 162 83 96 210 128 134 46 206 172 215 0 103
º®¢S`Ò€†.ά×
112 4 216 150 27 202 6 176 77 159 123 231 127 128 32 132
pؖʰMŸ{ç€ „pؖʰMŸ{ç€ „
215 110 118 33 167 193 212 121 156 46 63 139 183 124 63 152
×nv!§ÁÔyœ.?‹·|?˜×nv!§ÁÔyœ.?‹·|?˜
130 91 174 118 4 202 250 74 228 7 173 40 246 124 53 173
‚[®vÊúJä(ö|5‚[®vÊúJä(ö|5
231 48 204 82 235 172 202 139 33 134 137 81 209 191 210 217
ç0ÌRë¬Ê‹!†‰QÑ¿ÒÙç0ÌRë¬Ê‹!†‰QÑ¿ÒÙ
184 161 98 135 12 230 86 213 183 113 103 234 187 213 224 133
¸¡b‡æVÕ·qgê»Õà…¸¡b‡æVÕ·qgê»Õà…
Done
We're trying to convert each array of bytes into a char
array of hex values then base_64 encode it. But we're getting this output which is not what we want. Can you please explain how can we achieve this.
Please let us know if it is possible to open a chat with you so that we can understand this better.
JavaScript
example above for the key and plain variables and convert them to uint8_t arrays of hex. Among other issues described above.We've been trying for some time now from endless conversions to splitting up our plain text into chunks of 16 characters so that we can encrypt the message.
- I'm a little confused as to why that is a problem. Can you show your attempts to break your string of text into groups of 16 bytes? I'm thinking: a for loop - what are you thinking?