intrbiz / arduino-crypto

A minimal crypto library for ESP8266 Arduino
Other
140 stars 53 forks source link

Error in library #20

Closed jgp72 closed 4 years ago

jgp72 commented 6 years ago

using your library and comparing it with PHP openssl in AES.128 example with the same key (0x2B,0x7E,0x15,0x16,0x28,0xAE,0xD2,0xA6,0xAB,0xF7,0x15,0x88,0x09,0xCF,0x4F,0x3C) and the same iv (0xF1,0x89,0xBA,0x2A,0x79,0x1F,0x04,0x81,0xFB,0x6A,0x13,0x31,0x02,0x69,0xAE,0x40) about the plaintext "eyJ1c3VhcmlvIjoibWlBcmR1aW5vMCJ9" we obtain different results.

php in hex 388d2bf238129b58bc5e5da5c66076b6b5a08a9836514de9dd50b74d0d4b33c1e0bcd095a3a2b77188bc99d7669a10c8

your libery 388d2bf238129b58bc5e5da5c66076b6b5a08a9836514de9dd50b74d0d4b33c1bfed0d3ee53f9ec68b61698f5303449e

thes last 32 digit is diferent

vikingkk commented 5 years ago

Dear @intrbiz I have same case with jgp72 here is my code

include

include

define BLOCK_SIZE 16

byte key[BLOCK_SIZE] = {0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30}; byte iv[BLOCK_SIZE] = {0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30};

void bufferSize(char text, int &length) { int i = strlen(text); int buf = round(i / BLOCK_SIZE) BLOCK_SIZE; length = (buf < i) ? buf + BLOCK_SIZE : length = buf; }

void encrypt(char plain_text, char output, int length) { byte enciphered[length];

AES aesEncryptor(key, iv, AES::AES_MODE_128, AES::CIPHER_ENCRYPT); aesEncryptor.process((uint8_t*)plain_text, enciphered, length);

int encrypted_size = sizeof(enciphered); char encoded[encrypted_size]; base64_encode(encoded, (char*)enciphered, encrypted_size);

strcpy(output, encoded); }

void decrypt(char enciphered, char output, int length) { length = length + 1; //re-adjust int decodedLen = base64_dec_len(enciphered, length); char decoded[length];

Serial.println(enciphered); base64_decode(decoded, enciphered, length); bufferSize(enciphered, length); byte deciphered[length]; AES aesDecryptor(key, iv, AES::AES_MODE_128, AES::CIPHER_DECRYPT);

aesDecryptor.process((uint8_t)decoded, deciphered, length); strcpy(output, (char)deciphered); }

void setup() { Serial.begin(115200); while (!Serial) { ; //wait }

char plain_text[] = "Now is the time ABCDABC";

// encrypt int length = 0; bufferSize(plain_text, length); Serial.println(length); char encrypted[length]; encrypt(plain_text, encrypted, length);

Serial.println(encrypted);

// decrypt

length = strlen(encrypted); char decrypted[length]; decrypt(encrypted, decrypted, length);

Serial.println(decrypted); } void loop() { }

the encrypted is 1a7OeiH628V7IIoLU6+3n70Dzp6FBQjlGPxSwnuXdzo=

but I'm encrypt in this web AES Encryption and Decryption Online Tool and the result is 1a7OeiH628V7IIoLU6+3n7ILev6IwcZYVNLalS/TBEg=

Can you help me encrypt result same with this web? Thanks a lot!

vikingkk commented 5 years ago

Is it block cipher encryption by 8 byte in arduino, and block cipher encryption encryption by 32byte in web?

if it is, how to change AES lib to encryption by 32 byte in arduino?