Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // reference https://rweather.github.io/arduinolibs/crypto.html
- #include <CBC.h>
- #include <AES.h>
- #define BLOCK_SIZE 16
- CBC<AES128> cbc;
- byte key[BLOCK_SIZE] = "_###myKey1234###";
- byte iv[BLOCK_SIZE];
- byte chiper[BLOCK_SIZE];
- void setup() {
- Serial.begin(115200);
- // Set key
- cbc.setKey(key, BLOCK_SIZE);
- // perform test
- for(byte i = 0; i<5; i++){
- Serial.println();
- Serial.print("Run no. ");
- Serial.println(i);
- test_AES128();
- }
- }
- void loop() {
- // put your main code here, to run repeatedly:
- }
- // Helper routine to dump a byte array as hex values to Serial.
- void printHex(byte *buffer, byte bufferSize) {
- for (byte i = 0; i < bufferSize; i++) {
- Serial.print(buffer[i] < 0x10 ? " 0" : " ");
- Serial.print(buffer[i], HEX);
- }
- Serial.println();
- }
- void printString(byte *buffer, byte bufferSize) {
- for (byte i = 0; i < bufferSize; i++) {
- Serial.print((char)buffer[i]);
- }
- Serial.println();
- }
- void test_AES128(){
- long t1, t2 = 0;
- // Print original plain text (just for checking)
- byte plain[] = "0123456789ABCDEF";
- Serial.print("original plain text:");
- printString(plain, sizeof(plain));
- Serial.print("original plain byte:");
- printHex(plain, sizeof(plain));
- // Generate a random initialization vector
- for (int i = 0 ; i < BLOCK_SIZE ; i++ ) {
- iv[i]= random(0xFF);
- }
- // Set initialization vector
- t1 = micros();
- cbc.setIV(iv, BLOCK_SIZE);
- t2 = micros() - t1;
- Serial.print(t2);
- Serial.print(" uS - IV:");
- printHex(iv, sizeof(iv));
- // encrypt
- t1 = micros();
- cbc.encrypt(chiper, plain, BLOCK_SIZE);
- t2 = micros() - t1;
- Serial.print(t2);
- Serial.print(" uS - encrypted chiper:");
- printHex(chiper, sizeof(chiper));
- // decrypt
- t1 = micros();
- cbc.decrypt(chiper, plain, BLOCK_SIZE);
- t2 = micros() - t1;
- Serial.print(t2);
- Serial.print(" uS - decrypted plain:");
- printHex(plain, sizeof(plain));
- cbc.clear();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement